Hot answers tagged

14

This is a very common question asked by new students. The make command is used to compile the source code and generate an executable program. While there are complexities to this process (more explanation in later lessons), this answer will give a simple 'how-to' for the early lessons so that you can get started. To use make to build your executable, the ...


12

Five months is too long for a problem like mario 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... ## ### #### ##### ###### ...


8

In a for loop, the first item is the initialization. You are saying for (height; height>0; height--) and the compiler is expecting that first item to be an initialization but you have simply given it a variable. There are 2 ways around that. One way: for (int i = height; i>0; i--) and then, in your inner loop, use i rather than height. Or, ...


7

I can not seem to make a pyramid, only a straight line of #'s. In order to make a pyramid, you'll need more than one line. The way to move to the next line when printing out characters to the terminal/console is by printing a special character: \n, the newline character. I am stuck on my loops. should I have a single for loop that the #'s and the space's ...


6

You are validating the input after they enter it. So your program asks for input using GetInt(), which it sounds like you have figured out. and then you write a piece of code (a loop might be pretty good for this) that says something to the effect of: If the value isn't within this range that I specify (perhaps greater than or less than but how you ...


6

Well, you wanted some tips, so here are a few thoughts on it. You know that you have to create the correct number of rows, you have that part. Now, you need to think about how to create the spaces and hashes. Well, break it down into smaller and smaller parts. You need to think about how to build a given row. OK, that's a smaller part. What is a row? It'...


5

You have the right idea behind how the code works, it's just your English to C needs some practice (understandably). int main(void); This is, as its name suggests, the "main" method. Essentially, when you say to run the program, it looks for main(void) and goes from there. Therefore, all your other code should be inside this function, Which will take care ...


5

If the white space on the screen intimidates you, try outlining the problem on scrap paper. Think in broad terms, and describe what you're going to do in plain English before you write the code. Think in very small steps or instructions. Then try to figure out the code for one of those instructions. You might try doing a simple math calculation and printing ...


5

With your usage of int you're creating a new variable. Example: int blocks = 5; {// Just creating a block, similar to a loop int blocks = 4; //Variable inside the block printf("%d\n", blocks); // Prints 4 } printf("%d\n", blocks); //Variable outside block / prints 5 The int blocks = 4 variable lives only inside of the block {}, it's inside the block'...


5

What conclusion can be drawn from these two facts? This loop for (row = 0; row <= height; row++) will generate height + 1 lines of output (0 through height inclusive) The first output line (ie row = 0) is incorrect. Technically,when row is 0, the spaces loop executes height times, and spaces is height + 1 when done, therefore the hashes loops executes 0 ...


4

Before you post a question, you should search to see if your question or a similar one had already been asked, in which case yours is. Check 50 returns unexpected end of input I suggest that you try to include return 0 at the end of your code and try again, if that doesn't work, try running check50 again in a couple of hours.


4

I found a small but fatal glitch in your code. Have a look on you first loop in output part for (tower_height = 0; tower_height <= 23; tower_height ++) A moment before this line in your code, you promted the user to Input height of tower and stored it in the variable tower_height. And here in for loop you simply initialized it with, tower_height = 0. ...


4

The syntax for do-while loop goes like this do { //do something here } while(test_condition); It is better to use braces {} every time you use loops. Although loops work even without their use, but using braces provide readability to your code and avoid such confusions. So you should do this instead of what you mentioned. do { n = GetInt(); ...


4

You are printing one extra space character each row.


4

The value of s is increasing but the number of spaces it prints will be decreasing because you are inside the outer loop. Take your program and add some debug print statements like this: #include <cs50.h> #include <stdio.h> int main(int argc, string argv[]) { int line, s; int height = 10; for (line = 0; line < height; line++) ...


4

This line causes it: for (spaces = height - rows; spaces <= height; --spaces). While spaces is less than height, print a space and make spaces smaller. It is an infinite loop.


4

check50 is expecting the last # on a line to be followed by the newline. It is complaining about the trailing spaces.


3

Your condition is flawed. If x is greater than 0 OR x is less than 23, the loop will keep going. x > 0 includes all positive numbers, and x < 23 includes all negative numbers in addition to positive numbers up to 23. There are no integer values for which both would not be true, and thus you will forever be in that loop. Remember, || (OR) means if ...


3

Running the program: check50 should only be used to check your program before submitting it. To test your program while you're working on it, first run make to compile the program, then run mario to run it: jharvard@appliance (~/Dropbox/pset1): make mario # lots of messages appear here jharvard@appliance (~/Dropbox/pset1): ./mario This will ...


3

You've declared your integer height inside the curly braces, so it is no longer in scope (no longer in existence) once you reach the end of the curly brace. I'm assuming you've also declared it before this bit of code you are showing, and it will be that height that is being compared. Inside the do, simply set the height without declaring it again.


3

check50 is not wrong! Please check this question for more information about check50. Make sure you're in the same directory as mario.c and this is the command you run to check your program with check50 check50 2014/x/pset1/mario mario.c Also, your input must match the one required by the pset specification page. Here's a sample output per the pset ...


3

If you use printf(" ") and printf("#") they will print one after another, when you need to go to the next line, just printf("\n")


3

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 ...


3

From the comments above, it seems that you may have solved this already by using GetInt() from the cs50 library (and you should use it of course). But just if you're curious about why that happened, here's what was causing the infinite loop problem. Per the manual page for scanf() The format string consists of a sequence of directives which describe ...


3

This error is because of the missing braces '{}'. Languages like python compile the code based on the indenting. However, C requires braces around do-while to compile correctly. Your code doesn't allow the compiler to reach the while statement. Also, C needs braces around a set of instructions you wish to execute inside a conditional statement or loop, if ...


3

Change it to #include <stdio.h> You've got a typo.


3

In C all variable names have to be declared before they are used. If you try to use the name of a variable or a function that hasn't been declared you will get an "undeclared identifier" error. You issue deals with scoping. To fix your issue you need to declare the variable height before the while lope. Your code should be the following: int height; do {...


3

The while condition in a do while loop is the condition that causes the loop to repeat. do { //this } while (this is true) Can you see why yours is incorrect? You have used the opposite condition.


3

the pseudocode for Mario for height of pyramid for (calculate nr. of spaces at height) print ' ' for (calculate nr. of hashes at height) print '#' print a newline I'd defer from saying more since, well, that would kill all the fun of the problem :) Note that you've got other problems, too... your conditions aren't correct. You ...


3

If it is asking for the number again after building the pyramid, then you have a structure problem. Most likely, you didn't end the loop before starting to build the pyramid, or you've put the whole program in a loop. You need to have a while loop that only asks for, stores, and validates the input number. Once it has a number that is valid, the loop ends ...


Only top voted, non community-wiki answers of a minimum length are eligible