0

I have been able to print the half pyramid but its backwards (its the right half on the pyramid instead of the left). I believe it must be something to do with my spaces and perhaps not my hash's. I'm not asking for the answer id just like someone to point me in the right direction. I'll only post the 'for loop' parts.

Ive used a do while loop to get the input from the user.

    h = GetInt();
    x = h;

for  (int i = 0; i <x; i++)
{
     for (int i = 1; i < x; i++)
     {
         printf(" ");
     }
     for( int i = x; i + 2 >h; i--)
     {
         print("#");
     }
     printf("\n";
}

My apologies if its hard to read I couldn't Copy and paste from the CS50 appliance.

2
  • Can you add the code that defines what x and h contain? Otherwise, it's almost impossible to sort out.
    – Cliff B
    Jun 23, 2015 at 8:28
  • Ive added those. thanks.
    – Jay Jay
    Jun 23, 2015 at 9:25

2 Answers 2

1

The code as written prints the same line each time for the height, and it only has two hashes. The good news is that it will print the first line correctly for any height.

You need to focus on how a given line needs to look - how many spaces and how many #s. There is a relationship between the spaces, the hashes and the number of lines. Try working out on paper how many of each should be printed for a 1 line pyramid, then a two line, and so on. You should be able to figure out formulas for spaces and for hashes based on the height of the pyramid and which line is being printed. That should get you started.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

1
  • Thank you for your help, im gonna get on with this pset now. I think i know what to do!
    – Jay Jay
    Jun 24, 2015 at 9:39
1

if you follow the logic of your code, you'll find that the first nested loop always iterates x - 1 times and the second nested loop always iterates 2 times.

what you need to do is having the first nested loop iterate "less" times each row (each iteration of the outer loop) and the second nested iterate "more" times each row.

probably an easy solution would be having a variable to store the current number of spaces and another one to store the number of hashes, using these variables to make the relevant loops iterate the correct number of times, and updating them as you go to the next row.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .