Home > Software engineering >  bad output from bytes to block in c
bad output from bytes to block in c

Time:11-26

Hi I can't fix the code to have the output as in example 1. My output is in example 2. Can anyone advise me what I'm doing wrong or where am I wrong? Thank you for every answer I have no idea what could be wrong I tried a lot of possibilities and still nothing.

int main{

int length = 4 1, cols = 3, offset = 2;
bool bytes1[4 1][8] = {
    {0,1,0,0,0,0,0,1},
    {0,1,1,0,1,0,0,0},
    {0,1,1,0,1,1,1,1},
    {0,1,1,0,1,0,1,0},
    {0,0,0,0,0,0,0,0}
};
bool blocks1[offset*8][cols];
bytes_to_blocks(cols, offset, blocks1, length, bytes1);
for(int j = 0; j < offset*8; j  ){
    for(int i = 0; i < cols; i  ){
        printf("%d ", (blocks1[j][i] == true) ? 1 : 0);
    }
    printf("\n");
    if(j % 8 == 7){
        printf("\n");
    }
}
}

The first example is what the code should look like

// 0 0 0 
// 1 1 1 
// 0 1 1 
// 0 0 0 
// 0 1 1 
// 0 0 1 
// 0 0 1 
// 1 0 1 
// 
// 0 0 0 
// 1 0 0 
// 1 0 0 
// 0 0 0 
// 1 0 0 
// 0 0 0 
// 1 0 0 
// 0 0 0

My output is here

0 0 0
0 1 1
1 0 1
1 0 0
0 0 1
1 0 0
1 0 0
1 1 0

0 0 0
1 0 0
1 0 240
0 0 220
1 0 0
0 0 114
1 0 49
0 0 0


My code is here

void bytes_to_blocks(const int cols, const int offset, bool blocks[offset*8][cols], const int rows, bool bytes[rows][8]) {
    int col = 0;
    int paragraph = 0;
    for(int row = 0; row < rows; row  ) {
        col  ;
        if(col > cols) {
            col = 0;
            paragraph  = 8;
        }
        for (int bit_index = 0; bit_index < 8; bit_index  ) {
            blocks[paragraph   bit_index][col] = bytes[row][bit_index];
        }
    }
 
}

Thank you for every advice

CodePudding user response:

int main{ is wrong. main needs to be declared as a function, as with int main(void) {.

This code:

        col  ;
        if(col >= cols) {
            col = 0;
            paragraph  = 8;
        }

increments col before it is used in:

        for (int bit_index = 0; bit_index < 8; bit_index  ) {
            blocks[paragraph   bit_index][col] = bytes[row][bit_index];
        }

That is wrong; the first time that latter code executes, it needs the initial value of col (zero), not the incremented value. Swap those two pieces of code.

if(col > cols) is wrong. Once col is equal to cols, it is too high and needs to be reset. Change the comparison to >=.

CodePudding user response:

I Change the comparison to >= and my output is bad


0 0 0
1 1 1
0 1 1
0 0 0
0 1 1
0 0 1
0 0 1
1 0 1

0 0 0
1 0 0
1 0 112
0 0 68
1 0 0
0 0 162
1 0 30
0 0 0

My code look this:


void bytes_to_blocks(const int cols, const int offset, bool blocks[offset*8][co>
    int col = 0;
    int par = 0;
    for(int row = 0; row < rows; row  ) {
         

        for (int bit_index = 0; bit_index < 8; bit_index  ) {
            blocks[par   bit_index][col] = bytes[row][bit_index];
        }
        col  ;
         if(col >= cols) {
            col = 0;
            par  =8;
        }
    }
}

Thank you for help.

  •  Tags:  
  • c
  • Related