“Have a program request the user to enter an uppercase letter. Use nested loops to produce a pyramid pattern like this:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
The pattern should extend to the character entered. For example, the preceding pattern would result from an input value of E.”
So far I’ve been doing this for a good few hours and I’m getting the ‘pyramid’ to format properly for the letters when iterating forwards through the alphabet with:
#include <stdio.h>
int main(void)
{
char ch = 0;
char ch2 = 0;
int rows = 0;
printf("Enter a character: ");
scanf("%c", &ch);
rows = ch - 64;
while(rows > 0)
{
int spaces;
for(spaces = rows-1; spaces > 0; spaces--)
{
printf(" ");
}
ch2 = 65;
while(ch2 < (ch-(rows-2)))
{
printf("%c", ch2);
ch2++;
}
printf("\n");
rows--;
}
}
However, I feel as though I’ve hit a brick wall with trying to get it to iterate backwards properly. I know it should only be a few basic loops but I’m well and truly stuck. I’m sure it’s easy… I think I’ve just been looking at it too long. Ideas?
You are so close, you only need to take a breath and you’ll see it.
When you print out your character, it has to be done after this part
or it won’t fall at the end of the string. What you need is another loop that starts at the character that’s one below the last character printed. It should print a character and decrement that character until it has printed the ‘A’ character.
Since this is homework, I’ll give you a chance to write that loop before telling you the exact details.