Mann
Mann

Reputation: 606

how to move position of an array element only in one direction

Suppose I have a 4*4 matrix and I prompt to enter some position and that position is 4,1

(upward movement)now I want to iterate through positions (3,1) - (2,1) - (1,1) , check those values and sometimes I must change those values and finally print a new matrix with updates values

similarly how to iterate through (4,3) , (3,3) (2,3) , (1,3) if some one enters the position as 4,4 (left movement)

I have tried , this so far ..

for(rowCount = 0; rowCount < rows; rowCount++) {
    for(columnCount = 0; columnCount < columns; columnCount++){
        if(rowCount == specialRow && columnCount == specialColumn)

        {
            if(board[rowCount][columnCount] = 1 )
            {
                printf("%d \t",board[rowCount][columnCount]);
                board[rowCount][columnCount] = 0 ;

            }
        }

        for(rowCount = 0; rowCount < rows; rowCount++) {
            for(columnCount = 0; columnCount < columns; columnCount++)
                printf("%d \t",board[rowCount][columnCount]);

            printf("\n");
        }
    }
}

Upvotes: 0

Views: 338

Answers (1)

Gopi
Gopi

Reputation: 19864

Updating your array first based on some condition let's say the input is 4 4 so you should be doing

for(rowCount = 0; rowCount < rows; rowCount++) {

    for(columnCount = 0; columnCount < columns; columnCount++){
        if(columnCount == (specialColumn-1))

        {
            if(board[rowCount][columnCount] == 1 )
            {
                printf("%d \t",board[rowCount][columnCount]);
                board[rowCount][columnCount] = 0 ;

            }
        }
}
}

Now assuming the required modifications are made to your matrix print them out seperately

   for(rowCount = 0; rowCount < rows; rowCount++) {
   for(columnCount = 0; columnCount < columns; columnCount++)
     printf("%d \t",board[rowCount][columnCount]);

     printf("\n");
    }

Upvotes: 1

Related Questions