Final Edit
When debugging this code, I would see the first loop line execute n
times, but the following lines would execute only once. Only later I would notice that the expected value changes, though, were in fact being made: it was just the loop line was being executed at once, as if the loop went through all iterations in one single step. From the second loop on, Pressing F10 (I use VSCode) would skip straight to the next loop, instead of skipping to the next iteration. That's how the compiler behaved (MinGW64).
Now, formatting the loops like this:
for( ; x<n; x++) {
p[y][x] = h;
} x--;
for( ; y<n; y++) {
p[y][x] = h;
} y--;
etc...
did cause each iteration to be read separately: now from the second loop on, pressing F10 would just jump to the next iteration, not straight to the next loop.
Conclusion: both styles work the same, but debug differently.
Anyway, here's a version that's still pretty ugly, but now works as expected:
#include <stdio.h>
int main(){
int n,size,sqrs,start=0,h,y,x; scanf("%i",&n);
int p[n][n];
size = n;
if(n%2==0) sqrs = n/2;
else sqrs = n/2+1;
h = 1;
y = start;
x = start;
for(int t=0; t<sqrs; t++){
for( ; x<n; x++) p[y][x] = h; x--;
for( ; y<n; y++) p[y][x] = h; y--;
for( ; x>=start; x--) p[y][x] = h; x++;
for( ; y>=start; y--) p[y][x] = h; y++;
y++; x++; h++; start++; n--;
}
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
printf("%i ",p[i][j]);
} printf("\n");
}
}
Edit:
I'm trying to build a matrix where the numbers grow towards the center, i.e. a "pyramid". As of yet, I haven't had any problem with single-statemente one-liner for loops in C. Now, placing them consecutively is causing them, except for the first loop, to be skipped. As I see it, we have a `for` loop, then its single statement, and then a semicolon, which should (at least, I expected it to) terminate the line, then move on to the next piece of code, which just happens to be another `for` loop. The problem is the first `for` loop runs as expected, however, the following ones are just skipped, as they were at their ending condition.I've stressed "not nested" because:
- while searching for this issue, all I had found were about nesting loops and if statements regarding using or not braces;
- each for loop/statement ends with a semicolon.
I've tried bracing the single statements, but it did not solve the problem.
What DID solve the problem though was adding braces AND newlines, like this:
for( ; y<n; y++) {
p[y][x] = h;
}
But still, it bothers me to be clueless of why these were required in C, in this case. For example, while debugging I had noticed the second loop is skipped because the value of y
changes from 0
to 4
. This alone is already a mystery to me.
By the way, my apologies for being vague!
Original post:
Here is my code:
#include <stdio.h>
int main(){
int n,sqrs,start=0,h,y,x; scanf("%i",&n);
int p[n][n];
if(n%2==0) sqrs = n/2;
else sqrs = n/2+1;
h = 1;
y = start;
x = start;
for(int t=0; t<sqrs; t++){
for( ; x<n; x++) p[y][x] = h; // THESE LOOPS
for( ; y<n; y++) p[y][x] = h; // here, y changes from 0 to 4
for( ; x>=start; x--) p[y][x] = h;
for( ; y>=start; y--) p[y][x] = h;
y++; x++; h++; start++; n-=2;
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%i",p[i][j]);
} printf("\n");
}
}
As you can see, these loops aren't nested. They would nest though if there weren't any statements in between them, but there are.
So, the first one iterates normally but all of the following ones don't. They just skip.
I did manage to get around this, but curiously only when I added both braces and newlines, i.e. when all loops (except the first one, which doesn't seem to require it) looked like this:
for( ; y<n; y++) {
p[y][x] = h;
}
This remains a mystery to me. Of course, I'm sorry if this is a duplicate. I just couldn't find anything about this specifically.
EDIT: As some of you have thankfully noticed, yes, I'm trying to print a "pyramid" matrix. The reason I didn't mention it is because to me the problem was about syntax only, being the not so relevant.