468,491 Members | 1,918 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

creating a pyramid of letters using c

I just strating taking C class and still learning. I couldn't come up with the output like the one below:
A

ABA

ABCBA

ABCDCDA

ABCDEDCBA

Here is what I tried:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #define ROW 6
  3. #define RSIZE 6
  4. #define ASCEND 4
  5. #define SPACE 6
  6. #define DESCEND 4
  7. int main()
  8. {
  9.     char ch,letter, column;
  10.     int row;
  11.  
  12.     printf("Please enter an UPPERCASE letter:");
  13.     scanf("%c",&ch);
  14.  
  15.     for (row = 1 ; row < ROW ; row ++)
  16.     {
  17.         for (column = 1; column < SPACE - row; column++)
  18.         printf(" ");
  19.          {
  20.              for (letter = row   ; letter >= 1 ; letter --)
  21.                 printf("%c", ch);
  22.                 printf("\n");   
  23.          }
  24.     }
  25.    fflush(stdin);
  26.       getchar();
  27.       return 0;  
  28. }// main
  29.  
Thanks for you help
shulapi
Dec 30 '07 #1
8 8264
numberwhun
3,503 Expert Mod 2GB
I just strating taking C class and still learning. I couldn't come up with the output like the one below:
A

ABA

ABCBA

ABCDCDA

ABCDEDCBA

Here is what I tried:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #define ROW 6
  3. #define RSIZE 6
  4. #define ASCEND 4
  5. #define SPACE 6
  6. #define DESCEND 4
  7. int main()
  8. {
  9.     char ch,letter, column;
  10.     int row;
  11.  
  12.     printf("Please enter an UPPERCASE letter:");
  13.     scanf("%c",&ch);
  14.  
  15.     for (row = 1 ; row < ROW ; row ++)
  16.     {
  17.         for (column = 1; column < SPACE - row; column++)
  18.         printf(" ");
  19.          {
  20.              for (letter = row   ; letter >= 1 ; letter --)
  21.                 printf("%c", ch);
  22.                 printf("\n");   
  23.          }
  24.     }
  25.    fflush(stdin);
  26.       getchar();
  27.       return 0;  
  28. }// main
  29.  
Thanks for you help
shulapi
First, Welcome to TSDN!!! We hope you learn a lot during your stay.

Second, the next time you post, please be sure and choose a forum appropriate to the topic of your thread. The "Member Introductions" forum is not the place for a C programming question when there is a C programming forum.

I will move this thread over there now.

Regards and Happy Holidays!

Jeff
Dec 30 '07 #2
sicarie
4,677 Expert Mod 4TB
Okay, so you have the first half of the program down. Why are you asking for just one letter? I'd say it looks more like levels - you put in the number of levels, and then it prints out the appropriate letters per level. For instance, the the first level prints out one letter, the second level prints out three letters, and the third level prints out five, fourth prints seven...

Are you seeing a pattern? You'd have your loop as it is now, but it would increment through the alphabet until it got halfway through, and then it would decrement back to its original spot.
Dec 30 '07 #3
First, Welcome to TSDN!!! We hope you learn a lot during your stay.

Second, the next time you post, please be sure and choose a forum appropriate to the topic of your thread. The "Member Introductions" forum is not the place for a C programming question when there is a C programming forum.

I will move this thread over there now.

Regards and Happy Holidays!

Jeff
Thanks Jeff. I will make sure that won't happen again. Just new to the forum.
Dec 31 '07 #4
Okay, so you have the first half of the program down. Why are you asking for just one letter? I'd say it looks more like levels - you put in the number of levels, and then it prints out the appropriate letters per level. For instance, the the first level prints out one letter, the second level prints out three letters, and the third level prints out five, fourth prints seven...

Are you seeing a pattern? You'd have your loop as it is now, but it would increment through the alphabet until it got halfway through, and then it would decrement back to its original spot.
Thanks for your help sicarie. Can you please clarify it a bit more for me. If you run the program the way it is, it only outputs the left half of the pyramid without changing the letters. For instance if my input is the letter E, the output looks like the following: My problem is gettting the full pyramid with ascending descending letters. I will appreciate for your help.
E
E E
EEE
EEEE
EEEEE
Dec 31 '07 #5
Andr3w
42
Well,
If you have the algo that generates the half-pyramid already it's pretty easy to make a "mirror image" of it and output first the mirror image and then the actual pyramid that you have there. It's also easy do that (output both pyramids at the same time) with a little more efford in these for loops.

Also and advice, in order to get rid of the annoying uppercase/lowercase letter issue (in the cases where you don't want the input to be case-sensive as it's this one) you can use the following in order to convert the input value (whether it is lowercase or uppercase) to uppercase or lowercase depending on your needs

Expand|Select|Wrap|Line Numbers
  1. char pKey;
  2.  
  3. pKey = getch();
  4.  
  5. // your check or whatever
  6. if ( toupper(pKey) == 'K' /* can be anything else */ )
  7. }
  8.  // do stuff
  9. }
  10.  
also if you have while loops or anything else that repeats :p just to avoid continued calls to that function, if you don't change the value before the check do the following:

Expand|Select|Wrap|Line Numbers
  1. char pKey;
  2.  
  3. pKey = getch();
  4.  
  5. pKey = toupper(pKey) ;
  6. // your check or whatever
  7. while ( pKey == 'K' /* can be anything else */ )
  8. }
  9.  // do stuff
  10. }
  11.  
hope this helped!
Jan 1 '08 #6
sicarie
4,677 Expert Mod 4TB
Well,
If you have the algo that generates the half-pyramid already it's pretty easy to make a "mirror image" of it and output first the mirror image and then the actual pyramid that you have there. It's also easy do that (output both pyramids at the same time) with a little more efford in these for loops.

Also and advice, in order to get rid of the annoying uppercase/lowercase letter issue (in the cases where you don't want the input to be case-sensive as it's this one) you can use the following in order to convert the input value (whether it is lowercase or uppercase) to uppercase or lowercase depending on your needs

Expand|Select|Wrap|Line Numbers
  1. char pKey;
  2.  
  3. pKey = getch();
  4.  
  5. // your check or whatever
  6. if ( toupper(pKey) == 'K' /* can be anything else */ )
  7. }
  8.  // do stuff
  9. }
  10.  
also if you have while loops or anything else that repeats :p just to avoid continued calls to that function, if you don't change the value before the check do the following:

Expand|Select|Wrap|Line Numbers
  1. char pKey;
  2.  
  3. pKey = getch();
  4.  
  5. pKey = toupper(pKey) ;
  6. // your check or whatever
  7. while ( pKey == 'K' /* can be anything else */ )
  8. }
  9.  // do stuff
  10. }
  11.  
hope this helped!

Good advice, except I would never use getch() as it is non-standard and will break stuff. Stick with standard functions.
Jan 1 '08 #7
Andr3w
42
Well I said what I said because getch() is simple and convienient ;). Sure, you could also use scanf but oh, well it's up to you ;).
Jan 1 '08 #8
Well I said what I said because getch() is simple and convienient ;). Sure, you could also use scanf but oh, well it's up to you ;).
much appreciated Andr3w and sicarie
Jan 2 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Micheal Artindale | last post: by
reply views Thread by Andrew Westgarth | last post: by
8 posts views Thread by virtualadepts | last post: by
1 post views Thread by rashee | last post: by
2 posts views Thread by Gn02 | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.