Student grade sort program

Write your question here.

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
  /* This Program takes the average of a user's test

    after dropping the lowest test score.
    You can enter as many scores as you like.
    Testscore Average
*/


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

void getScore(int&, float&, double *); // stores the testscores
void calcAverage (double&,int, float); // used to find the average test scores

void selectionSort (double *, int); // sorts the test scores in ascending order
void showArray(double *, int);

int main ()
{
    float Total;
    int numTest;
    double average;
    double *score;
    cout << fixed << showpoint << setprecision(2);


	getScore(numTest, Total, score); //asks user to input their test data
	selectionSort(score, numTest); //sorts the testscores
	calcAverage(average,numTest,Total); // finds the average
    showArray(score, numTest);


return 0;
}
void getScore(int &numTest, float &Total, double *score)
{
// Gets the number of test scores
	cout << "How many test scores are you entering? ";
	cin >> numTest;

    score = new double[numTest];

	Total=0;
	int test;

	for (test =1; test <= numTest; test++)
	{
        //Gets the tests scores using the number entered
		cout << "Enter test " << test<< " score now: ";
		cin >> *score;

            // validates score entries
			if ( *score < 0 || *score > 100)
			{
					cout << "\nYou have entered an invalid number. \n\n"
                        <<"Re-Enter: ";
			    cin >> *score;
			}

    //Gets the Total of all the scores
	Total+=*score;

	}
}





//**************************************************************
// Definition of function selectionSort. *
// This function performs an ascending order selection sort on *
// array. size is the number of elements in the array. *
//**************************************************************

void selectionSort(double *score, int numTest)
{
    int startScan, minIndex;
    double minValue;

    for (startScan = 0; startScan < (numTest - 1); startScan++)
    {
        minIndex = startScan;
        minValue = *(score + startScan);

        for(int index = (startScan + 1); index < numTest; index++)
        {
            if (*(score + index) < minValue)
            {
                minValue = *(score+ index);
                minIndex = index;
            }
        }
    *(score + minIndex) = *(score + startScan);
    *(score + startScan) = minValue;
    }
}

void calcAverage (double& average,int numTest, float Total)
{
    void getScore(int numTest, float Total, int *score);
    void calcAverage (double average);

    //finds the average testscore
	average = (Total) / (numTest);
	cout << "\nThe average score is: "<<average<<". \n";
}
void showArray(double *score, int numTest)
{
    for (int count = 0; count < numTest; count++)

    cout <<"#" <<(count +1)<<": "<< *(score + count);
    cout << fixed << showpoint << setprecision(2);
    cout << endl;
}


Writing a program to sort grades after they've been entered and find the average. The sorting part of the program does not work. Any help will be appreciated

After doing some rewriting I got it to run, however the sorting portion is not working. I would like to fix the sorting without writing an array. That would mean re-writing the whole program
Last edited on
> The sorting part of the program does not work
The sorting part does work.
In fact, it's the only function that's correct.
- getScore() writes only on the first position of the array. Also, it leaks memory. (this is your biggest culprit)
- showArray() uses the stream manipulators too late. Also, you should add some spacing between the elements
- calcAverage(): I would expect this function to return a value. Still, ¿why isn't Total computed here instead of in getScore()?
- main(): you forgot to drop the lowest score
getScore() - does what I asks it to do and prints the correct results
showArray() - the print part is just to display the results it wasn't the final version
calcAverage() - also does what I want

I took out the get lowest portion that was part of a different program I wrote
it's only PrintTest that doesn't print the sorted input numbers. I'm trying to do this without rewriting everything to call arrays.

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
/* This Program takes the average of a user's test

    Sorts the entered scores in ascending order
    You can enter as many scores as you like.
    */


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

void getScore(int&, int&, double *); // stores the testscores
void calcAverage (float, int, int &); // used to find the average test scores

void selectionSort (double *, int); // sorts the test scores in ascending order
void PrintTest(double *, int); // Prints testscores in ascending order

int main ()
{
    int Total; // holds the sum of all scores
    int numTest; // holds the amount of scores the user will enter
    double average; // holds the average of all scores
    double *score; // dynamically holds all scores

    // Gets the number of test scores
	cout << "How many test scores are you entering? ";
	cin >> numTest;

    // Dynamically allocates an array large enough
    // to hold the user-defined number of scored
    score = new double[numTest];
    cout << fixed << showpoint << setprecision(2);


	getScore(numTest, Total, score); //asks user to input their test data
	selectionSort(score, numTest); //sorts the testscores
	calcAverage(average,numTest,Total); // finds the average
    PrintTest(score, numTest); // Prints Test scores in ascending order


return 0;
}
void getScore(int &numTest, int &Total, double *score)
{

	Total=0;
	int test;

	for (test =1; test <= numTest; test++)
	{
        //Gets the tests scores using the number entered
		cout << "Enter test " << test<< " score now: ";
		cin >> *score;

            // validates score entries
			if ( *score < 0 || *score > 100)
			{
					cout << "\nYou have entered an invalid number. \n\n"
                        <<"Re-Enter: ";
			    cin >> *score;
			}

    //Gets the Total of all the scores
	Total+=*score;

	}
}



//**************************************************************
// Definition of function selectionSort. *
// This function performs an ascending order selection sort on *
// array. size is the number of elements in the array. *
//**************************************************************

void selectionSort(double *score, int numTest)
{
    int startScan, minIndex;
    double minValue;

    for (startScan = 0; startScan < (numTest - 1); startScan++)
    {
        minIndex = startScan;
        minValue = *(score + startScan);

        for(int index = startScan + 1; index < numTest; index++)
        {
            if (*(score + index) < minValue)
            {
                minValue = *(score + index);
                minIndex = index;
            }
        }
    *(score + minIndex) = *(score + startScan);
    *(score + startScan) = minValue;
    }
}


//Prints the test scores in ascending order

void PrintTest(double *score, int numTest)
{
    cout<<"The test scores in ascending order are. \n\n" <<"Score \n" <<"----- \n\n";
    for (int count = 0; count < numTest; count++)
    {
     cout << *(score + count)<<endl; // displays test scores in ascending order
    }

    cout << fixed << showpoint << setprecision(2);

}



//Calculates the average test score

void calcAverage (float average,int numTest, int& Total)
{
    void getScore(int numTest, int Total, int *score);
    void calcAverage (double average);

    //finds the average testscore
	average = (Total) / (numTest);
	cout << "\nThe average score is: "<<average<<". \n";
}


This is the version I currently have, I changed the order around a bit since I seem to be getting an allocation error when it tried to print selectionSort

I just remembered. I was having the same issue with the original Average program and rewrote it using a while loop. I'm going to rewrite the printTest portion and see if it works.
Last edited on
getScore() writes only on the first position of the array, (that is, score[0] or *score if you prefer)
Yep I understand that. That was the same issue I had with getlowest until I rewrote it using a while loop. Right now I'm trying to figure out how to do the same for selectionsort.

Its only giving me the last item entered like my getlowest program before I used a while loop in place of the for loop.

Its either that or rewrite everything using arrays
When I fix the problems with getScore(), the code works for me.

I suggest that you add code to temporarily call PrintTest() right after you call getScore() to you can see the contents of the score array. There's no point trying to debug the selectionSort until you're certain that the scores are correct to begin with.
Topic archived. No new replies allowed.