StackUser
StackUser

Reputation: 1612

why the first index of the column of a matrix is negative?

why in this simple part of code to modify the point in the lower left of the bidimensional array have I to put in the coordinate m[37][-40]?

#include <stdio.h>
#include <stdlib.h>
#define MAX 40

int main()
{
    int i, j;
    char m[MAX][MAX];

    for(i=0; i<MAX; i++){
        for(j=0; j<MAX; j++)
            m[i][j]=' ';
    }

    m[37][-40]='X';

    for(i=0; i<MAX; i++){
        for(j=0; j<MAX; j++)
            printf("%c", m[i][j]);
    }
    return 0;
}

Souldn't it be m[37][0]? Becouse the row is 37 and the column is 0...

Upvotes: 2

Views: 50

Answers (1)

Jashaszun
Jashaszun

Reputation: 9270

It is probably because you don't have line breaks in your printing loops, and this is exacerbated because you print spaces for the most part (which are, of course, invisible). If you add a printf("\n"); after your inner printing loop (so the following code), and replace m[37][-40]='X'; with m[37][0]='X';, then it should work (it did when I ran it):

for(i=0; i<MAX; i++){
    for(j=0; j<MAX; j++)
        printf("%c", m[i][j]);
    printf("\n");
}

I also replaced spaces with periods ('.') to make it more obvious.

Upvotes: 1

Related Questions