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.
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
}
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.
#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';
#include <iostream>
usingnamespace 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
}
}