5
\$\begingroup\$

I wrote a code which prints the following pattern :

5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5

The code is as follows :

#include <stdio.h>

void main(){
   int n,i,k,b,a;
   printf("ENTER OUTER NUMBER:");
   scanf("%d",&n);
   for(i=0;i<2*n-1;i++){
     printf("%d",n);
   }
   printf("\n");
   int p=n;
   for(i=0;i<n-1;i++){
     int a=0;
     for(k=0;k<=i;k++){
        printf("%d",n-a);
        a++;
     }
     for(b=0;b>i;b++){
        printf(" ");
     } 
     for(k=1;k<=2*p-3;k++){
        printf("%d",n-i-1);
     }  
     p--;
     for(k=i;k>=0;k--){
        printf("%d",n-k);
     }
     printf("\n");
   }
    int q=1;
    for(i=0;i<n-2;i++){
     for(k=n;k>i+1;k--){
        printf("%d",k);
     }
     for(k=1;k<=2*q-1;k++){
        printf("%d",q+1);
     }
     q++;
     for(k=2+i;k<=n;k++){
        printf("%d",k);
     }
     printf("\n");
   }
   for(i=0;i<2*n-1;i++){
     printf("%d",n);
   }
   getchar();
}

I printed the first and last line independently.Then,I divided the pattern into two halves and considered some common triangular patterns as follows:

5                 5    2 3 4 5 
5 4             4 5      3 4 5
5 4 3         3 4 5        4 5
5 4 3 2     2 3 4 5          5 

The code ran well in the CODE BLOCKS IDE.But, I think it's too long and I complicated the coding.My query is:

  1. Can I shorten the code with the similar logic of breaking the pattern into parts or mine is ok ?

  2. Is there any alternative to this ?

\$\endgroup\$
3
\$\begingroup\$
  • Use a bit more spaces. It will make your code more readable.

  • Try to be coherent with your indents

  • You should sanitize input. What happen if user enter a number greater than 9 ? Or 0, 1, or 2? Or negative number? Or not a number? Never trust user, They all try to broke your program.

  • When you just print on char, prefer putchar() instead of printf().

Here's my solution, maybe not the most optimal, but clean and short. I compute the distance from the current coordinate to nearest side and then remove it to the base number.

#include <stdio.h>
int main(void) {
    int n = 5;
    int a;
    int b;
    const int m = 2*n-1;
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < m; ++j) {
            a = (i >= n) ? m - i - 1: i;
            b = (j >= n) ? m - j - 1 : j;
            putchar('0' + n - ((a < b) ? a : b));
        }     
        putchar ('\n');
    }
    return 0;
}

To improve this, you could split the inner loop to avoid the two conditional assignments. I let you trying to do it yourself.

\$\endgroup\$
1
\$\begingroup\$

The same pattern can be produced by a single instance of nested for loops, where outer loop variable is line and inner one - horizontal position.

I do not want to spoil your fun in finding the function at the heart of the loop (at least yet), which given two coordinates produces the value at those coordinates.

\$\endgroup\$
  • \$\begingroup\$ Did you speak about the method as mentioned in the abovw answer? \$\endgroup\$ – Nehal Samee Nov 14 '18 at 2:25
  • \$\begingroup\$ Yes. The method above is that. \$\endgroup\$ – Roman Susi Nov 14 '18 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.