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.

There are a few problems with your code

first off it is a good idea to work out the logic and
method you want on paper first because I suspect
that the book method has resulted in you using a method that
has confused you a little

log10(1) == 0
log10(0) == error

luckily you avoid the error but it helps if you use temporaries

double power2 = pow(2, row);
int ndigits = ceil(log10(power2);

it would allow you to print out numbers to see what is happening
at stages and reduces what you need to type

so the ones are being lost because you have not decalred what to do if log10(power2) == 0; which happens when power2 = 1

the more major thing is that you are using doubles in your for
loops it is not a good choice stick to int, unsigned int or iterators.

the for loop has three things that go in the backets

initial conditions;
condition to do;
action

the condition to do has to be true for a cycle to happen
so

intialise
while(end condition)
{
action;
}

if you look at line 20
this needs row == column to be true
to run otherwise it stops this is not what you want;

if you are using a double stop condition it is much better to use <

I recommend you test one block at a time until you get this working /* starts a comment block
and */ ends one

your own functions would help make you code easier.

I hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.