Question

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},...

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

Solutions

Expert Solution

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;
}


Related Solutions

How can i bubble sort a sentence in a char array in c++ This is the...
How can i bubble sort a sentence in a char array in c++ This is the prototype of the function: char* sort(char string[], int numOfWords, int lengthOfWord); This is the testing code in the main file: char words[] = "CAT FAT BAT HAT RAT"; printf("Before sort: t%s\n";words); char result = sort(words; 5; 3); printf("After sort : t%s\n"; result); Expected output: Before sort: CAT FAT BAT HAT RAT After sort: BAT CAT FAT HAT RAT
for C program 10 by 10 char array. char 0-9 as rows and char a-j as...
for C program 10 by 10 char array. char 0-9 as rows and char a-j as collumns.
I am running this code using C to count the words in char array, but somehow...
I am running this code using C to count the words in char array, but somehow it keeps counting the total characters + 1. Any solution to fix this problem? #include <stdio.h> int main() {    char input[1001];    int count =0;    fgets(input, 1001, stdin);    char *array = input;    while(*array != '\0')       {        if(*array == ' '){            array++;        }        else{        count++;        array++;...
(In C language) Given a two-dimensional char array, how do I encode it into a one...
(In C language) Given a two-dimensional char array, how do I encode it into a one dimensional integer array? For example: char arr [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}} into int arr2 [8] I know this problem requires bit-shifting but I am unsure how. It also needs to be as efficient as possible. Thanks!
Send an element from 2d array to a function using pointer to pointer. c++ I am...
Send an element from 2d array to a function using pointer to pointer. c++ I am having an issue with being able to send specific elements to the swap function. #include <iostream> using namespace std; void swap(int **a, int **b){    int temp = **a;    **a=**b;    **b=temp; } void selSort(int **arr, int d){    for(int i =0; i<d-1; i++){        int min = *(*arr+i);        int minin = i;        for(int j =i+1; j<d; j++){...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one hundred rows and fifty columns. Iterate through each element and assign it a random value between -72 and 75 inclusive. Have your random number seed be 25. Create functions that do the following: 1. A function called “sum” that returns the sum of all the elements in your 2D Array. 2. A function called “average” that return the average value of the elements in...
Write a C program that generates the following 2D array. 64   32   16   8   4   2  ...
Write a C program that generates the following 2D array. 64   32   16   8   4   2   1 32   32   16   8   4   1   1 16   16   16   8   4   2   1 8   8   8   8   4   2   1 4   4   4   4   4   2   1 2   2   2   2   2   2   1 1   1   1   1   1   1   1
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.        There should be two public methods within the class definition namely: void setMatrixValue(int i, int j); that should set m[i][j] with user defined values int getMatrixValue(int i, int j); that should return m[i][j] Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication
C Implement the function Append (char** s1, char** s2) that appends the second character array to...
C Implement the function Append (char** s1, char** s2) that appends the second character array to the first, replaces the first array with the result and replaces the second character array with the original first array. For example, if the first character array is "hello" and the second is "world" at the end of the function the new value of the first character array should be"helloworld" and the second array should be "hello". If the input is invalid the function...
Translate this algorithm to code on C compare(char array[ ]) word = split(array, " "); //gets...
Translate this algorithm to code on C compare(char array[ ]) word = split(array, " "); //gets the first word before the blank space if word == Hello { print("I am saying hello"); }else if (word == Bye) print("I am saying goodbye"); } Example--------------------- char array = "Hello Ana"; compare(array); char array1 = "Bye Ana" compare(array1); result "I am saying hello" "I am saying goodbye"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT