Thread: Trying not to gloat...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    99

    Cool Trying not to gloat...

    But I'm really proud of myself. First time a question in the book had me stumped for a while, but I was able to figure it out. Check this baby out:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        char user_letter;
        char pyramid_letter;
        char space_counter;
        int i=0;
        int j;
    
        printf("Enter a capital letter please.  Thank you very, very, much:    ");
        scanf("%c", &user_letter);
        space_counter=user_letter-1;
    for(i='A'; i<=user_letter; i++)
    {
        for(j='A'; j<=space_counter; j++)
            printf(" ");
        for(pyramid_letter='A'; pyramid_letter<=i; pyramid_letter++)
            printf("%c", pyramid_letter);
    
        for(pyramid_letter=pyramid_letter-2; pyramid_letter>='A'; --pyramid_letter)
            printf("%c", pyramid_letter);
        space_counter--;
        printf("\n");
    }
    
        return 0;
    }
    Don't make too much fun of me LOL.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    But I'm really proud of myself.
    And you should be -> This is why a lot of people are addicted to programming: The feeling you get when you figure out a solution to something!

    You need to fix your indentation

    If you wanted to play around with it, I suggest that you use getc and putc instead of scanf and printf if you are only passing characters (even for the space and new line) - All except the intro message (of coarse)

    I like to use
    Code:
    int main(void)
    {
      /* Blah blah blah */
      
      return EXIT_SUCCESS; // (instead of 0)
    
    }
    It's defined in stdlib.h and does the job and makes your code that little bit more portable.

    You need to test if the input is a capital letter before trying to use it -> This is about making sure that the input isn't unexpected.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by gratiafide View Post
    First time a question in the book had me stumped for a while, but I was able to figure it out.
    Congratulations!

    Now, just think of the kicks you get when you get to solve a problem you or your coworkers/friends/anyone has, by writing your own program. It's more than a little addictive

    The indentation could use a bit of fixing, but that's a readability issue for us humans; the compiler will not care.

    Assuming you wish to progress to writing command-line tools at some point, you might rewrite the initial part of your code to take the character from the command line instead of scanning it from input:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        char user_letter;
        char pyramid_letter;
        char space_counter;
        int i=0;
        int j;
    
        if (argc != 2) {
            fprintf(stderr, "Usage: %s LETTER\n", argv[0]);
            return EXIT_FAILURE;
        }
    
        if (argv[1][0] == '\0') {
            fprintf(stderr, "No LETTER specified.\n");
            return EXIT_FAILURE;
        }
    
        if (argv[1][1] != '\0' || !(argv[1][0] >= 'A' && argv[1][0] <= 'Z')) {
            fprintf(stderr, "%s: Not a capital letter.\n", argv[1]);
            return EXIT_FAILURE;
        }
    
        user_letter = argv[1][0];
        space_counter = user_letter - 1;
    Above, argv[0] is the name the program was run with, and argc is 1+the number of parameters, i.e. argc == the number of strings in the argv array.

    If argc < 2 there is only the program name, no parameters.

    If argc > 1, the first parameter is in argv[1]. It is then never a NULL pointer, but it may be an empty string (say, ./a.out ""), in which case argv[1][0] == '\0'.

    In the above code, the first if statement checks that there is exactly one command-line parameter. (Program name and one parameter string means argc == 2.) Otherwise the program shows the usage and exits.

    The second if statement checks if the first parameter is an empty string. I like to be careful, you see; it costs just a few lines of code, but saves a lot of trouble if you accidentally (say, in a script) supply an empty string.

    The third if statement makes sure the first character is an ASCII uppercase letter A..Z, and only one-character long. Otherwise, it complains.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Thanks for the advice and encouragement to both of you! I really appreciate it and this forum.

Popular pages Recent additions subscribe to a feed