Write and test a function search( ) that searches for an integer s in an array o
ID: 3790739 • Letter: W
Question
Write and test a function search( ) that searches for an integer s in an array of n elements. If s is found to match an element in the array, the function returns the address of that element; otherwise it returns NULL. The function must use the travelling pointer (version-2) notation to traverse the array. The function has the following prototype. int* search ( int *p , int s, int n) 2. Write and test a function search( that searches for an integer s in an array of n elements. If s is found to match an element in the array, the function returns the address of that element; otherwise it returns NULL. The function must use the travelling pointer (version-2) notation to traverse the array. The function has the following prototype. int search int *p, int s, int n)Explanation / Answer
#include<stdio.h>
int * search(int *ptr,int s,int n)
{
if(n<0)
return NULL;
if(*(ptr+n)==s)
return (ptr+n);
else return search( ptr,s,n-1);
}
int main()
{
int array[]={8,9,4,2,6,7,5}; // Array of an element
int s=2; // search element
int *ptr;
int n=sizeof(array)/sizeof(array[0]);
ptr=search(array,s,7);
printf("%d",ptr);// print the address of an element;
printf(" %d",*ptr);// print an element
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.