Thread: post problem with print ABC pyramid

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Question post problem with print ABC pyramid

    hi
    I need some help please with the program.
    The program get a small letter not odd position of ABC.
    The program prints a pyramid of not odd characters.
    I have a problem with the shape of the pyramid.
    Thank you.
    For example: for the input 'g' print

    alignment center pyramid
    Code:
             a     
           ccc      
         eeeee     
        ggggggg

    but now my program print
    a
    ccc
    eeeee
    ggggggg

    I have a problem with the alignment of the pyramid. I think missing a for loop define the amount of space according to the character is get.
    The program can accept any character lowercase ABC not even position (a, c, e, g, i, k...) and print a pyramid untill last char.
    Thank you.
    Last edited by loop2012; 12-17-2011 at 01:37 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you have two variables...

    int numCharsToPrint;
    int numSpacesToPrint;

    At the end of the loop, you do
    numCharsToPrint += 2;
    numSpacesToPrint -= 1;
    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.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    Thanks for the response, this is my code.
    Can you help me complete the code? Thank you

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
        
        char lowercase;
        int i=97;
        int j=1;
    
    
        printf("Please enter a lowercase letter.\nfor example: 'g'\n");
        scanf("%c",&lowercase);
        printf("\n");
    
        if ( (lowercase>=97) && (lowercase<=122) && ((lowercase%2)>0) )
        {
        
    
            /* print a-g ++2   */
            for (i=97;i<=lowercase;)
            {
                printf("\t\t");
    
                for (j=97;j<=i;j++)
                {
    
                    printf("%c",i);
                    
                }
    
                printf("\n");
                i=i+2;
            }
    
    
    
        }
        else
        {
            printf("Invalid Input\n");
        }
    
          return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have the general logic for the program, but you can't use \t (tab) spacing for this, at all. It's all about printing single spaces, and changing the number of single spaces you print, on every row.

    I like the nested loop technique:

    Code:
    for(etc) {  //handles the row logic
       for(etc) { //handles the column logic
       
       }
    }
    Work with that, and re-read Salem's post, and you'll be right on the right path.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    this was our Exam question , 20 points .
    firstly you should, do this.
    Code:
    char a;
    enter a height of pyramid=n,
    after do a for(i=0;i<n;i++){
    a='A'
    
    printf spaces = for(i4=;i4<(n-i);i4++) printf(" ")  times
    
    
    put a for loop again for(i2=0;i2< i+1;i2++) printf("%c",a+i2);  //you printed the half vertical pyramid    
    
    after, put a foor loop again and for(i3=0;i3<i;i++) printf("%c",a-1-i);
    
    printf("\n");
    
    }
    firstly if n=3
    spaces will be put ,after the letters and ,after other half of pyramid(the letters after ".", this is my algoritm,Not assume that there is no "." in the below shape.
       A
     AB.A
    ABC.BA

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Star Pyramid
    By subhadeepgayen in forum C Programming
    Replies: 2
    Last Post: 08-10-2010, 05:16 AM
  2. pyramid char..
    By Kinshara in forum C Programming
    Replies: 3
    Last Post: 11-05-2009, 12:23 PM
  3. Number pyramid
    By Tehy in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 09:01 AM
  4. stupid pyramid
    By nikki19 in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2002, 11:33 PM