SOLVED

SOLVED
Last edited on
I debugged your code, it appears that when you enter 2.2 and 3.2 into the array, what is instead being inputted is 2.0 and 3.0, so it will be the median of (2 + 3) / 5 which is 2.5. I have no idea why this happens, hopefully someone can shed some light.
If we add a little output in, it becomes clear at what stage the error occurs:

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
#include <iostream>
using namespace std;

int getnumGrades();
void getGrades(double grades [], int size);
void selectionSort(double grades [], int size);
double findMed(double grades [], int size);

void showGrades(double grades [], int size)
{
    for (int i = 0; i < size; ++i)
        std::cout << grades[i] << ' ';
    std::cout << '\n';
}

int main()
{
    double grades[20], size;

    size = getnumGrades();

    getGrades(grades, size);

    std::cout << "Before sort: ";
    showGrades(grades, size);

    selectionSort(grades, size);

    std::cout << "After sort: ";
    showGrades(grades, size);

    double median = findMed(grades, size);

    cout << "The median grade is " << median << endl;

    return 0;

}

int getnumGrades()
{
    int numGrades;
    cout << "Please enter the number of grades" << endl;
    cin >> numGrades;


    return numGrades;
}

void getGrades(double grades [], int size)
{
    int i;

    cout << "Please enter your grades" << endl;

    for (i = 0; i < size; i++)
    {
        cin >> grades[i];
    }
}
Please enter the number of grades
4
Please enter your grades
1
2.2
3.2
4
Before sort: 1 2.2 3.2 4
After sort: 1 2 3 4
The median grade is 2.5
Press any key to continue . . .


If we then look at your sort implementation, we see that you are using an int to hold a double value in the variable minValue. You probably didn't do anything about the warning generated there, since you ignored all the other warnings for the same issue with your inappropriately typed size variable in main.

Don't disregard warnings.
Topic archived. No new replies allowed.