4
\$\begingroup\$

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");
    }
}
\$\endgroup\$
0

2 Answers 2

2
\$\begingroup\$

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).

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the answer. I didn't return something because someone told me main() automatically returns 0. Is that actually true? \$\endgroup\$ Apr 22, 2016 at 16:21
  • 2
    \$\begingroup\$ No, functions will not return anything unless you explicitly tell it to do so. \$\endgroup\$ Apr 22, 2016 at 16:21
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.