• 06-22-2012
Jake Garbelotti
Hi, I have to write a program where I give the user "p" random math problems, they decide how many by inputting p. I wrote the program using a for loop that gives them as many problems as they ask for, and ask them to input an answer for each one.

The problem is I need to be able to write a code that calculates how many they got right and then gives a percentage. I have no idea how to do this and any pointers would be greatly appreciated. I calculate the answer to each problem as float "answer" and the user answer for each problem is float "user".

here is my code so far:

Attachment 11759
• 06-22-2012
Jake Garbelotti
BUMP? This is due in an hour :/

Do I have to use a flag or something??
• 06-22-2012
Salem
No, you should copy and paste your code directly from your text editor to your browser edit window.

A screen dump just doesn't cut it.
• 06-22-2012
christop
Why are percentages so hard? This seems to be the second (or third?) thread with the problem of calculating the percentage of problems the user answered correctly.

If you don't know basic math like fractions and percentages, you shouldn't be programming.
• 06-22-2012
Matticus
Quote:

Originally Posted by christop
Why are percentages so hard? This seems to be the second (or third?) thread with the problem of calculating the percentage of problems the user answered correctly...

More than that, it's the same assignment from this post. I wonder if they have the same professor. And I wonder if this person attached a screen shot of their code so it wouldn't turn up in an internet search if a professor got curious.
• 06-22-2012
smokeyangel
Percentage = (number correct / total questions) * 100

So you need to know the total questions (which you do, it is 'p') and the total correct answers. Whether or not an answer is correct can be found by comparing "answer" to "user" for each question.

Code:

```if (answer == user)   num_correct = num_correct + 1; // or num_correct++; if you prefer```
For a beginner I think the tricky bit would be handling the float values correctly, and handling conversions float/int. In your code:

Code:

`answer = x / y;`
answer is a float and x,y are ints. This will just calculate the integer part of x/y. E.g.

15 / 4
We know it's 3.75. If we rounded it we'd get 4. C will give you 3 -- just the int part of the result. It doesn't matter that the lhs of the assignment is a float, what matters is the types in the expression.

In any case - you need to think about how to check the answer is right when the answer is a float. You can't use a straight "==":
If the question is 2 / 3, and the user writes 0.66667, are they right? How close does the value have to be to be correct?
If the question is 61 / 10, and the user writes 6.1, are they right? It is not possible to represent 6.1 exactly in C's floating point representation (much like we can't write 2/3 exactly in decimal), so "==" will fail.

You'll need to decide how accurate the user's answer must be and test that the answer given is within that accuracy tolerance.

Also:
Your program will occasionally divide by 0, since your rand expression can generate 0. With floats, you'll get a defined value back -- but with ints, I think it's undefined (could crash your program). In any case, I think it's impossible for the user to get the question right ;) So you should avoid asking the user this: perhaps a loop to keep regenerating num2 until it's not 0 if op is divide.
• 07-20-2012
Jake Garbelotti
I don't want to be programming, I have to take this class to graduate, sorry sir.

Thanks to all those that helped. Finished the program successfully.