I have a function compress2d() that takes as input a 2-dimensional array (10x10) of binary data, compresses each row of the array by replacing each run of 0s or 1s with a single 0 or 1 and the number of times it occurs, and prints on each line the result of compression. For example, the row with data 0011100011 may be compressed into 02130312.
eg:
input:
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
Desired Output:
1 5 0 5
0 5 1 5
1 5 0 5
0 5 1 5
1 5 0 5
0 5 1 5
1 5 0 5
0 5 1 5
1 5 0 5
0 5 1 5
But i am getting:
1 5
1 4
1 4
1 4
.
.
.
.
1 4
Here is my code:
void compress2D(int data[SIZE][SIZE])
{
int i,j=0, target, count=1, location=1;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (data[i][j] == data[i][j+1] && (j+1) < 10)
count++;
else {
data[i][location] = count;
data[i][location+1] = data[i][j+1];
count = 1;
location +=2;
}
}
data[i][location] = '\0';
location = 1;
count = 0;
}
for (i = 0; i < SIZE;i++) {
for (j = 0; data[i][j] != '\0'; j++) {
printf("%d ", data[i][j]);
}
printf("\n");
}
}