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

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++){...
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"
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT