Float not working.

Why is the median function returning integer number?
The average function works just fine.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cmath>

using namespace std;

float average(int*, int);
float median(int*, int);
int mode(int*, int);

int main()
{
    int num;
    float a;
    int b, c;
    cout << "Number of students surveyed: ";
    cin >> num;
    int *student;
    student = new int[num];
    for (int i = 0; i < num; i++)
    {
        cout << "Number of movies student number "<<i+1<<" saw: ";
        cin >> student[i];
    }
    a = average(student, num);
    b = median(student, num);
    c = mode(student, num);
    cout << "The average is "<<a<<endl;
    cout << "The median is "<<b<<endl;
    cout << "The mode is "<<c<<endl;
}

float median(int student[], int num)
{
    int mid = num/2;
    if ((num%2)==0)
    {
        return (student[mid-1]+student[mid])/2;
    }
    else
        return student[mid];
}
float average(int student[], int num)
{
    float avg;
    float temp;
    for (int i = 0; i < num; i++)
    {
        temp = temp + student[i];
    }
    avg = temp/num;
    return avg;
}

int mode(int student[], int num)
{

    int counter = 1;
    int max = 0;
    int mode = student[0];
    for (int i = 0; i < num - 1; i++)
    {
        if ( student[i] == student[i+1] )
        {
            counter++;
            if ( counter > max )
            {
                max = counter;
                mode = student[i];
            }
        }
        else
        counter = 1;
    }
    return mode;
}
Sample output:

Number of students surveyed: 4
Number of movies student number 1 saw: 1
Number of movies student number 1 saw: 1
Number of movies student number 1 saw: 2
Number of movies student number 1 saw: 3
The average is 1.75
The median is 1
The mode is 1


I know it's supposed to be sorted first. I'm just confused in why the float is not returning decimal values.
Last edited on
In median you are only doing operations with integers, so by the time you return your result, it is still an integer, which is then converted to a float:

1
2
3
4
5
6
7
8
//An example:
float func() {
   return 5/2; //these are both integers so you get a result of 2, which is then converted to a float
}

int main() {
   float x = func(); //x is 2, not 2.5
}
Is it because the array is integer type?
1
2
3
4
5
6
7
8
9
10
11
12
13
float median(int student[], int num)
{
    int mid = num/2;
    float temp = 0, avg;
    if ((num%2)==0)
    {
        temp = temp + (student[mid-1]+student[mid]);
        avg = temp/2;
        return avg;
    }
    else
        return student[mid];
}


So I tried this and it still won't work. Where am I wrong?
Hm, that looks like it should work to me. Have you tried stepping through it with a debugger to see what the values of the variables are?
The problem is that variable 'b' is of type int.
return (student[mid-1]+student[mid])/2;

you should divide by 2.0 here to avoid integer division.
I found the problem!
It turns out that my 'b' variable is of int type, not float type, as defined on top. That's why it always displays an integer.

Sorry for taking your time!
Topic archived. No new replies allowed.