Thread: Help with pyramid shapey program?

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    11

    Cool Help with pyramid shapey program?

    So I have been given this nice but sorta tough for me(at least for now) program. Main objective is to print the follow pattern till the rows user enters->

    *****
    **1**
    *222*

    etc etc.

    My code->
    Code:
    #include<stdio.h>#include<conio.h>
    void main()
    {
    int i,j,v=1,n,m,rows=0,l;
    clrscr();
    printf("Enter number of rows.\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
      if(i==0)
      {
       for(;i<n*2-1;i++)
       {
        printf("*");
       }
       printf("\n");
       rows+=1;
      }
      for(l=n;l>0;l--)
      {
       for(j=1;j<n;j++)
       {
         printf("*");
       }
       for(v=1;v<=rows;v++)
       {
         printf("%d",rows);  }
       for(j=1;j<n;j++)
       {
         printf("*");
       }
       printf("\n");
       rows+=1;
      }
    }
    getch();
    }
    I think I have gotten quite close but it is confusing for now. Might take a break. Been working on programs for hours now.

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    11

    Red face

    Okay so after twisting some stuff around, i ended up achieving what I had aimed...just with a slight different problem. The output puts much more lines than the user inputs. Perhaps, Someone can help with what wrong went with the logic.

    Code:
    #include<stdio.h>#include<conio.h>
    void main()
    {
    int p,o,i,j,v=1,n,m,rows=0,l;
    clrscr();
    printf("Enter number of rows.\n");
    scanf("%d",&n);
    m=n;
    o=n;
    for(i=1;i<n;i++)
    {
      if(n==1)
      {
       for(i=0;i<=m*2-1;i++)
       {
        printf("*");
       }
       printf("\n");
       rows+=1;
      }
      for(l=n;l>=1;l--)
      {
       for(j=n;j>=rows;j--)
       {
         printf("*");
       }
       for(v=1;v<=rows;v++)
       {
         printf("%d",rows);
       }
       for(p=n;p>=rows;p--)
       {
         printf("*");
       }
       printf("\n");
       rows+=1;
      }
    }
    getch();
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Nothing I said last time made a blind bit of difference.
    Help me with reverse pyramid program?

    If you're not listening, there's no point talking.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    11
    Quote Originally Posted by Salem View Post
    Nothing I said last time made a blind bit of difference.
    Help me with reverse pyramid program?

    If you're not listening, there's no point talking.
    Well, If you mean writing variables in more readable forms - Yeah, Sorry about that. It is kinda like an old bad habit. I should work on it for sure.

    If you mean about int main then since we aren't passing any value shouldn't "void main" and "int main(void)" be same thing? I mean, Theoretically.

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    11
    Managed to find the problem. It was quite very very small. One misplacing of letter. Ironic.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Orcus View Post
    If you mean about int main then since we aren't passing any value shouldn't "void main" and "int main(void)" be same thing? I mean, Theoretically.
    Nothing "Theoretical" about it!

    There are three ways to define main().

    int main(void) (No command line arguments)

    int man(int argc, char *argv[])

    int main(int argc, char **argv)

    The last two are technically the same.

    The "int" before main is the data return type. main returns int, ONLY int, and NOTHING BUT an int!!! The startup code is hard coded to expect an integer to be returned from main().

    The void in the parentheses after main ignores any command line arguments to the program. That use of void has NOTHING to do with the data return type.

    This should have been taught in the first class of a course, or the first couple of chapters of a good book on the C Programming Language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with reverse pyramid program?
    By Orcus in forum C Programming
    Replies: 1
    Last Post: 06-18-2017, 01:11 PM
  2. C program to draw a pyramid
    By nyck66 in forum C Programming
    Replies: 3
    Last Post: 05-20-2017, 04:23 AM
  3. how make this all pyramid in c++ 4.5
    By shah18 in forum C Programming
    Replies: 5
    Last Post: 10-04-2010, 11:34 AM

Tags for this Thread