ALPHABET PYRAMID - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ALPHABET PYRAMID

aazizaaziz Posts: 2Member
i need help writing a c++ program that takes an alphabet from user and makes a pyramid. so for example if you input 'C', the output should be:

A
ABA
ABCBA

PLEASE HELP!!

Comments

  • WuduWudu Posts: 25Member
    : i need help writing a c++ program that takes an alphabet from user and makes a pyramid. so for example if you input 'C', the output should be:
    :
    : A
    : ABA
    : ABCBA
    :
    : PLEASE HELP!!
    :

    [blue]
    Use a switch-case-statement like
    [code]
    switch (Input)
    {
    case 'a':
    Output = "A";
    break;
    case 'b':
    Output = "ABA";
    break;
    // ..
    }
    [/code]
    and for constructing the pyramid you can do something like
    [code]
    char Input = 'a';
    while (Input != UserInput)
    {
    Output = //..
    Input++; // increment, see asciicode
    }
    [/code]
    [/blue]
  • 13thApostle13thApostle Posts: 5Member
    : : i need help writing a c++ program that takes an alphabet from user and makes a pyramid. so for example if you input 'C', the output should be:
    : :
    : : A
    : : ABA
    : : ABCBA
    : :
    : : PLEASE HELP!!
    : :
    :
    : [blue]
    : Use a switch-case-statement like
    : [code]
    : switch (Input)
    : {
    : case 'a':
    : Output = "A";
    : break;
    : case 'b':
    : Output = "ABA";
    : break;
    : // ..
    : }
    : [/code]
    : and for constructing the pyramid you can do something like
    : [code]
    : char Input = 'a';
    : while (Input != UserInput)
    : {
    : Output = //..
    : Input++; // increment, see asciicode
    : }
    : [/code]
    : [/blue]
    :


    you COULD do that, but that switch statement looks pretty big.
    Also, the user may enter capital letters too, not just lowercase

    Here's the way I'd do the program. I've documented everything, so
    hopefully you get what I'm doing. This was written and run in vc++
    It needs to be run in a dos window

    [code]
    [blue]
    // included header files //
    #include // for cout
    #include // for toupper
    #include // for system("pause") and exit()

    // begin main function
    int main(int argc, char* argv[])
    {
    // local variables

    char Input = 'A'; // what the user will Input
    int intValue = 65; // the ASCII value of the Input

    // begin main while loop (so you can run the program as
    // many times as you want, testing different numbers
    ///////////////////////////////////////////////////////
    // Note that '0' is the terminating character

    while(Input != '0')
    {
    // prompt user for intput

    cout << "Enter Character (0 to exit): ";
    cin >> Input;

    // if Input is the terminating number, break out
    // of the loop

    if(Input == '0')
    break;

    // convert to ASCII number
    // NOTE: uppercase and lowercase letters have different
    // ASCII integer equiv, so I used the TOUPPER function,
    // which converts the character to uppercase, whether
    // it's uppercase already or lowercase
    ////////////////////////////////////////////////////
    // Additionally, for the sake of the for-loop, I
    // subtracted 64 from the value (since 65 is 'A' in
    // ASCII, so intValue is ready to be used in the for- // loop

    intValue = toupper(Input) - 64;

    // the following 6 lines of code build the pyramid
    ///////////////////////////////////////////////////
    // the first for-loop tells the compiler start at
    // the ASCII value of the Input and work backwards,
    // line by line (note the ENDLINE at the end of the
    // for-loop

    for(int i = intValue; i >= 1; i--)
    {
    //the second for loop produces all
    // the characters from 'A' to whatever
    // 'i' is above

    for(int n = 1; n <= intValue - i + 1; n++)
    cout << char(n + 64);

    // this third for loop then produces all
    // the characters from 'i' (first for loop)
    // to 'A' (works backwards)

    for(int g = n - 2; g >= 1; g--)
    cout << char(g+64);

    // after the line is printed, the computer moves
    // to the next line

    cout << endl;

    } // end for (int i = intValue; i >= 1; i--)

    // this function pauses the action (kind of like getch)
    // It also prints a default message ("Press any key..."

    cout << endl;
    system("pause");

    } // end while (Input != '0')

    return 0;

    } // end main function

    [/code]
    [/blue]

    E-mail me with any questions @ 13thApostle@highstream.net
    Good Luck!
Sign In or Register to comment.