# PSET1 Mario need help!

Below is my code and it's output for PSET1 Mario. Can anyone tell me what's wrong with it?

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

int h;

int GetHeight(); // creat a function: GetHeight() to prompt user to imput the height of pyramid.

int main(void) // get input from user and make a half-pyramid
{
do
{
GetHeight();
}
while (h > 23 || h < 0); // assigned h a value.

int l = 1; // create l to promise a new line.
int s = h - 1;
int b = l + 1;

while(l == 1)
{
while (s > 0)
{
printf(" "); s--;
}
while (b > 0)
{
printf("#"); b--;
}
if (h == 0)
{
l--;
}
else
{
printf("\n"); h--;
}
}

}

int GetHeight() // the content of GetHeight(), it returns a value h to main()
{
printf("height:");
h = GetInt();![enter image description here][1]
return h;
}
``````

You are declaring int s and int b outside of the loop l == 1, so after you do go around once and do h-- ( making h == 2, in the case of entering 3 ), s and b don't change, they are still 2 and 2, as they were initialized. Assignments are static, for example, if you say a = b; b = b + 1; a would not increment, a just takes the value of b, they are not magically paired.

I just finished this pset, while your approach is different than mine, I'll see if I can help.

First, put a break; before your l == 1 loop, then just inside that loop write a printf for each of your variables, then run gdb. That will show you where your variables are at each time the loop runs, then you can say this is what the variable should be, and ask your self how can I make that happen.(I suppose you don't have to use gdb or the break and run it with the printf's alone, but running it step by step will help point you directly to where the fix needs to happen.

Second, once you've fixed that loop you'll notice if you left every thing else the same I believe your still going to only get 2 # per row even if your spaces are correct.

One thing that helps is instead of using generic "foo" variables, use descriptive ones. Try replacing them, for example use "hashtags" for the variable that is used to print your hashtags, "rows" for rows, "spaces" for spaces, and "height" for height.

Work on one loop at a time until it's right. Try writing a different program for each loop. Try writing a spaces program that just prints the correct amount of spaces for each row, then put them all together.

The hashtags was the hardest part for me, when you get to that point try drawing the desired output on paper and the value of your variables for each row to see how you need to conditionalize that loop, what value to compare to and how to increment your variable.

A few suggestions: Comment on each loop so you can see the intent of each one, I heard you get extra points for comments. Before you turn it in try to make it as neat and tidy looking as possible. You could also look at validating the users input inside your get height function, so that it doesn't appear inside your main function.

Hope that helps I'm trying to be vague so I don't give you the answer, but I think if you put the two answers you got so far together you should get it. ask questions if any of that doesn't make sense.

• thanks a lot! i have solved it!!! Commented Mar 30, 2015 at 1:51