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

(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!
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
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
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...
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...
I have this program in C that takes three char arrays that each have a first...
I have this program in C that takes three char arrays that each have a first and last name. I have two functions that reverese the name and change it to all upper case. I have the program completeed but need to change both functions to use pointers instead of arrays. I will bold the functions I need to use pointers. #include <stdio.h> void upper_string(char []); int main() { char name1[100]="John Smith"; char name2[100]="Mary Cohen"; char name3[100]="Carl Williams"; upper_string(name1);// calling...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow...
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 (int...
In C, build a connect 4 game with no GUI. Use a 2D array as the...
In C, build a connect 4 game with no GUI. Use a 2D array as the Data structure. First, build the skeleton of the game. Then, build the game. Some guidelines include... SKELETON: Connect Four is a game that alternates player 1 and player 2. You should keep track of whose turn it is next. Create functions: Initialization – print “Setting up the game”. Ask each player their name. Teardown – print “Destroying the game” Accept Input – accept a...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT