Question

In: Computer Science

(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!

Solutions

Expert Solution

If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.

Solution :

#include <stdio.h>
// main function
int main()
{
// define a 2d character array
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}};
// initialize a 1d integer array
int arr2[8];
// iterate the 2d charater array
for(int i=0;i<8;i++)
{
// declare val = 0
int val=0;
for(int j=0;j<8;j++)
{
// use bit shifting and add to val to get the number
val = val + (arr[i][j])*(1<<(8-j-1));
}
// insert the integer val to the integer array
arr2[i]=val;
}   
// print the integer array
for(int i=0;i<8;i++)
{
printf("%d ",arr2[i]);
}
return 0;
}


Related Solutions

in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
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
language is c++ I need to take a num and put it into an array one...
language is c++ I need to take a num and put it into an array one number at a time suck that the hundredths place is at 10^3, tens is 10^2 and so on For milestone 1 you need to build a constructor that converts a int to a bigInt You need to peel off each digit from the int and place it into the appropriate location in the array of int. To do this you need to use integer...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text and String key consisting of integers only, such that int rows=(int)Math.ceil(text.length()/key.length()); // or int rows=(int)Math.ceil(text.length()/key.length()); ? int columns= key.length(); int remainder= text.length() % key.length(); // such that the last row avoids taking an index beyond the string text by making columns - remainder The method fills the 2d array with letters a String entered by the use (row by row)....
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
(C LANGUAGE) Write a function called upperLower that inputs a line of text into char array...
(C LANGUAGE) Write a function called upperLower that inputs a line of text into char array s[100]. Output the line in uppercase letters and in lowercase letters This is what I have so far: void * upperLower (const char * s) { char *word[sizeof(s)]; for(int i = 0; i < sizeof(s); i++){ *word[i] = s[i]; } word = strtok(word, " "); int counter = 0; while(word != NULL){ if(counter % 2 == 0){ word[counter] = toupper(word); printf("%s ", word); }...
For a given two-dimensional array in C as follows (Each int-type element occupies 4 bytes of...
For a given two-dimensional array in C as follows (Each int-type element occupies 4 bytes of memory) int A[8][16]; If the address of A[1][4] is 0x0FFA0040, what is the memory address of A[2][6]?
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++;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT