简体   繁体   中英

Printing a pattern in C using loops

The pattern to be printed(for user input n) is:

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

My attempt:

#include <stdio.h>

int main() 
{
    int i,j,k,l,p,n,tc,i1,j1,k1;
    n=4;

    for(i=1;i<=n*2-1;i++)
    {
        p=n;
        if(i<=n)
        {
        for(j=1;j<=i-1;j++)
        {
            printf("%d ",p);
            p--;
        }
        for(k=j;k<=n*2-j;k++)
            printf("%d ",p);
        for(l=k;l<=n*2-1;l++)
        {
            p++;
            printf("%d ",p);
        }
        }
        else
        {
            p=n;
            for(i1=1;i1<=n*2-i-1;i1++)
            {
                printf("%d ",p);
                p-=1;
            }
            for(j1=i1;j1<=n*2-i1;j1++)
                printf("%d ",p);
            for(k1=j1;k1<=n*2-1;k++)
            {
                p+=1;
                printf("%d ",p);
            }
        }
        printf("\n");
    }
   return 0;
} //main

I am using a gcc 6.3 compiler and it is giving

Output File Size Exceeded

Where am I going wrong?I can't figure it out. I have tried this code in gcc 4.x and I was getting the correct output.

In your last loop, you increment the wrong variable.

for(k1=j1;k1<=n*2-1;k++){
   p+=1;
   printf("%d ",p);
}

change k to k1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM