Hot answers tagged

43

First of all, kudos for debugging your program and identifying the problem as having to do with multiplying your input. Let's take a look at that part of your code step by step, to illustrate what's going wrong. change = GetFloat(); At this point, we're going to enter 4.2; let's make sure we see very precisely what number gets stored by printing out the ...


19

An array is not strictly required to solve this. You already have the mechanism to extract the last digit from a number. You just need to extend this by using use division to "shift" the digits the required number of places. For example, if you have the number 1234, to get the second digit you can divide by 10, to get 123. Using 123 mod 10 will give you ...


15

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

Variables can be declared and initialized in different ways. Variable Declaration There are two common methods for declaring variables. Suppose we want to declare two int variables, x and y Method 1: int x; // declare x int y; // declare y Method 2 int x, y; // declare x and y Notice that method 2 works only if x and y (and probably more if we want) ...


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

You are given a value of dollars and cents. You must calculate the least number of coins that can be used to make that value. The coins available are worth 25 cents (quarter), 10 cents (dime), 5 cents (nickel), and 1 cent (penny). Let's say you are asked for 1.32 1.32 is 132 cents. So how many coins would you need? To get the least number of coins, you ...


6

Your code isn't working for the same reason that this code: int x = 5; x + 5; printf("%i", x); does not print the number 10. In C, (int) is a type casting operator; much like the + operator, it does not modify a variable; it creates an expression, which is evaluated and replaced by its result. So the above code is equivalent to: int x = 5; 10; printf("%...


6

If you want to store each digit of a number as an element of an array you can do the following. long long number = 1234567890; int digits[10]; for (int i = 0; i < 10; i++) { digits[i] = number % 10; number = number / 10; } // At the end digits = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1} But if you want the elemnts to keep the same order than the original ...


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


6

Add a new line at the end of your printf string like that: printf("%d\n", count);


6

You'll want to use the round function on that amount*100. Given that it's simply a matter of syntax, I don't mind just telling you. It's as simple as int rounded_number = round(other_number*100); (using your own variables, of course) Be sure to #include the library where the round() function is defined. I'll leave that to you to find. Hint: reference....


6

This is a very common problem and the whole point of greedy.c, so let me try and give the definitive answer. Float values with a decimal part (to the right of the decimal, in case you weren't certain) rarely store with exact precision. They have to be converted from base 10 to base 2. Since there is a limit on the number of digits that can be used, it is ...


5

If statements need to have a body like so: if (statement) { // Body } or if (statement) // Body Note that in the second case, the body is strictly one line long, while the first case allows you to use as many lines inside the curly brackets as you wish. Your statement is incorrect too. 0 <= tower_height and tower_height <= 23 would both be ...


5

Google search of expression result unused will lead to 2 links : This It seems that you have written the number in a wrong way. Have a look at it, you are not supposed to write commas in the numbers. i.e. int i = 50,000 is wrong way and should be written as int i = 50000. Remove any such commas from your program. This Although the link is of for loop but ...


5

Use round() function of math.h. #include <stdio.h> #include <cs50.h> #include <math.h> int main(void) { float f = GetFloat(); int value = round(f * 100); printf("%d", value); } For input: 4.2 Output: 420


5

it takes a maximum of 2 weeks to get your pset submission graded. usually it is graded much faster. If you've been waiting for more than 2 weeks though, you may email Robert Bowden at rob@cs.harvard.edu. your email should probably contain your edX name and email address along with details about the psets in question!


5

You should use the round() function to round your float to the nearest int! Casting a float to an int truncates all the digits after the decimal point. Also, 4.2 isn't necessarily stored as 4.2 because floats are not precise.


5

The solutions to the hacker edition problem sets are not submitted nor graded. They're optional!


5

You can use arithmetic to loop through the numbers. In your example: 12345686868 % 10 = 8 which leads on to 12345686868 / 10 = 1234568686 so 1234568686 % 10 = 6 You can carry this on to access to all the numbers in the credit card.


5

Here's some pseudo code: int accumulator = 0 for each digit in the card number { if the digit is at an odd position (position is not divisible by 2) { int factor = multiply the digit x 2 for each digit in factor { add factor digit to accumulator } } } Given your example digits 1234567, this will do ...


5

You wrongly named your file, before issuing make command with your file you named practice, rename it to practice.c then compile and run it. Here are the steps you need to follow rename the file using the command mv practice practice.c compile your program by issuing the command make practice finally run it by issuing the command ./practice in case 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

Did you follow the instructions in pset1 that said to run update50 first? There's an update that will fix your gedit built-in terminal to use the correct compiler commands. Also, jharvard@ubuntu is perfectly fine.


5

True that check50 is strict about the output. Here is what the instructions say Incidentally, so that we can automate some tests of your code, we ask that your program’s last line of output be only the minimum number of coins possible: an integer followed by \n. The problem in your output is not the newline, which is correct. It is the space between ...


5

As far as I know, there's only 2 things needed to compile programs that use the code examples from the CS50 course. The cs50.h header file, and the cs50 library (linked when -lcs50 is passed to the compiler). If you have those, I expect that you'd be able to build your programs using MinGW, which you can download for free. You wouldn't be using the CS50 ...


5

Hmmm, this looks like a unique problem. ;-) check50 expects very exact output. That means that any missing or extra content, like whitespace, prompts, punctuation, characters, spaces, or anything else will almost always cause a fail. In looking at the output, I'm thinking that the problem is the spaces that are printed after the last # and before the ...


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