# What's wrong with my code for mario?

I've been stuck on pset1 for five months now. I like to do things chronologically and mario has been giving me headaches.

Every time I think I have the problem solved, I'm told it's wrong or a lot is missing. I need help. The cs50 subreddit community has been very helpful but I need more of an active community to help.

I really need help, tips and tricks. I've practiced with loops on the cs50 run and most of the time my for loops come with errors.

I don't want to give up but I'm this close in doing so. Please help. Thank you.

• Have you solved your problem? If not can you post any code here? Otherwise it is better to choose one of the answers or make a comment, so people know the status.
– user2477
Commented Oct 1, 2014 at 10:41
• Did you post any code? Commented Dec 13, 2017 at 16:11

Five months is too long for a problem like or pretty much any other problem in the course.

You could try solving it manually with low-tech tools (e.g., a pencil and a paper) trying to draw half a pyramid row-by-row from top to bottom and left to right.

Suppose the input is 5, we should get the following output...

``````    ##
###
####
#####
######
``````

By analyzing the problem, we can get information like:

1. The initial number of spaces to be printed out in terms of the height (i.e., 5) is

``````height + 1 - the initial number of hashes
``````
1. The initial number of hashes is 2.
2. The number of spaces decreases each row until it becomes 0 in the last row.
3. The number of hashes increases by 1 until it becomes `height + 1` in the last row.

Using this information, we could write the following pseudocode (assuming `height` is a valid integer inputted by the user):

``````calculate the initial number of spaces in terms of height
calculate the initial number of hashes in terms of height

// draw the rows
for (i = 0 to i < height, increment i)
// draw spaces
for (j = 0 to j < spaces, increment j)
print a space

// draw hashes
for (j = 0 to j < hashes, increment j)
print a hash

move to the next row
decrement the number of spaces
increment the number of hashes
``````
• The problem is that I've done it on pen and paper and windows notepad. I've worked on greedy.c as well and I have a much harder time than that. I know 5 months is a long time, mind you, I'm still new to programming and new to the C language. I appreciate the help. Commented Jun 10, 2014 at 20:00
• Spend a bit of time thinking about Kareem's algorithm. The relationship between the height and the width (i.e. the number of hashes on the base row) is the first thing to work out, from that you can work out how many spaces are needed for each row and how many hashes. Commented Jun 10, 2014 at 21:39
• @user1175, I once was in your place facing troubles building a pyramid so this is familiar. But I can ensure you that over time you'll be familiar with problem solving in general and when you take a look back at this you'll find it pretty simple :) Good luck!
– kzidane
Commented Jun 10, 2014 at 21:45
• Is it possible to nest this? I've tried doing it but I got a whole bunch of errors. Commented Jun 10, 2014 at 21:47
• If you're talking about the for loops, then of course yes! :)
– kzidane
Commented Jun 10, 2014 at 21:49

On lines 15 and 16, you are declaring two variables and initializing them to an empty space.

``````int spaces =
int hashes =
``````

This is very bad coding practice. Also, lines 23 - 27 have no function yet you have a block of code.

When trying to solve mario.c its best to use three for-loops (in my personal opinion) as it is easier to manipulate.

HINT: Have the outer for loop handle the number of rows to print, and have the two inner loops responsible for printing the number of spaces and hashes.

If you have no programming experience, Mario.c can be quite daunting at first but it just requires a little bit of logic applied to it, it teaches you how to think logically and helps prepare you for the upcoming PSETS. Don't give up my friend, keep trying and you will understand it.

