Write the following functioned int search(int a[], int n, int key, int **loc); a
ID: 3848175 • Letter: W
Question
Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and test it. Write a function to convert a string of 32 '1' and '0's in 2's complement form to a 32 bit signed integer: int bits2Integer(char bitString[]); The bit string is stored in the bitString[] argument in "natural" form, i.e., the MSB is on the left side and LSB on the right.Explanation / Answer
#include<stdio.h>
#include<conio.h>
//main method
void main()
{
int x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result = 0;
int** first_loc = NULL;
result = search(x, 10, 4, first_loc);
if(result)
{
printf("key found !!");
}
}
//search function
int search(int a[], int n, int key, int** loc)
{
int i = 0;
for(i = 0; i<n; i++)
{
if(a[i] == key) //logic to search for key
{
loc = &a[i];
return 1;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.