I am trying to assign values to each index in my 3x3 dimensional array. I initialized all the values to be 1 at the beginning, then set index[0][2] to be 2. however, somehow index[1][0] also attached to the value 2. then i tried to set [1][2] to 2, and [2][0] also set to value 2. I am not sure what is happening here?
1 | 2 | 1 | ----> 1 | 1 | 2 |
1 | 1 | 1 | ----> 1 | 1 | 1 |
2 | 1 | 1 | ----> 1 | 2 | 1 |
void magicSquare (int param){
//param = 3
int volume = param - 1;
int squareArray[volume][volume];
int c = 0;
int d = 0;
for (int i = 0; i < param*param; i++) {
squareArray[c][d] = 1;
c +=1;
if (c == param) {
d +=1;
c = 0;
}
}
squareArray[0][2]= 2;
c = 0;
d = 0;
printf (" %d | ",squareArray[c][d]);
for (int i = 1; i < param*param; i++) {
c +=1;
if (c == param) {
d +=1;
c = 0;
printf ("\n %d | ",squareArray[c][d]);
}
else printf (" %d | ",squareArray[c][d]);
}
int squareArray[volume][volume];(Variable Length Array) is not standard C++ and supported only by extension to the language. Usestd::vectorinstead.