C Array and Functions pleaes follow the format Consider an i
C++ (Array and Functions) pleaes follow the format
Consider an integer array named A of N elements. Write a function that returns 1 if a number num is present in the array and 0 if it is not.
Prgoram:
#include <iostream>
using namespace std;
// write the prototype
int main() {
int N;
int A[] = {5,9,7,6,2,3,8,4,1,18};
int num;
cin >> num;
cout << //output the returned value
return 0;
}
int search (int A[], int N, int num){ //complete the definition of the function
}
Solution
#include <iostream>
using namespace std;
int search (int A[], int N, int num); // write the prototype
int main() {
int N;
int A[] = {5,9,7,6,2,3,8,4,1,18};
int num;
cin >> num;
cout << search(A, 10, num)<<endl; //output the returned value
return 0;
}
int search (int A[], int N, int num){ //complete the definition of the function
for(int i=0; i<N; i++){
if(A[i] == num){
return 1;
}
}
return 0;
}
Output:
7
1
--------------
10
1
