I have to write a program that “compresses” a matrix if in 4 adjacent cells there is a 1 in each else 0 and then move the next square and so on and so on.
the 1’s and 0’s are filled in a different matrix.
now when I tried to debug the program let’s say I had this matrix
0 0 0 0
0 1 0 1
1 0 0 1
1 1 0 1
when the loop read matrix[i+1] i == 0 it read the cells as [1 0 1 0] and not [0 1 0 1]
the one that it read doesn’t even exist in the matrix
this is the code in the start i initialize the comressed matrix to all cells as -1
void compress(int matrix2[][MSZ], int matrix[][CMSZ]) {
resetMat(matrix, CMSZ, CMSZ);
int row = 0;
int col = 0;
for (int i = 0; i < MSZ - 1; i += 2) {
for (int j = 0; j < MSZ - 1; j += 2) {
int val1 = matrix2[i][j];
int val2 = matrix2[i][j + 1];
int val3 = matrix2[i + 1][j];
int val4 = matrix2[i + 1][j + 1];
if (val1 == 1 && val1 == val2 && val2 == val3 && val3 == val4) {
matrix[row][col] = 1;
}
else {
matrix[row][col] = 0;
}
col++; // Move to the next column
}
row++; // Move to the next row
col = 0; // Reset col to 0 for the new row
}
}```
I think most of the logic is good but it's something I never encountered.
I wrote the whole of last semester in JAVA and now we learn C and my learinng curve is quite difficult but this is something I didn't think would be an issue
I use visual studio community if it matters
שליו מוחרר is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.