Reputation: 11
I want to create a program that creates a pyramid. Its size (number of lines) is determined by the user input.
This is what I have tried so far. It is only showing one half of the pyramid.
#include <iostream>
using namespace std;
int main()
{
//delcare all the variables
int n, m, i, j;
cout << "Enter the number of lines: ";
cin >> n;
for (i = 1; i <= n; i++)
{
//prints the spaces in between row and column using loop
for(j = 1; j <= n - i; j++)
{
cout <<" ";
}
//prints the element of using loop
for (j = i; j >= 1; j--)
{
cout <<" ";
}
//prints first and last elements using loop
for (j = 2; j <= i; j++)
{
cout << " " << j << " ";
}
//elements in new line printed
cout << "\n";
}
return 0;
}
I was expecting a whole pyramid when I entered the number 7 but the current output is only half of the pyramid, namely from 2 up to 7.
Upvotes: 0
Views: 4382
Reputation: 13134
Declare variable as close to where they're used. Give them meaningful names:
#include <iostream>
#include <iomanip>
int main()
{
std::cout << "Enter the number of lines: ";
int num_lines;
if (!(std::cin >> num_lines) || num_lines < 1 || 15 < num_lines) {
std::cerr << "Input error. Bye :(\n\n";
return EXIT_FAILURE;
}
for (int current_line{ 1 }; current_line <= num_lines; ++current_line) {
for(int spaces = 0; spaces < num_lines - current_line; ++spaces)
std::cout << " ";
for(int number = current_line; number; --number)
std::cout << std::setw(2) << std::right << number << ' ';
for (int number = 2; number <= current_line; ++number)
std::cout << std::setw(2) << std::right << number << ' ';
std::cout.put('\n');
}
}
Enter the number of lines: 15
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
15
:I am using std::wetw()
to pad the output of the numbers to two digits. std::right
right-justifies the output inside those two characters: 1
~> " 1"
but 11
~> "11"
. I do that so i don't have to write more complicated code when I am printing the spaces at the beginning of each line. One printed number is alsways three characters wide - two for the number and a following space to seperate the numbers.
Talking of space caracters to begin each line: At the base of the pyramid all numbers from 15 to 1 and up to 15 again get printed. So the number of spaces preceeding the numbers must be 14 times three for the first line, 13 times three for the second line, 12 times three for the third and so on until it are zero times three spaces in the last, the 15th line. So as you can see the number of spaces is num_lines - current_line
times three. That's what the first inner for()
-loop does.
The other two inner for()
-loops should be self-eplanatory. One counts down from current_line
to 1
and the other back up again to current_line
. The loop counting upwards starts at 2
to not print two times the number 1
.
Upvotes: 2