2
\$\begingroup\$

Now I'm reading C Premier Plus by Stephen Prata and there's an exercise 4 in chapter 6 which has no solution in the book, so I can't check if I did it properly. My code works like it should, but I want to know maybe there are some better or cleaner solutions.

Here's the description:

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 .

Hint: Use an outer loop to handle the rows. Use three inner loops in a row, one to handle the spaces, one for printing letters in ascending order, and one for printing letters in descending order.

#include <stdio.h>

int main(void) {
    char ch;
    char row;
    char spaces;
    char let;
    char back;

    printf("Please, insert an uppercase letter: ");
        scanf("%c", &ch);

    for(row = 'A'; row <= ch; row++) {
        for(spaces = ch; spaces > row; spaces--)
            printf(" ");

        for(let = 'A'; let < row; let++)
            printf("%c", let);    

        for(back = let; back >= 'A'; back--)
            printf("%c", back);
        printf("\n");
    }
    return 0;
}
\$\endgroup\$
  • 2
    \$\begingroup\$ You should not modify code much after you posted it for code review. See this meta question for details. \$\endgroup\$ – mkalkov Jan 27 '15 at 20:10
  • \$\begingroup\$ Ok. Sorry for that. \$\endgroup\$ – Vitaliy Pisnya Jan 27 '15 at 20:36
  • \$\begingroup\$ Please don't change the code in your question after receiving answers. That invalidates them. \$\endgroup\$ – Quill Apr 10 '16 at 7:08
2
\$\begingroup\$

It's been a while since I coded in C but here are couple observations.

  • Input validation. You should check if the scanned character is really an upper case letter or exit with an error otherwise.
  • Choice of functions. Since you're scanning and printing out a character at a time, you could have used putchar instead of printf, and getchar instead of scanf.
  • Variable naming. let is not the best variable name since it is often used as a keyword in other languages. You also could have used the same variable in place of let and back.
  • Code formatting. There are a lot of different opinions about how to format source code, but most agree that it is better to put parenthesis around loop and condition bodies.

Here is how I would have formatted your code.

#include <stdio.h>

int main(void) {
    char ch;
    char row;
    char spaces;
    char letter;

    printf("Please, insert an uppercase letter: ");
    scanf("%c", &ch);

    for (row = 'A'; row <= ch; row++) {
        for (spaces = ch; spaces > row; spaces--) {
            printf(" ");
        }
        for (letter = 'A'; letter < row; letter++) {
            printf("%c", letter);    
        }
        for (; letter >= 'A'; letter--) {
            printf("%c", letter);
        }
        printf("\n");
    }
    return 0;
}
\$\endgroup\$
  • \$\begingroup\$ 1. I can do check, but it doesn't required in the exercise. \$\endgroup\$ – Vitaliy Pisnya Jan 27 '15 at 20:15
  • 1
    \$\begingroup\$ Sure, if it was an exam question, the answer would have been accepted. However, you asked for improvement suggestions, and here they are. \$\endgroup\$ – mkalkov Jan 27 '15 at 20:19
  • \$\begingroup\$ Thanks:) Can you write an example of code formatting. I didn't get it. \$\endgroup\$ – Vitaliy Pisnya Jan 27 '15 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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