Manuel Stoilov Manuel Stoilov - 1 year ago 46
C Question

Why are the wrong array values being printed?

For some reason, the nested

loop I have created at the bottom seems to be printing out the wrong first value (Gives me 3, when it should be 8). Yet, when I simply do
(at the bottom), I am given the right value. Not really sure what's wrong with my code.

#include <stdio.h>

int main(void)
int d;
printf("Please input dimensions: (between 3 and 9, inclusive): \n");
scanf("%i", &d);

int array[d][d];

int k = 1;

for (int i = 0; i < d; i++)
for (int j = 0; j < d; j++)
array[i][j] = (d * d) - k; //d^2 doesn't work to square a function

for (int z = 0; z < d; z++)
for (int y = 0; y < d; y++)
printf("%i\n", array[z][y]);

printf("%i\n", array[0][0]);
printf("%i\n", array[0][1]);

Edit: Sorry guys, the top value that was being printed was my own input. I was simply thinking it was the first value being printed.

Answer Source

Are you sure you aren't looking at your own input? I cut and paste and see:

Please input dimensions: (between 3 and 9, inclusive): 

The 3 is actually what I typed and is echoed.