473,394 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 8663
numberwhun
3,509 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

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

Similar topics

7
by: Micheal Artindale | last post by:
I am looking at creating list of letter combinations. letters a-h 6 letters per combination letter can repeat its self in the combination, but not next to its self and, a series of letter can...
0
by: Andrew Westgarth | last post by:
Hi all, i'm struggling with a page idea I have. I need to write a page with an A to Z list of available schools in the area. I only want to display the letters in the A to Z which have school...
5
by: Kosmos | last post by:
Hey :) hopefully someone can help me with this...I decided to take on the task of programming an access database for my legal co-op/internship...I'm studying law and music production on the...
8
by: virtualadepts | last post by:
I spent quite a bit of work trying to get this to format on google groups, so let me know if it needs explination. Basicly the tree sorts randomly, and can start from any number on the pyramid. ...
4
by: i m gr8 | last post by:
Hello, I need to make a pyramid 20 lines long showing Xs like this: X XX XXX XXXX and so on . I have made this code so far:
5
by: Kahlia | last post by:
Hi. I am trying to print a pyramid of asterisks using recursion so that the recursive function takes in an integer and prints that number of lines of asterisks and then reverses, eg: * ...
1
by: rashee | last post by:
hello everybody, i want to print the pyramid of numbers in the following manner. 1 212 32123 4321234 and so on depending on the no of rows. plz help me out.
29
n8kindt
by: n8kindt | last post by:
hey guys, i'm using access 2007. i'm not happy with my solution to the following problem so i'm hoping one of you guys has a better idea. my solution would require use of multiple valued fields...
1
by: process | last post by:
http://projecteuler.net/index.php?section=problems&id=18 def recur(tree, pos): if not tree: return else: return ] + recur(tree, pos)] + \ ] + recur(tree, pos+1)]
2
by: Gn02 | last post by:
Hi im noob in JAVA could someone explain to me how to make this program for loop.. asterisk pyramid sideward .. plss answer me i want to learn it
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.