0

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++.

5 Answers 5

2

Obviously you're going to get it printing out the progress of val as you have the printf in this part of the code.

        if(board[i][j] == board[i][j+1])
        {

            val++;
            printf("%d ", val); 
        } 

You've made the code overly complicated when all you really need to do is track how many of the last number there are and when the number changes or you reach the end of the row, then you output the count. Something like this maybe:

for(i = 0; i < height; i ++)
{
    val = 0;

    for(j = 0; j < width; j++)
    {
        if(board[i][j]>0)
        {
            if((j==0) || (board[i][j] != board[i][j-1]))
            {
                if(val)
                {
                    printf("%d ", val); 
                }
                val=1;
            }
            else
            {
                val++;
            }
        }   
    }
    if(val)
    {
        printf("%d ", val);
    }
    printf("\n");
}

Running it with your example input

6 4
0 1 2 2 1 0
1 0 0 0 0 1
1 0 0 0 0 1
0 1 1 1 1 0

gives the output as

1 2 1 
1 1 
1 1 
4 
Sign up to request clarification or add additional context in comments.

4 Comments

thank you so much Chris! one more question here... how do I print 0 when all inputs in a row are 0?
You'll need to have another variable that tracks whether or not it's printed anything for the row and if not, outputs val anyway
I have a last question Chris... what should I do if I want outputs in y - axis, not x-axis? for example, for the inputs above, the outputs should be: 2 / 1 1 / 1 1 / 1 1 / 2 /
That's more complicated and probably warrants an entirely new question being written as it depends on how you want the output laid out etc...
0

board[i][j] != board[i][j-1]

You start your j to 0 so you can't do that.

Instead of comparing your current value (board[i][j]) with your previous value [i][j - 1], i think it's easier to store the last value (prev = board[i][j]) at the end of the j loop, and compare your current value with that value. If it's the same, then you can increment your counter.

Before the loop, set your prev to -1 so you don't compare to prev if it's the first column.

Comments

0

I slightly modified your code

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]) // same, increase val
            {
                val++;
            } 

            if(board[i][j+1] == 0 || board[i][j] != board[i][j+1]) // differ to next one, print out and reset val
            {
                printf("%d ", val); 
                val = 1;
            }

        }

    }   
    printf("\n");
}

Comments

0

You jave to print the val value in the block if(board[i][j+1]==0) before setting val=1;

Comments

0
#define H 4
#define W 6

int a[H][W] = {
        {0,1,2,2,1,0},
        {1,0,0,0,0,1},
        {1,0,0,0,0,1},
        {0,1,1,1,1,0}
    };

void PrintRepeat() {
    for (int i = 0; i < H; i++) {
        int repeat = 0, prev = -1;
        for (int j = 0; j < W; j++) {
            if (a[i][j]){
                if (a[i][j] != prev) {
                    if (repeat) printf("%d ", repeat);
                    repeat = 1;
                }
                else repeat++; 
            }
            prev = a[i][j];
        }
        if (repeat) printf("%d\n", repeat);
    }
}

int main() {
    PrintRepeat();

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.