0

Ive been struggling with this problem for quite some time now and im finally caving and asking for help. first ill show you my code and explain my thought process.

#include <cs50.h>
#include <stdio.h>

int main(void)

{
    int height, rows, spaces, hashes;

        do 
        {   
            printf("How high will mario climb? Please pick a number between 1-23: ");
            height = GetInt();
        } 
        while (height <= 0 || height >= 24);

    for (rows = 0; rows <= height; rows++) //print user generated amount of rows
    {

        for (spaces = height - rows - 2 ; spaces == 0; spaces --) //print spaces
        {
            printf(" ");
        }
        for (hashes = rows + 2; hashes <= height; hashes++) //print hashes
        {
          printf("#");  
        }
        printf("\n");
    }
   return 0;
 }

this is printing: usuing user input (10)

##########
#########
########
#######
######
#####
####
###
##
 #

at first I thought I did something backwards so i switched space to print "s" and hashes to print "h" expecting

hhhhhs
hhhhss
hhhsss
hhssss
shssss 

but got

hhhhhh
hhhhh
hhhh
hhh
hh
 h

So I believe the problem lies somewhere in the for loops, but I can not figure out what it is. (I'm also aware the final row is messed up but Im trying to fix the pyramid first)

my logic behind: (rows = 0; rows <= height; rows++) //print user generated amount of rows

is that the rows will increase by one until it reaches the height specified by the user (I believe everything up to and including this line of code is correct)

...

my logic behind : (spaces = height - rows - 2 ; spaces == 0; spaces --) //print spaces

is that the amount of spaces should = the height (10) - rows (0) - 2 and then decrease by 1 each time through the loop until eventually there is only hashes. this way the first row should leave room for 2 hashes(top of the pyramid)

...

logic behind (hashes = rows + 2; hashes <= height; hashes++) //print hashes

is that the amount of hashes should be = to the row (0) + 2 increasing by 1 each time though the loop until it is greater than or equal to the user input height. This way the first row will include 2 hashes (0 + 2 = 2)

any insight as to where the problem is and tips or advise on how to come up with the solution would be greatly appreciated !

  • Can you please help me if the above example was explained in lecture in CS50 introduction to computer science? Can you please help me with the link? – Rahul John Sep 17 '18 at 11:44
0

You have several bugs, but you're on the right track. I'll try to get you thinking about it, without actually doing it for you.

Your biggest problem is here:

for (spaces = height - rows - 2 ; spaces == 0; spaces --) //print spaces

If you printed "s" instead of space, the code would actually have printed a single "s" in the entire output. Why does this happen? Why doesn't it print one or more "s" on each line? Except for the one time, this line never prints anything. It isn't going into the loop. Why? How does a FOR loop command execute? Remember that it is executing the test condition to see if it can go into the loop. What is the actual test here? What are the values of height, rows and , most important, "spaces" when it tries to start the loop?

Once that's fixed, your next issue is that your pyramid is upside down. You're printing the bottom of the pyramid first. How can you flip that? You need to change the logic of how many #'s are printed. At least you really are printing a progression, so your logic is in the neighborhood.

Finally, make sure you're printing the right number of spaces and #s on each row.

You're close. Once you fix the test condition, the rest should be easy to fix. If not, come back and let us know what progress you've made.

| improve this answer | |
  • got it!! I decided to try everything the opposite way. Instead of the initialization expression starting at the desired integer, I started them at zero and then worked up to the desired integer based on the text expression. most examples Ive seen in my brief time with programming have the initialization expression starting at zero, is that the way it usually is? – Eric Bezanson Apr 9 '15 at 1:33
  • Either way works, starting at 0 and going up is more common. On another note think about this. The number of spaces plus the number of #s on each line is a constant. ;-) Anyways, if my answer was sufficient, please mark it as answered. Good luck. – Cliff B Apr 9 '15 at 1:54

You must log in to answer this question.

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