43
votes
Accepted
Greedy.c works for all numbers except 4.2
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 ...
20
votes
Accepted
Finding the nth digit for credit card number
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 ...
17
votes
Accepted
Error: "make: *** No rule to make target `mario'. Stop."
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 ...
12
votes
What's wrong with my code for mario?
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 ...
8
votes
Accepted
How to Declare and Initialize Variables in C?
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 ...
8
votes
Accepted
Problem with "expression result unused" error in for-loop
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 ...
8
votes
How to convert the digits of a number into elements of array?
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] = ...
8
votes
Can't submit or check
Had similar problem with similar message. This is how we found out what was wrong:
Executed check50 -h to see what logging options there were. Discovered we can turn on debug logging using --log-...
7
votes
Accepted
Stuck on pyramid for mario.c
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 ...
6
votes
How to Fix the "expression result unused" Error?
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....
6
votes
Completely lost.. pset1, Mario help
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 ...
6
votes
How to approach greedy in problem set 1?
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 ...
6
votes
permission denied
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 ...
6
votes
Accepted
Can't cast a float to an int?
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 ...
6
votes
Tips on spaces and hashes Mario
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 ...
6
votes
Accepted
pset1 greedy.c problem
Add a new line at the end of your printf string like that:
printf("%d\n", count);
6
votes
Accepted
Difficulty rounding on Greedy in pset1
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); ...
6
votes
Accepted
pset1 - "greedy" failing at one value
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)...
6
votes
Accepted
Check50 and style50 not working: pset1
So apparently markupsafe was upgraded and soft_unicode does not exist in it anymore. I downgraded markupsafe to 2.0.1 and it fixed the issue for me:
pip install markupsafe==2.0.1
5
votes
Accepted
How do I fix the precision error for convering float to int?
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", ...
5
votes
How long to wait for being graded
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....
5
votes
Accepted
Can I write an "if" statement without an associated code block?
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 ...
5
votes
Accepted
Why does an input of 4.2 yield an output of 22 not 18?
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 ...
5
votes
Accepted
Hacker pset1 How to submit it?
The solutions to the hacker edition problem sets are not submitted nor graded. They're optional!
5
votes
Finding the nth digit for credit card number
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 ...
5
votes
Finding the products' digit and adding them individually
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 ...
5
votes
Accepted
How to debug "initialized element is not a compile-time constant" and "expected identifier" errors
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, ...
5
votes
How to approach Problem Set 1 Mario!
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 ...
5
votes
How do I get the cs50 appliance library to work
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 ...
5
votes
Accepted
Convert from dollars to cents
The main problem is how you are storing the initial retrieved value. You are calling GetFloat() and storing the result in owed, a variable of type int. GetFloat() returns a floating point number, ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
pset1 × 1156mario × 354
greedy × 223
credit × 95
cs50x × 86
check50 × 80
cash × 76
c × 59
error × 36
mario.less × 25
cs50-pset1 × 20
water × 17
hacker1 × 16
water.c × 16
cash.c × 16
credit.c × 15
pyramid × 14
round × 14
for-loop × 13
appliance × 12
cs50-ide × 12
loop × 12
less × 12
float × 11
hello.c × 11