• I have some programming experience via codecademy. This is pretty hard and daunting. So the best solution would be three for loops? Can they be 3 separate nested for loops? Thanks for the response and encouragement. Commented Jun 9, 2014 at 20:26
• Yes, they can be three nested for loops. Commented Jun 9, 2014 at 20:44
• Correct, you should have nested for loops for this to work! :) You know, I think you just panicked here. Relax, you can do it - the coding involved actually is not hard, nor daunting; what might be hard at first is to find the right way to think about the problem, put it on paper, and only then write the code. But you need to write the things exactly how they must be - think of a computer like a five-year old. You need to tell it every single bit, and it will take you literally. You can do it :) Commented Jun 9, 2014 at 23:15
• Whenever I try to do nested loops I get errors. I don't know how int works at all. I've done it on paper too. No avail. Commented Jun 10, 2014 at 20:02
• where is the code? Commented Nov 22, 2014 at 19:36

You need to use pen and paper for mario.. think and think about loops..try n make algorithms..you need to use nested loops. loop for changing line, hashes...and be sure to use new line character carefully.. take care of the upperlimits you are using in loops..

It's definitely easier to come at a problem if you understand what your program should do. Imagine that you are telling a child what to do and they are very smart, but they will only do EXACTLY what you tell them.

So, give your self a demo height that's easy and work with that. For example a height of 3. What would you need to do if you were to explain to a child how to draw it using a word processor? How many spaces would you need? How many hashes?

Instead of spaces I'll uses periods (.) so that you can visually see and count them. So, for a pyramid of 3 this is what you would get.

```..##
.###
####
```

Notice that the right side lines up and the first hash on the bottom is at the first spot on the left. So, with a height of 3 how many spaces are there? What is the initial amount of hashes used? How is the amount changed? Notice that the spaces and the hashes are changed differently, so you need to take that into account.

So, as the walkthrough video says, you have a nested loop. The first loop will tell the computer how many lines to print and the nested loop will tell the computer how many times the spaces and hashes are to be printed out.

Good luck with that. Oh, one last thing. If you're still struggling with this, you might consider going to Scratch to do something similar which would help you understand the logic.

As for you code, there are several problems besides the logic errors. If you're going to declare an Int, you can do it one of two ways (from my limited understanding)

The first way is simply declaring it.

Int n;

However, this is not complete was the variable 'n' has no value set to it. The second way to do this is to give it a value at the same time.

Int n = 0;

In your code, you started to declare a variable, but you didn't give it any value, and you also didn't end the line by using a semi-colon (;).

For this code:

``````{
printf("  ");
printf("##");
printf("\n");
}
return 0;
``````

It appears that you are trying to make a function or something as you have "return 0;" at the end. I don't think this is what you want at all.

However, there is no function declared anywhere. You just plunked that down without any conditions or statements. I'm not really certain how the computer will react to that, but it's not good, I'm sure.

The good news is that your code you wrote for the user input seems to be valid. I would throw everything after that part away and start fresh and use some of the ideas that the other posters have given you.

• The return 0 is for the main function. Dont think he was trying to declare one of his own. Commented Jun 29, 2014 at 21:45
• Thanks :-) I realize that now after reading more and coding a bit more. Commented Jun 30, 2014 at 22:09

There are different kinds of very common issues. The first being "how does a for-loop work".

As you have written it, the code prints a space, a hash and a newline for height times.

Instead, for each i iteration, you should write (height - i) spaces and i hashes (not quite so, but I on't want to solve all your issues at once...).

On line 23-27 you put brackets with no block. If you don't have an if(), a for-loop, a do-while or a while() loop, you don't need brackets.

As a homework, try to understand better how for loops work (there are many examples on the video lectures and walkthroughs).

Luigi

• Something I want to add for future responses. I'm not expecting anyone to do the assignment for me, I just need to know if I'm on the right track or if there's anything I need to improve. I've watched weeks 0 through 2, several times now and I'm still not getting it. I just don't know how the loops work besides seeing the outlines and lectures. I can't do much if all examples I see are "Hello, World". I know it's standard in coding with "Hello, fill in the blank". Other websites such as cprogramming doesn't help as well. Maybe I'm a slow learner or like to learn differently. Commented Jun 9, 2014 at 17:23
• Try the prorgam at cprogramming.com/tutorial/c/lesson3.html The for loop with numbers might help you better than hello world. Commented Jun 9, 2014 at 17:30