Help with comparing answer to math problem and user's answer

This is a discussion on Help with comparing answer to math problem and user's answer within the C Programming forums, part of the General Programming Boards category; Hi, I have to write a program where I give the user "p" random math problems, they decide how many ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    Help with comparing answer to math problem and user's answer

    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:

    Name:  1034276-TZSMMHC.jpg
Views: 615
Size:  35.5 KB
    Last edited by Jake Garbelotti; 06-22-2012 at 12:20 PM.

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    BUMP? This is due in an hour :/

    Do I have to use a flag or something??

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,554
    No, you should copy and paste your code directly from your text editor to your browser edit window.

    And please remember to use [code][/code] tags around your code.

    A screen dump just doesn't cut it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    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.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    Quote Originally Posted by christop View Post
    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.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    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.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numbering questions that the user wants to answer.
    By KumarSingh in forum C Programming
    Replies: 7
    Last Post: 06-20-2012, 03:23 PM
  2. an answer raises another problem...
    By Dorky King in forum C Programming
    Replies: 2
    Last Post: 06-12-2007, 03:11 PM
  3. stupid problem. answer = 0.0
    By spydrvnm in forum C Programming
    Replies: 6
    Last Post: 09-26-2004, 10:53 AM
  4. problem with answer going to default?
    By Patrick1234 in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2002, 09:11 AM
  5. slight problem just can't see the answer
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2002, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21