### Hour Glass shape with letters (Beginner)

How would I go about coding this. just need some helpful thing to get me started?

For example, if the user enters 7, then the following hourglass
comprised of 7 rows is printed:

DCBABCD (0 spaces preceding first letter)
_CBABC_ (1 space preceding first letter)
__BAB__ (2 spaces preceding first letter)
___A___ (3 spaces preceding first letter)
__BAB__ (2 spaces preceding first letter)
_CBABC_ (1 space preceding first letter)
DCBABCD (0 spaces preceding first letter)

P.S. I'm just a beginner, I don't know a lot stuff yet.
Last edited on
also the '_' are suppose to be spaces.
Hi there,

You will need a loop, like a for-loop.
The amount of times you loop will be determined by the number the user enters.

With each iteration, the amount of letters decreases, the amount of spaces increases.
When you reach the halfway point, the amount of letters increases and the amount of spaces decreases.

There is a little more to it - the difference between even and odd numbers entered by the user for instance - but this should be a start.

Please do let us know if you require any further help.

All the best,
NwN
would I use something like :

for ( int count = N - 2 ; count >= 1 ; count ++ )
{
for ( int j = 0 ; j < ( N - count ) / 2 ; j++ )
{
cout << ' ' ;
}
for ( int j = 0 ; j < count ; j++ )
{
cout << char ( j + ' A ' ) ;
}

????
Hi there,

This: for ( int count = N - 2 ; count >= 1 ; count ++ ) would be an infinite loop, unless count is negative, in which case it will never loop at all. You are looking too far:

 12345678910111213141516 int amount_letters = user_number; int amount_spaces = 0; for (int i=0; i < user_number; ++i) //as many rows as there are letters { for (int j=0; j < amount_spaces; ++j) //print spaces std::cout << " "; for (int k=0; k < amount_letters; ++k) //print letters std::cout << "#"; amount_letters -= 2; //decrease letters amount_spaces += 2; //increase spaces std::cout << std::endl; //print newline }

Hope that makes sense.

All the best,
NwN
This is more naturally solved using recursion. Do you know what that is?
I get what your trying but i'm not getting how to print it in the letter format also I don't under stand the spacing for each line.

I know the spaces has to be something like row - 'A' or something like that?
Hi there,

I'm afraid I'm not sure what you're asking - could you please verify, perhaps share your code once more?

All the best,
NwN
well right now it prints out this
if I type in a 7 it'll print

AAAAAAA
AAAAA
AAA
A

I need it to print it in a centered format like this:

AAAAAAA
_AAAAA_
__AAA__
___A___

Also I need it to print the alphabet.
so it would actually need to be printed like this:

DCBABCD
_CBABC_
__BAB__
___A___

Sorry I made a little mistake:

 12345678910111213141516171819 int amount_letters = user_number; int amount_spaces = 0; for (int i=0; i < user_number; ++i) //as many rows as there are letters { for (int j=0; j < amount_spaces; ++j) //print spaces std::cout << " "; for (int k=0; k < amount_letters; ++k) //print letters std::cout << "#"; for (int j=0; j < amount_spaces; ++j) //print spaces again std::cout << " "; amount_letters -= 2; //decrease letters amount_spaces += 2; //increase spaces std::cout << std::endl; //print newline }

On a sidenote, I would like to acknowledge that Mr. Molina;s suggestion of recursion is indeed the better (more code-compact) choice. However, as you remarked you are a beginner I wrongly assumed you hadn't seen recursion yet, so if you have, you'd probably want to use recursion.

All the best,
NwN
Last edited on
That looks just like your first one did. I'm so lost right now. :(
Hi there,

Thanks,
NwN
This is what i have right now.

#include <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 ) //less than 1 , more than 51 or even
cout << "Invalid number." << endl;
}
while( N < 1 || N > 51 || N % 2 == 0 ); //less than 1, greater than 51 or even

cout << endl ;
int amount_letters = N;
int amount_spaces = 0;
for (int i=0; i < N; ++i) //as many rows as there are letters
{
for (int j=0; j < amount_spaces; ++j) //print spaces
std::cout << ' ';

for (int k=0; k < amount_letters; ++k) //print letters
std::cout << 'A';

amount_letters -= 2; //decrease letters
amount_spaces += 2; //increase spaces

std::cout << std::endl; //print newline
}
}
amount_spaces += 2; //increase spaces

Should be:

amount_spaces++;

Then you will need to reverse it - but you should be able to figure that out with the code you already have.

Let us know if you need any further help.

All the best,
NwN
I'm not figuring out how to reverse it. I'm sorry that I'm so bad at this.
bump
 123456789101112131415161718192021222324252627282930313233343536373839404142 #include 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 ) //less than 1 , more than 51 or even cout << "Invalid number." << endl; } while( N < 1 || N > 51 || N % 2 == 0 ); //less than 1, greater than 51 or even cout << endl ; int amount_letters = N; int amount_spaces = 0; for (int i=0; i < N ; ++i) //as many rows as there are letters { for (int j=0; j < amount_spaces; ++j) //print spaces std::cout << ' '; for (int k=0; k < amount_letters ; ++k) //print letters std::cout << 'A'; if( i < N/2 ) // top half of the hourglass { amount_letters -= 2; // decrease letters amount_spaces += 1 ; // increase space } else // bottom half of the hourglass { amount_letters += 2; // increase letters amount_spaces -= 1 ; // decrease space } std::cout << std::endl; //print newline } }
Last edited on
Topic archived. No new replies allowed.