Please follow the instructions as it says. I did not quite undestood how exactly
ID: 3587645 • Letter: P
Question
Please follow the instructions as it says. I did not quite undestood how exactly she will test. Is my program will be tested from the command line or inside the main function? It would be great if I had a answers to these questions with Coding fully. Thank you.
4. Write a C program to convert a 2's complement binary number to decimal in C. I will test the code using function calls similar to below. Test for different size binary numbers. The function prototype to use is also provided below. char data[5]'1', '0', '1', '0', '0'; printf("%d ", convert-2s-complement-to-decimal(data, 5)); char data1 [3] printf("%d ", {'0', '1', '1'); convert-2s-complement-to-decimal(datal, 3)); char data2 [6] = {'1', '1', 'O', '1', '1', '1'); printf("sd ", convert_2s_complement_to_decimal(data2, 6)); // Here's what will be in convert.h header file /I Takes an array of bits and the length of the bits to return the corresponding // decimal equivalent. int convert_2s_complement_to_decimal(char[, int); You must write your code in convert.c source code file and make sure that it will compile on cssgate without any issues. Only provide the convert.c file.Explanation / Answer
#include<stdio.h>
#include<math.h>
int convert_2s_complement_to_decimal(char a[], int n){
int d = 0;
int i;
for (i = 1; i<n; i++){
if (a[i] == '1')
d = d + pow(2,(n-1)-i);
}
if (a[0] == '1')
return -d;
else
return d;
}
int main(){
char data[5] = {'1','0','1','0','0'};
printf("%d ",convert_2s_complement_to_decimal(data, 5));
char data1[3] = {'0','1','1'};
printf("%d ",convert_2s_complement_to_decimal(data1, 3));
char data2[6] = {'1','1','0','1','1','1'};
printf("%d ",convert_2s_complement_to_decimal(data2, 6));
}