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 <stdio.h>
int main(void)
{
char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char letter;
printf("Enter an uppercase letter: ");
scanf("%c", &letter);
letter -= 65;
for (int row = 0; row <= letter; row++)
{
for (int spaces = letter; spaces > row; spaces--)
printf(" ");
for (char letter2 = 0; letter2 <= row; letter2++)
printf("%c", alphabet[letter2]);
for (char letter3 = 0 + row; --letter3 >= 0; )
printf("%c", alphabet[letter3]);
printf("\n");
}
}
2 Answers 2
Instead of having a static array of 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)
.
-
\$\begingroup\$ Thanks for the answer. I didn't return something because someone told me main() automatically returns 0. Is that actually true? \$\endgroup\$Lúcio Cardoso– Lúcio Cardoso2016年04月22日 16:21:05 +00:00Commented Apr 22, 2016 at 16:21
-
2\$\begingroup\$ No, functions will not return anything unless you explicitly tell it to do so. \$\endgroup\$THE AMAZING– THE AMAZING2016年04月22日 16:21:49 +00:00Commented Apr 22, 2016 at 16:21
I assume you are requesting a 2d ASCII representational filling of an equilateral triangle using an incremental basic Latin ascended alphabetical array.
Array("a","b","c"...)
First we need to determine the parameters of the 'pyramid' I will use 25 x 13. I will also assume you want to draw down from the top.
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][A][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][A][B][A][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][A][B][C][D][E][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][A][B][C][D][E][F][G][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][A][B][C][D][E][F][G][H][I][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][A][B][C][D][E][F][G][H][I][J][K][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][A][B][C][D][E][F][G][H][I][J][K][L][M][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][ ][ ][ ][ ]
[ ][ ][ ][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][ ][ ][ ]
[ ][ ][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][ ][ ]
[ ][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][ ]
[A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]
Notice the [ ]
these will need to be empty spaces for design purposes.
int main(void){
unsigned short int dimensions[2] = [25, 13];
unsigned short int n_characters = 1;
for(unsigned short int row=0; row <= dimensions[1]-1; row++){
printf("%c", alpha_string(n_characters);
n_characters += 2;
}
return 0;
}
char* alpha_string(unsigned short int n_characters, unsigned short int width){
char label[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char ret[n_characters];
for (unsigned short int i= 0; i<= width-n_characters; i++){
ret[i] = " ";
}
for (unsigned short int i= 0; i<= n_characters; i++){
ret[i] = label[i];
}
return ret;
}
Consider this pseudo code, i free handed this without error checks.