httpiimgurcomgGPjNCdpng Write the following function a is an
http://i.imgur.com/gGPjNCd.png
Write the following function: a is an array to be searched, n is the number of elements in the array, and key is the search key. search should return TRUE if key matches some element of a; FALSE if it doesn\'t. Use pointer arithmetic - not subscripting - to visit array elements.Solution
Here is code:
bool search (int a [], int n, int key){
int i;
/*search for values with pointer*/
for(i=0; i<n; i++){
if (key==*(a+i)){
return true;}
else{
return false;}
}
}
Here is sample code to test:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> // boolean values
bool search (int a [], int n, int key){
int i;
/*search for values with pointer*/
for(i=0; i<n; i++){
if (key==*(a+i)){
return true;}
else{
return false;}
}
}
int main()
{
int k, j=9;
int array[]={10, 20, 13, 32, 50, 25, 40, 49, 11, 12};
bool result;
k=10;
//call search
result = search(array,j,k);
if (result==true){
printf (\"%d does exist\", k);
}
else if(result==false){
printf (\"%d does not exist\", k);
}
return 0;
}
Output:
10 does exist
