In: Computer Science
C++. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not.
Sample Run:
Please input the number of id numbers to be read
4
Please enter an id number
96
Please enter an id number
97
Please enter an id number
98
Please enter an id number
99
Please input an id number to be searched
67
67 is not in the array
ANSWER :
CODE :
#include <iostream>
using namespace std;
void search(int arr[], int n,int x)
{
int a = 0;
for(int i=0; i<n; i++)
{
if(arr[i] == x)
{
a= 1;
break;
}
}
if(a==0)
{
cout<<x<<" is not in the array";
}
else
{
cout<<x<<" is in the array";
}
}
int main()
{
int n;
int x;
cout<<"Please input the number of id numbers to
read"<<endl;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cout<<"Please enter an id number"<<endl;
cin>>arr[i];
}
cout<<"Please input an id number to be
searched"<<endl;
cin>>x;
search(arr,n,x);
return 0;
}
OUTPUT :