Hi, all. I'm pretty new to C++, and I think I'm getting the hang of it, but there's a problem with a program I'm writing and I just can't seem to fix it. The program's supposed to print a pyramid like this:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
Sorry about the alignment. Basically, the column in the center of the pyramid has squares of 2 from 1 to 128. It has to be spaced so that if the number has 1 digit, then 4 spaces come before it; if it has 2 digits, 3 spaces come before it; and if the number has 3 digits, two spaces are printed. I have the algorithm for the spacing right (I think), but my output isn't complete. When I run the program, it fails to print the half of the pyramid to the left of the middle column. It also doesn't print any of the 1's. So my output looks something like this:
2
4 2
8 4 2
16 8 4 2
32 16 8 4 2
64 32 16 8 4 2
128 64 32 16 8 4 2
I'm sure the problem has something to do with the conditions for the for loops, but I really don't know what's wrong... I would be very grateful if someone could please look at my code and point out my errors. Thanks in advance.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double column,row;
for (row = 0; row <= 8; row++) {
for (column = 0; column == 7 - row; column++) {
if (ceil(log10(pow(2,column))) == 1)
cout << " " << pow(2,column);
else if (ceil(log10(pow(2,column))) == 2)
cout << " " << pow(2,column);
else if (ceil(log10(pow(2,column))) == 3)
cout << " " << pow(2,column); }
//left half
for (column = 0; column == row; column++) {
if (ceil(log10(pow(2,column))) == 1)
cout << " " << pow(2,column);
else if (ceil(log10(pow(2,column))) == 2)
cout << " " << pow(2,column);
else if (ceil(log10(pow(2,column))) == 3)
cout << " " << pow(2,column); }
//right half
for (column = row - 1; column >= 0; column--) {
if (ceil(log10(pow(2,column))) == 1)
cout << " " << pow(2,column);
else if (ceil(log10(pow(2,column))) == 2)
cout << " " << pow(2,column);
else if (ceil(log10(pow(2,column))) == 3)
cout << " " << pow(2,column); }
cout << endl; }
return 0; }
P.S. This is based mostly on pseudocode that my textbook provided.