Printing Pattern Using Loops

Sort by

recency

|

1034 Discussions

|

  • + 0 comments

    Hmmm, IMHO the tests suit machinery use to validate output (in the assert stage), could be improved using patterns, to discard whites character, specially if we use format string to justify the out put it fails.

    printf ("%5d", np);

    #include <stdio.h>
    
    /**
     * Given @param n as natural number it prints the following pattern:
     *
     *   |---------  2n - 1 ----------|         ^
     *   
     *    n    n    n   ...     n    n          |
     *    n   n-1  n-1  ...   n-1    n          |
     *    n  n-1  n-2 ...  n-2  n-1  n          |
     *    ...			          
     *    n n-1 n-2 .. 1 .. n-2 n-1  n        2n - 1
     *    ...			          
     *    n  n-1  n-2 ...  n-2  n-1  n          |
     *    ...                                   |
     *    n  n-1  n-1  ...   n-1     n          |
     *    n    n    n   ...     n    n          v
     * 
     * Please note that dimession of "square" is giving by the expression: `2n - 1'.
     *
     * I used a Cartesian coordinates to know which number will be located at which
     * position, incrementing or decrement in function of the coordinate (r,c).
     */
    void
    print_pattern (int n)
    {
      int r, c;
      
      if (n <= 0)
        {
          fprintf (stderr, "%s: error: invalid parameter\n", __func__);
          return;
        }
    
      for (r = 0; r < 2 * n - 1; ++r)
        {
          int np = n;
    
          for (c = 0; c < 2 * n - 1; ++c)
            {
              printf ("%5d", np);
    	  
    	  if (c < r)
                np--;
    	  if (c + r >= 2 * n - 2)
                np++;
            }
    
          puts ("");
        }
    }
    
    int
    main ()
    {
      int n;
    
      if (scanf ("%d", &n) != 1)
        {
          fprintf (stderr, "error: invalid input\n");
          return 1;
        }
    
      print_pattern (n);
    
      return 0;
    }
    
  • + 0 comments

    include

    include

    include

    include

    int main() {

    int n;
    scanf("%d", &n);
    // Complete the code to print the pattern.
    

    int min = 0; for (int i = 1; i <= 2 * n - 1; i++) { for (int j = 1; j <= 2 * n - 1; j++) { int a = i; if (i > n) a = 2 * n - i; int b = j; if (b > n) b = 2 * n - j; if (a < b) min = a; else min = b;

            printf("%d ", n + 1 - min);
        }
        printf("\n");
    }
    return 0;
    

    }

  • + 0 comments

    There were definitely better ways to accomplish this...

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        scanf("%d", &n);
        // Complete the code to print the pattern.
        int line_length = n*2-1;
        int line_counter = 0;
        int reverse_point = line_length / 2;
        int reverse_flag = 0;
        int iteration_counter = 0;
    
        while (iteration_counter != line_length){
            for (int j = 0; j < line_counter; j++) {
                printf("%d ", n-j);
            }
    
            for (int k = 0; k < line_length - line_counter * 2; k++) {
                printf("%d ", n-line_counter);
            }
    
            for (int l = 0; l < line_counter; l++) {
                printf("%d ", n-line_counter+l+1);
            }
    
            if (reverse_flag == 1) {
                line_counter--;
            } else {
                line_counter++;
            }
    
            if (line_counter >= reverse_point) {
                reverse_flag++;
            }
    
            iteration_counter++;
            printf("\n");
        }
        return 0;
    }
    
  • + 0 comments

    There were definitely better approaches but this got the job done...

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        scanf("%d", &n);
        // Complete the code to print the pattern.
        int line_length = n*2-1;
        int line_counter = 0;
        int reverse_point = line_length / 2;
        int reverse_flag = 0;
        int iteration_counter = 0;
    
        while (iteration_counter != line_length){
            for (int j = 0; j < line_counter; j++) {
                printf("%d ", n-j);
            }
    
            for (int k = 0; k < line_length - line_counter * 2; k++) {
                printf("%d ", n-line_counter);
            }
    
            for (int l = 0; l < line_counter; l++) {
                printf("%d ", n-line_counter+l+1);
            }
    
            if (reverse_flag == 1) {
                line_counter--;
            } else {
                line_counter++;
            }
    
            if (line_counter >= reverse_point) {
                reverse_flag++;
            }
    
            iteration_counter++;
            printf("\n");
        }
        return 0;
    }
    
    return 0;
    

    }`

  • + 0 comments

    Most solution I see starts for loops from 0 or 1. Go out of the box and start the for loop from negative. It's easier to understand.

    int main() 
    {
    
        int n;
        scanf("%d", &n);
        for (int i = -(n-1); i < n; i++){
            for (int j = -(n-1); j < n; j++){
                    printf("%d ", abs(j)+1 > abs(i)+1 ? abs(j)+1 : abs(i)+1);
            }
            printf ("\n");
        }
        return 0;
    }