problem with percentage

hey guys
i have a problem with the percentage of my code, it is wrong, can you help me?
Code:

http://pastebin.com/z08HaTsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//programming language "C" ...
 
 
#include <stdio.h>//for all xD
#include <time.h> //for random
#include <string.h> //only 2 be sure
#include <stdlib.h>//for some shit
 
 
int main()
{
   
    srand ((unsigned)time(NULL));
    int max; //maximalnumber
    int maxtasks;//how many tasks
    int tasks;//only int to count the tasks
    int right = 0;//rightanswers
    int wrong = 0;//wronganswers
    int d;
    printf("How many exercises do you want to have?\n");
    scanf("%i",&maxtasks);
    printf("Put in the max of your number: ");
    scanf("%i",&max);
    int a = rand()%max;
    int b = rand()%max;
    int c = a + b;
    printf("%i + %i = ",a,b);
    scanf("%i",&d);
    if (d == c)
    {
        printf("right\n");
    }
    else
    {
        printf("wrong\n");
    }
    for (tasks=1;tasks < maxtasks;tasks++)
    {
        a = rand()%max;
        b = rand()%max;
        c = a + b;
        printf("%i + %i = ",a,b);
        scanf("%i",&d);
        if (d == c)
        {
            printf("right\n");
            right += 1;
        }
        else
        {
            printf("wrong\n");
            wrong += 1;
        }
    }
    double percentright = 100 / maxtasks * right;
    printf("You have %lf percent right\n",percentright);
    return 0;
}

You' re using int multiply and divide where you should be using double for youe calculation.
http://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c

Lets assume maxtasks = 3 and right = 3:
100 / 3 * 3 = 33.(3) * 3 = 33 * 3 = 66


Use floating point division here.
Topic archived. No new replies allowed.