patterncMinor
Alphabetic Pattern Pyramid
Viewed 0 times
alphabeticpatternpyramid
Problem
I want to know if I can improve this program. Its goal is to print a pattern in a pyramid form, by first receiving a letter. The first row is always 'A', only. Then 'ABA', then 'ABCBA'. Increasing order, then decreasing.
#include
int main(void)
{
char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char letter;
printf("Enter an uppercase letter: ");
scanf("%c", &letter);
letter -= 65;
for (int row = 0; row row; spaces--)
printf(" ");
for (char letter2 = 0; letter2 = 0; )
printf("%c", alphabet[letter3]);
printf("\n");
}
}Solution
Instead of having a static array of
Leaving Your
Your index of chars (
In case of single char,
chars You could just use ASCII values from 0x41 to 0x5A. If You still insist having a static array, please consider marking it as a const.Leaving Your
int main() without any return value --- sign of the bad taste, to say the least. Consider having some proper error codes or at least return 0 on proper exit.Your index of chars (
letter2, letter3) can't be negative, so why would You use int instead of uint_* or size_t? Same applies to row and spaces variables.In case of single char,
putchar() is way faster then printf("%c", character).Context
StackExchange Code Review Q#126374, answer score: 2
Revisions (0)
No revisions yet.