I am working on a project where i have a matrix and i am doing some processing with the matrix by moving a character up, down, left and right. I have stored the moves in a char array. Now i want to print only the last 10 states of the matrix after the other moves are performed on it. But i don't want the other moves to print, just the last 10 states of a matrix.
So i am looping over the moves like this:
int i = 0;
for (; i < strlen(movesArray); i++ ) {
operation = movesArray[i]; // move
switch (operation) {
case 'v': // process the moves
}
Then while still inside the for
loop i do something like this:
#ifdef NDEBUG // but this printing every state from 1 to 99
if( i >= strlen(movesArray) - 10)
printf("%s %d:\n", "Move", i );
print(matrix);
#endif
However it is printing all the moves where as i want just the last 10 so far instance. Can anyone please guide me in the right direction? I have been at it for a couple of hours now.
If i have 99 moves, then it should perform the all the moves but it should just print the last 10 states of the matrix and that should include the moves that have been done the matrix.
i compile my program with -D
flag.
You have no curly braces on your if
. Think of it this way:
if( i >= strlen(movesArray) - 10)
printf("%s %d:\n", "Move", i ); // No curly braces means only the statement after the
// if is part of the conditional, in this case
// thats this one..
print(matrix);
So you should do this:
if( i >= strlen(movesArray) - 10) {
printf("%s %d:\n", "Move", i );
print(matrix);
} // Everything between the curly braces is part of this conditional.
Plenty of industry subscribes to the coding standard that you should always use curly braces regardless of whether you have multiple lines or just one. As shown above it only adds one line but can prevent bad bugs like this one.
Note: This is my own opinion, no requirement is implied.