Program that finds average of test after dropping lowest

Why do I get a weird number for the average after I drop the lowest test grade?

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
// Average of test scores
//Gaddis 7th edition number 3

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

 //Function prototypes
 void sort(float *, int);
 void sortAry(int *, int);
void minPos(int *,int ,int);
void swap(int *, int*);

//Eecution begins here
 int main()
 {
	 //Declare variables
 int *testScores, 
 total = 0;
 float average; 

 int numTest, 
 const count=0; 

 //Input
 cout << "How many test scores are you entering? ";
 cin >> numTest;


 //Allocte array
 testScores = new int[numTest];

 cout << "Enter the test scores below.\n";
 for (int count = 0; count < numTest; count++)

 {
 cout << "Test Score " << ": ";
 cin >> *(testScores + count);
	if(*(testScores + count)<0)
	{
cout << "Test score cannot be negative, please try again" << endl;
 count --;
	}
 }
  cout << endl;
 //Adding the test Scores
  total=0;
 for (int count = 0; count < numTest; count++)
 {
 total += *(testScores+count);
 }


 //Calculate the average test scores
 average = total / numTest;


 cout << "The average of all the test scores is " << setprecision(2) <<
	 fixed << average << endl;
 
 cout << endl;

 
 //Display the Test Scores in ascending order
 cout << "The test scores in ascending order are: ";

 sortAry(testScores, numTest);
 for(int i=0; i<numTest; i++)
 {
	 cout << *(testScores+i) << " ";
 }
 cout << endl;
 cout << endl;
 cout << "After dropping your lowest grade which was " 
	 << *testScores << ", Your average is now: ";
 
  for (int i = 1; i < numTest; i++)
 {
 total += *(testScores+i);
 }
  float average2=0;

 //Calculate the average test scores
 average2 = total / numTest;

 cout << average2 << "%" << endl << endl;;


 delete [] testScores;
 testScores = 0; 
 system("PAUSE");
 return 0;
 }

 //Sort the array
 void sortAry(int *testScores,int n){
   
    for(int i=0;i<n-1;i++){
        minPos(testScores,n,i);
    }
}

void minPos(int *testScores,int n,int pos){
	

    for(int i=pos+1;i<n;i++){
        if(*(testScores+pos)>*(testScores+i))
            swap(testScores+pos,testScores+i);
	}
    }

	void swap(int *a,int *b)
	{
    //Swap in place
    *a=*a^*b;
    *b=*a^*b;
    *a=*a^*b;
	}
Last edited on
Reset total to zero before for(int i = 1; i < numTest;i++). And divide by (numTest-1).
Topic archived. No new replies allowed.