How to print the list of sorted score?

Hello, everyone

I want all scores show like xx.x

For example, 78.8 instead of 78, 64.5 instead of 64, and so on.

An early replay would be appreciated!


Here is the requirement:

The median score is generated after sorting the list of final scores.
At the end, your program should print
the list of scores sorted from lowest to highest,
the class average and the median score. 



Here is the output:
--------------------------
    Name	Average
--------------------------
     Ana	88.5
Beatrice	74.9
  Bethan	82.8
    Juan	95.6
  Joanne	68.6
 Patrick	86
    Rita	89.2
   Suzan	72.8
 Wallace	95.3
  Wesley	65.8
  Amanda	64.5
    John	74.5
--------------------------

**************************
Following is the sorted average:
64 65 68 72 74 74 82 86 88 89 95 95.6 
The class average is 79.4
The class median is 78
**************************


Here is my code
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// C++ Program
// This program can show the average score for each student
// Date 11/05/2015
// *************************************************************

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

// function prototype
double calcAverage (double [], int);
double calcMedian  (double [], int);
void selectionSort (double [], int);

int main()
{
    ifstream infile;
    string filename;
    const int MAX_SCORES = 10;
    const int MAX_STUDENTS = 12;
    string stuName[MAX_STUDENTS];
    double final[MAX_STUDENTS];
    double scores[MAX_SCORES];
    double classAverage;
    double classMedian;
    double sort;
    int count = 0; // loop counter

    // open file
    cout << "Enter filename: ";
    cin >> filename;
    infile.open(filename.c_str()); 
    cout << "\n";

    if(infile)
    {
        while (infile >> stuName[count])
        {
            for(int i = 0; i < MAX_SCORES; i++)
            {
                infile >> scores[i];
            }
            final[count] = calcAverage(scores, MAX_SCORES); // store average to fianl array
            count++;
        }


        cout << "--------------------------\n";
        cout << setw(8) << "Name" << "\t" << "Average\n";
        cout << "--------------------------\n";
        for(int k = 0; k < MAX_STUDENTS; k++)
        {
            cout << setw(8) << stuName[k] << "\t" << final[k] << endl;
        }
        cout << "--------------------------\n\n";

        cout << "**************************\n\n";
        selectionSort (final, MAX_STUDENTS);
        cout << "Following is the sorted average:\n";
        for(int i = 0; i < MAX_STUDENTS; i++)
        {
            cout << setprecision(3) << final[i] << " ";
        }

        classAverage = calcAverage(final, MAX_STUDENTS);
        cout << "\nThe class average is " << setprecision(3) << classAverage << endl;
        classMedian = calcMedian(final, MAX_STUDENTS);
        cout << "The class median is " << classMedian << endl;
        cout << "**************************\n\n";
    }
    else // display a error message if file does not open
    {
        cout << "Error! Please enter correct filename with file type";
        cout << "such as lab5.txt\n";
    }
    return 0;
}

double calcAverage(double array[], int MAX)
{
    double total = 0;
    for(int i = 0; i < MAX; i++)
    {
        total += array[i];
    }
    return (total / MAX);
}

double calcMedian(double array[], int MAX)
{
    int a, b;
    double median;
    selectionSort (array, MAX);
    if(MAX % 2)
    {
        a = MAX / 2;
        median = array[a];
    }
    else
    {
        a = MAX / 2;
        b = a - 1;
        median = (array[a] + array[b]) / 2;
    }
    return median;
}

void selectionSort(double array[], int size)
{
    int startScan, minIndex, minValue;

    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minValue = array[startScan];
        for(int index = startScan + 1; index < size; index++)
        {
            if(array[index] < minValue)
            {
                minValue = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minValue;
    }
}


OK, I figured it out on my own, just need to change the type the variable.

1
2
3
4
5
6
7
8
void selectionSort(double array[], int size)
{
    int startScan, minIndex;
    double minValue; // change int to double will work!

void selectionSort(double array[], int size)
{
    int startScan, minIndex, minValue; // here is the origin.  


Last edited on
line 38: finalScore = calcAverage(sumOfScore);


calcAverage expects 2 arguments not 1.

edit:
also finalScore = total / size; might not yield desired results.
how do you calculate size?
Last edited on
I only changed
finalScore = calcAverage(sumOfScore);
to
finalScore = calcAverage(sumOfScore, MAX_SCORES);

changed
1
2
3
4
5
6
7
8
9
10
double calcAverage(double a[], int size)
{
    double finalScore, total = 0;
    for(int k = 0; k < size; k++)
    {
        total += a[k];
        finalScore = total / size;
    }  
    return finalScore;
}

to
1
2
3
4
5
6
7
8
9
10
double calcAverage(double score1[], int SIZE)
{
    double finalScore, total = 0;
    for(int k = 0; k < SIZE; k++)
    {
        total += score1[k];
        finalScore = total / SIZE;
    }
    return finalScore;
}


and keep the rest.



Here is what I got
error: no matching function for call to 'calcAverage'
note: candidate function not viable: no known conversion from 'int [10]' to 'double *' for 1st argument.
Last edited on
finalScore = calcAverage(sumOfScore, MAX_SCORES)

first argument is supposed to be an array of double[] not int, you are passing in an int

your declaration should look like this:
1
2
    double score[MAX_SCORES];
    int sumOfScore;


what does finalScore = total / SIZE; do inside for loop? what are you calculating here? an average or meaningless number?

please move finalScore = total / SIZE; outside of for loop to calculate an average:
1
2
3
4
5
6
7
double calcAverage(double score1[], int SIZE)
{
    double total = 0;
    for(int k = 0; k < SIZE; k++) total += score1[k];

    return total / static_cast<double>(SIZE);
}
Last edited on
code kiddy, Thank you very much ;)
Last edited on
Topic archived. No new replies allowed.