Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following function int search(int a[], int n, int key, int **loc); a i

ID: 3848307 • Letter: W

Question

Write the following function 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

int bitsToInteger(char binary[])
{
   int significantBits = 32;
int power = pow(2,significantBits-1);
int sum = 0;
int i;

for (i=0; i<significantBits; ++i)
{
if ( i==0 && binary[i]!='0')
{
sum = power * -1;
}
else
{
sum += (binary[i]-'0')*power;//The -0 is needed
}
power /= 2;
}

return -sum;
}