Home > Back-end >  C consecutive one line for loops (not nested!) don't iterate
C consecutive one line for loops (not nested!) don't iterate

Time:09-28

Here's 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;
        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 mistery to me. Of course, I'm sorry if this is a duplicate. I just couldn't find anything about this specifically.

CodePudding user response:

There are multiple mistakes in your code:

  • you should undo the last increment/decrement after each of the inner loops. As coded, you store h beyond the end of the array p.
  • n should be decremented only by 1
  • since n is modified, the final loops have no effect.

Here is a modified version, keeping your corny style:

#include <stdio.h>
int main(){
    int n,sqrs,start=0,end,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;
    end = n;
    for(int t=0; t<sqrs; t  ){
        for( ; x<end; x  ) p[y][x] = h; x--;
        for( ; y<end; 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  ; end--;
    }
    for(int i=0; i<n; i  ){
        for(int j=0; j<n; j  ){
            printf("%i",p[i][j]);
        } printf("\n");
    }
}

This code can be simplified as:

#include <stdio.h>
int main(){
    int n; scanf("%i",&n);
    int p[n][n];
    int h=1,start=0,stop=n-1;
    for(int t=0; t<n; t =2,h  ,start  ,stop--){
        int x=start,y=start; p[y][x] = h;
        while(x<stop) p[y][x  ] = h;
        while(y<stop) p[y  ][x] = h;
        while(x>start) p[y][x--] = h;
        while(y>start) p[y--][x] = h;
    }
    for(int i=0; i<n; i  ){
        for(int j=0; j<n; j  ) printf("%i",p[i][j]);
        printf("\n");
    }
}

And here is a more classic version:

#include <stdio.h>

int main() {
    int n;
    if (scanf("%i", &n) != 1 || n <= 0)
        return 1;
    int p[n][n];
    int h = 1, start = 0, stop = n - 1;
    for (int t = 0; t < n; t  = 2, h  , start  , stop--) {
        int i = start, j = start;
        if (start == stop)
            p[i][j] = h;
        for (; j < stop; j  )
            p[i][j] = h;
        for (; i < stop; i  )
            p[i][j] = h;
        for (; j > start; j--)
            p[i][j] = h;
        for (; i > start; i--)
            p[i][j] = h;
    }
    for (int i = 0; i < n; i  ) {
        for (int j = 0; j < n; j  )
            printf("%i", p[i][j]);
        printf("\n");
    }
    return 0;
}

CodePudding user response:

It appears that you are trying to make a pattern of concentric boxes in a matrix. Your logic is wrong. Following is an example using the Ada programming language. Translating that into C should be easy.

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is
   type matrix is array (Positive range <>, Positive range <>) of Positive;
   Side : Positive;
begin
   Put ("Enter the number of elements on a side of the array: ");
   Get (Side);
   declare
      Box    : matrix (1 .. Side, 1 .. Side);
      Center : Positive;
      Start  : Positive := 1;
      Stop   : Positive := Side;
      Value  : Positive := 1;
   begin
      Center := (if Side mod 2 = 0 then side / 2 else side / 2   1);
      for turn in 1 .. Center loop
         -- sides
         for I in Start .. Stop loop
            Box (Start, I) := Value;
            Box (I, Start) := Value;
            Box (Stop, I)  := Value;
            Box (I, Stop)  := Value;
         end loop;
         -- adjust for inner box values
         Start := Start   1;
         Stop  := Stop - 1;
         Value := Value   1;
      end loop;

      for I in Box'Range (1) loop
         for J in Box'Range (2) loop
            Put (Item => Box (I, J), Width => 1);
         end loop;
         New_Line;
      end loop;
   end;
end Main;

The output of a sample execution is:

Enter the number of elements on a side of the array: 7
1111111
1222221
1233321
1234321
1233321
1222221
1111111

CodePudding user response:

It's not clear from either the OP code or the description what it is you are trying to achieve.

Based on another recent SO question, perhaps this is what you are seeking. This takes advantage of the symmetry of the (I believe) desired output. Four array elements are assigned in each iteration until the centre of the pattern is reached.

int main() {
    const uint8_t n = 9;
    uint8_t mat[n][n];

    for( int r = 0; r < (n 1)/2; r   )
        for( int c = 0; c < (n 1)/2; c   )
            mat[r    ][c    ] =
            mat[n-1-r][c    ] =
            mat[r    ][n-1-c] =
            mat[n-1-r][n-1-c] =
                1   (c<r?c:r);

    for( int i = 0; i < n; i   ) {
        for( int j = 0; j < n; j   )
            printf("%d ", mat[i][j]);
        puts( "" );
    }
    return 0;
}
1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 1
1 2 3 3 3 3 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 3 3 3 3 2 1
1 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1

Perhaps the "not nested" is the criteria?

int main() {
    const uint8_t n = 9, h = ((n 1)/2); // 'h'alf way
    uint8_t mat[n][n];

    for( int x = 0; x < h*h; x   ) {
        int r0 = x / h, r1 = n-1-r0; // top & bottom rows of this square
        int c0 = x % h, c1 = n-1-c0; // left & right cols of this square
        uint8_t v = 1   ( c0 < r0 ? c0 : r0 );

        mat[r0][c0] = mat[r0][c1] =
        mat[r1][c0] = mat[r1][c1] = v;
    }
    /* same nested output loops */
    return 0;
}
  • Related