-
1 Attachment(s)
Formatting Arrays
I'm practicing printing some arrays and I am having trouble figuring out how to properly print one out. I have attached the assignment I am working on. I have solutions to the problems, but when I try to check my work and print out the arrays I can't get the formatting right.
Here is what I have for 2)
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i, square[ 5 ] [ 5 ] = { 0 };
for ( i = 0; i < 5; i++) {
for ( i = 0; i < 5; i++) {
square[ i ] [ i ] = ( i + 1 ) * ( i + 1 );
printf("%d", square[ i ] [ i ] );
}
printf("\n");
}
system("pause");
return 0;
}
The output comes out in 1 single line instead of as a square. I thought the printf("\n"); inside the rows loop would fix the issue, but I think the issue might have something to do with the square array not being completely populated in the calculation sequence. I thought that the values not entered in that step would automatically just be zero or empty.
Please help/offer input. Thanks
-
Write out on paper a 2d array and step through the process of your nested for loop to see exactly where you are going wrong. Hint: You need to index your rows and columns with separate variables.
-
whoops nvm
okay nvm i got it i just didnt recognize it at first because all the zeroes made it look ugly