using namespace std;
const int SORT_MAX_SIZE = 16;
// helper function
bool isPrime(int n, int i = 2)
{
if(n == 1)
return false;
if(n == 2)
return true;
if (n % i == 0)
return false;
if (i * i > n)
return true;
// Checking for next divisor
return isPrime(n, i + 1);
}
bool IsArrayPrimeIter(int *arr, int n){
cout<"entering>"entering>
for(int i=0;i<>
if(arr[i]==1){
cout<"leaving>"leaving>
return false;
}
for(int j=2;j*j<=arr[i];>=arr[i];>
if(arr[i]%j == 0){
cout<"leaving>"leaving>
return false;
}
}
}
cout<"leaving>"leaving>
return true;
}
bool IsArrayPrimeRecur(int *arr, int n){
cout<"entering>"entering>
if(n==0){
cout<"leaving>"leaving>
return true;
}
if(isPrime(arr[n-1])){
return IsArrayPrimeRecur(arr, n-1);
}
cout<"leaving>"leaving>
return false;
}
int main(){
cout<"enter number="" of="" elements(max="16):">"enter>
int n; cin>>n;
if(n > SORT_MAX_SIZE){
cout<"invalid input!!="">"invalid>
return 0;
}
int *arr = new int[n];
cout<"enter the="" array="" elements[input="" range="" :="" (1="" to="" 99="" inclusive)]="" :="">"enter>
for (int i = 0; i < n;="" ++i)="">
cin>>arr[i];
if(arr[i] > 99 or arr[i]<>
cout<"invalid input!!="">"invalid>
return 0;
}
}
if(IsArrayPrimeIter(arr,n)){
cout<"prime array="" using="">"prime>
}else{
cout<"not a="" prime="" array="" using="">"not>
}
cout<>
if(IsArrayPrimeRecur(arr,n)){
cout<"prime array="" using="">"prime>
}else{
cout<"not a="" prime="" array="" using="">"not>
}
delete[] arr;
return 0;
}
Extracted text: Enter number of elements(max Enter the array elements[input range : (1 to 99 inclusive)] : 2 5 9 3 41 68 52 76 Entering IsArrayPrimeIter Leaving IsArrayPrimeIter Not a Prime Array using iteration 16): 8 Entering IsArrayPrimeRecur Leaving IsArrayPrimeRecur Not a Prime Array using recursion
Extracted text: Enter number of elements(max Enter the array elements[input range : (1 to 99 inclusive)] : 7 5 2 11 23 37 Entering IsArrayPrimeIter Leaving IsArrayPrimeIter Prime Array using iteration 16): 6 Entering IsArrayPrimeRecur Entering IsArrayPrimeRecur Entering IsArrayPrimeRecur Entering IsArrayPrimeRecur Entering IsArrayPrimeRecur Entering IsArrayPrimeRecur Entering IsArrayPrimeRecur Leaving IsArrayPrimeRecur Prime Array using recursion