0
votes

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");
        }
    }
}
  • rows means maximum rows
  • columns means maximum columns
  • rowCount for row iteration
  • columnCount for colum iteration
  • specialRow and sepcialColumn for the new position entered (from this position we have scan values in upward/left/backward directions) .
1
if(board[rowCount][columnCount] = 1 ) : = --> == ?BLUEPIXY
i did'nt get it. are you saying to change my statement in if condition as board[rowCount][columnCount] = 1 to board[rowCount][columnCount] = --> == 0Mann
lol, No he is not suggesting that. Quite rightly - he is suggesting you examine your use of = and perhaps consider ==Grantly
To trick yourself out of doing such mistakes again you might like to start using Yoda Conditions: en.wikipedia.org/wiki/Yoda_conditionsalk

1 Answers

1
votes

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");
    }