Continue computing in do, even if there are no columns

What I'm trying to do (not very successfully) is if my code detects a (if(matrix[i][j] ==1))coming signal (1 or 0) for the next few steps, I want my code to be written in a new matrix: newmatrix[i][j]=10and if not continue 0. Here is my code:

    for (i = 0; i < rows; i++) {
        j = 0;
        do {
            if (matrix[i][j] == 1) {
                int m = j;
                while (j < m + 3) {
                    newmatrix[i][j] = 10;
                    printf("newmatrix[%i][%i] and %f\n", i, j, newmatrix[i][j]);
                    j++;
                    continue;
                }
            }
            if (matrix[i][j] == 0) {
                newmatrix[i][j] = 0;
                printf("newmatrix[%i][%i] and 0 is %f\n", i, j, newmatrix[i][j]);
                j++;
                continue;   
            }
            j++;
        } while (j < MAXTIME);
    }
}

The problem is that if there is a signal near the end, and does not stop when the number of columns reaches the maximum number, the code inserts new columns, although they are only 10:

Where is my mistake can someone point me in the right direction? Could there be a way to make this cleaner using instructions goto?

+4
source share
1 answer

Here is a simpler approach with a temporary variable:

for (i = 0; i < rows; i++) {
    int spike = 0;
    for (j = 0; j < MAXTIME; j++) {
        if (matrix[i][j] == 1) {
           spike = 3;
        }
        if (spike) {
           newmatrix[i][j] = 10;
           spike--;
        } else {
           newmatrix[i][j] = 0;
        }
        printf("newmatrix[%i][%i] is %f\n", i, j, newmatrix[i][j]);
    }
}

Notes:

  • , matrix[i][j] 0, 1. newmatrix[i][j] , .
  • for. do/while, , , , , .
+2

Source: https://habr.com/ru/post/1694958/


All Articles