V
V
Vasily Vasilyev2017-10-15 15:26:34
C++ / C#
Vasily Vasilyev, 2017-10-15 15:26:34

How to replace elements of a two-dimensional array by their indices (vector)?

In C ++ a couple of days (came from Java), I ran into a problem:
There is a two-dimensional matrix array in which the specified elements must be replaced according to a given algorithm. I tried to do what first came to mind - at the output, the method returns matrix without changes. What am I doing wrong?

vector<vector<int> > waveRun (vector<vector<int> > matrix, int numberOfWave){
        for(int i = 0; i < matrix.size(); i++){
            for (int j = 0; j < matrix[i].size(); j++) {
                if(matrix[i][j] == numberOfWave){
                    if(matrix[i-1][j] == 0) matrix[i-1][j] = numberOfWave+1;
                    if(matrix[i+1][j] == 0) matrix[i+1][j] = numberOfWave+1;
                    if(matrix[i][j-1] == 0) matrix[i][j-1] = numberOfWave+1;
                    if(matrix[i][j+1] == 0) matrix[i][j+1] = numberOfWave+1;
                }

            }
        }

    return matrix;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question