Using (Microsoft Visual Studio C++ 6.0) software, follow the sample output to write and run a program that uses a function called search which will use pointers to search for the address of a given integer t in an array a of size n. If the given integer is found, the function returns its address; otherwise it returns NULL.
The prototype of the function is as follow:
int*search(inta[], intn, int t);
Sample Output (1):
Enter the size of the array: 4
Enter the values in the array: 33 55 77 99
Enter the value to search for: 99
The value 99was found and its address is 0012FDC4
Sample Output (2):
Enter the size of the array: 5
Enter the values in the array: 22 44 66 88 11
Enter the value to search for: 50
The value 50 was not found.
-------------------------------------------------
#include<iostream>
using namespace std;
int* search(int a[], int n, int t);
int main()
{
int *aPtr;
int n,t;
int a[100];
int index;
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"Enter the values in the array: ";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the value to search for: ";
cin>>t;
index=search(a,n,t);
if(index!=-1)
cout<<"The value "<<t<<" was found and its address is "<<*&aPtr<<endl;
Function "search" is supposed to return the address of the element of an array if it is equal to the search value. So modify the lines from 24-48 with the following code change:
aPtr=search(a,n,t);
if(aPtr!=NULL)
cout<<"The value "<<t<<" was found and its address is "<<aPtr<<endl;
else
cout<<"The value "<<t<<" was not found."<<endl;
return 0;
}
int* search(int *a, int n, int t)
{
for(int i=0;i<n;i++)
if(*(a+i)==t)
return &a[i];
return NULL;
If you're using this as part of a course... that's unfortunate. Look around for a more modern course. You will be learning a bunch of stuff that either has changed or is just flat-out wrong and won't work on modern compilers.
If this is not part of a course.... update your compiler.