In: Computer Science
in C
i have a 2d array char m[8][8] =
{{1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,1},
{1,0,1,0,1,1,0,1},
{1,0,1,0,0,0,0,1},
{1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,1},
{1,0,1,0,1,0,1,1},
{1,1,1,1,1,1,1,1}}
turn this array into a 8 integer array int new[8]={-1,-119,-83, ......}
i.e Two's Complement to integer 10001001 --> -119
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
#include <stdio.h>
int getConvertedToDecimal(int * a, int size){
int val=0;
int i;
int twoVal=1;
for(i=7;i>=0;i--){
val += twoVal*a[i];
twoVal= twoVal*2;
}
return val-256;
}
int main()
{
int array[8][8] = {{1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,1},
{1,0,1,0,1,1,0,1},
{1,0,1,0,0,0,0,1},
{1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,1},
{1,0,1,0,1,0,1,1},
{1,1,1,1,1,1,1,1}};
int size=8;
int i;
int newArray[8];
for(i=0;i<8;i++){
int
dec=getConvertedToDecimal(array[i],size);
newArray[i] = dec;
}
printf("The content of array is : ");
for(i=0;i<8;i++){
printf("%d ",newArray[i]);
}
return 0;
}