Question :
A Bunker array is an array that contains the value 1 if and only if it contains a prime number. The array {7, 6, 10, 1} is a Bunker array because it contains a prime number (7) and also contains a 1. The array {7, 6, 10} is not a Bunker array because it contains a prime number (7) but does not contain a 1. The array {6, 10, 1} is not a Bunker array because it contains a 1 but does not contain a prime number.
It is okay if a Bunker array contains more than one value 1 and more than one prime, so the array {3, 7, 1, 8, 1} is a Bunker array (3 and 7 are the primes).
Write a function named isBunker that returns 1 if its array argument is a Bunker array and returns 0 otherwise.
You may assume the existence of a function named isPrime that returns 1 if its argument is a prime and returns 0 otherwise. You do not have to write isPrime, you can just call it.
If you are programming in Java or C#, the function signature is
int isBunker(int [ ] a)
If you are programming in C or C++, the function signature is
int isBunker(int a[ ], int len) where len is the number of elements in the array.
Solution :
A Bunker array is an array that contains the value 1 if and only if it contains a prime number. The array {7, 6, 10, 1} is a Bunker array because it contains a prime number (7) and also contains a 1. The array {7, 6, 10} is not a Bunker array because it contains a prime number (7) but does not contain a 1. The array {6, 10, 1} is not a Bunker array because it contains a 1 but does not contain a prime number.
It is okay if a Bunker array contains more than one value 1 and more than one prime, so the array {3, 7, 1, 8, 1} is a Bunker array (3 and 7 are the primes).
Write a function named isBunker that returns 1 if its array argument is a Bunker array and returns 0 otherwise.
You may assume the existence of a function named isPrime that returns 1 if its argument is a prime and returns 0 otherwise. You do not have to write isPrime, you can just call it.
If you are programming in Java or C#, the function signature is
int isBunker(int [ ] a)
If you are programming in C or C++, the function signature is
int isBunker(int a[ ], int len) where len is the number of elements in the array.
Solution :
public static int
isBunker(int [] a)
{
int rtnVal
=0;
for(int i=0;i<a.Length;i++)
{
if(isPrimeCheck(a[i]))
{
for (int j = 0; j < a.Length; j++)
{
if (a[j] == 1)
{
rtnVal = 1;
break;
}
}
}
}
return rtnVal;
}
public static bool isPrimeCheck(int n)
{
bool isPrime = true;
if (n
<= 0)
isPrime=false;
if (n ==
1)
isPrime=false;
for(int i=2;i<=n/2;i++)
{
if(n%2==0)
{
isPrime=false;
break;
}
}
return isPrime;
}
-------------------------------------------
ReplyDelete--------this methode is true---------------
-------------------------------------------
static boolean isPrime(int nbr){
boolean isPrim=true;
if(nbr==1)
isPrim=false;
if(nbr<=0)
isPrim=false;
for(int i=2;i<=nbr/2;i++){
if(nbr%i==0){
isPrim=false;
}
}
return isPrim;
}
----------------------------------------
----------------------------------------
static int bunkerArray(int []a){
int len=a.length;
int isBunker=0;
for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
if(isPrime(a[i])){
if(a[j]==1) {
isBunker=1;
break;
}
}
}
}
return isBunker;
}
---------------------------------------
---------------------------------------