What I am trying to do is that,
for example, assuming that there are 6 and 4 for width and height respectively, and the inputs for the 2d array are:
0 1 2 2 1 0
1 0 0 0 0 1
1 0 0 0 0 1
0 1 1 1 1 0
The 2nd row finishes with 1 and the 3rd row starts with 1, hence the number in "count" variable increases its value as it regards the "1s" as duplicate number.
To be more specific,
the overall output for those inputs should be:
1 2 1 (1st row)
1 1 (2nd row)
1 1 (3rd row)
4 (4th row)
The first row has one 1 and two consecutive 2s and one 1, hence it becomes 1 2 1.
The second row has two 1s but they are not linked each other so it becomes 1 1.
The last row has four consecutive 1s so it becomes 4.
int main(void) {
int width = 0;
int height = 0;
int input = 0;
int i, j = 0;
int val = 1;
scanf("%d %d", &width, &height);
int board[height][width];
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
scanf("%d", &input);
board[i][j] = input;
}
}
for(i = 0; i < height; i ++)
{
val = 1;
for(j = 0; j < width; j++)
{
if(board[i][j]>0)
{
if(board[i][j] == board[i][j+1])
{
val++;
printf("%d ", val);
}
if(board[i][j+1] == 0)
{
val = 1;
}
if(board[i][j] != board[i][j+1] && board[i][j] != board[i][j-1])
{
val = 1;
printf("%d ", val);
}
}
}
printf("\n");
}
return 0;
}
This is what I have done so far but there are two problems... Using the inputs above, this code outputs:
1 2 1
1 2
1
2 3 4
where second row seems to having a problem that I mentioned above and the fourth row prints out all the process of val++, not the final result of val++.