Alphabet from the middle, out.

I have it so my program prints out the alphabet, but i need it so the "A" is in the middle of the shape and it branches out. What change do I need to make so that's possible?

nclude <iostream>
using namespace std;

int main ()
{
int N ;
do
{

cout << endl << "Please enter an odd integer, 1 - 51 : " ;
cin >> N ;
if( N < 1 || N > 51 || N % 2 == 0 )
cout << "Invalid number." << endl;
}
while( N < 1 || N > 51 || N % 2 == 0 );

cout << endl ;
int amount_letters = N;
int amount_spaces = 0;
for (int i=0; i < N ; ++i)
{
for (int j=0; j < amount_spaces; ++j)
cout << ' ';

for (int k=0; k < amount_letters ; ++k)
cout << char ( k + 65 );

if( i < N/2 ) // top half of the hourglass
{
amount_letters -= 2;
amount_spaces += 1 ;
}

else // bottom half of the hourglass
{
amount_letters += 2;
amount_spaces -= 1 ;
}

cout << endl; //print newline
}
}
nclude <iostream>
using namespace std;

int main ()
{
    int N ;
    do
    {

        cout << endl << "Please enter an odd integer, 1 - 51 : " ;
        cin >> N ;
        if( N < 1 || N > 51 || N % 2 == 0 )
            cout << "Invalid number." << endl;
    }
    while( N < 1 || N > 51 || N % 2 == 0 ); 

    cout << endl ;
    int amount_letters = N;
    int amount_spaces = 0;
    for (int i=0; i < N  ; ++i) 
    {
        for (int j=0; j < amount_spaces; ++j) 
           cout << ' ';

        for (int k=0; k < amount_letters ; ++k) 
           cout << char ( k + 65 );

        if( i < N/2 ) // top half of the hourglass
        {
            amount_letters -= 2; 
            amount_spaces += 1 ;  
        }

        else // bottom half of the hourglass
        {
            amount_letters += 2; 
            amount_spaces -= 1 ;  
        }

        cout << endl; //print newline
    }
}
I didn't copy the #include <iostream> all the way but i do have it.
Your program is working fine; it's printing

1
2
3
4
5
ABCDE
 ABC
  A
 ABC
ABCDE

What else do you want?
I need it to print like this

GFEDCBABCDEFG
 FEDCBABCDEF
  EDCBABCDE
   DCBABCD
    CBABC
     BAB
      A
     BAB
    CBABC
   DCBABCD

and so on. Alphabet on both sides of the letter "A"
Try using this

1
2
3
4
5
for(int k = amount_letters / 2 - 1; k >= 0; --k)
    cout << char ( k + 66 );

for (int k=0; k <= amount_letters / 2; ++k) 
    cout << char ( k + 65 );
Last edited on
Topic archived. No new replies allowed.