I am trying to write a program that takes a user submitted number of test, along with the test scores. Then I need to sort them in ascending order. (I've done all this). Now I need to get the average, but my getAvg function drops will drop the decimal and give me a whole number. What gives? Any help at all will be much appreciated.
#include <iostream>
using namespace std;
void bubSort(int [], int);
int getAvg (int[], int, int);
int main() {//start main
int *scores; //dynamically allocate an array
int numTest; //hold number of test
int total=0;
float average;
int x; //hold open window
int count; //use to count through for statement
cout << "How many test scores are there? " ; //obtain how many test scores
cin >> numTest;
scores = new int[numTest]; //dynamically allocate an array large enough to hold that many test scores
cout << "Enter the test scores: " << endl;
for(count = 0; count < numTest; count++) //Obtain test scores
{
cout << "Test " << (count + 1) << endl; //Gets test score for each test taker
cin >> scores[count] ;
}
cout << endl;
bubSort(scores, numTest);
cout << "Sorted Test" << endl;
for (count = 0; count < numTest; count++) //sort test with bubble sort
{//start for
cout<< scores[count] <<endl;
}//end for
cout << endl << "Average Test Score is: " ;
average = getAvg(scores, numTest, total);
cout << average;
cin >> x;
}//end main
void bubSort (int scores[], int numTest)
{//start function
bool swap;
int temp;
do
{//start do/while
swap = false;
for (int count = 0; count < (numTest - 1); count++)
{ //start for
if (scores[count] > scores[count + 1] )
{//start if
temp= scores[count];
scores[count] = scores[count + 1];
scores[count +1] = temp;
swap = true;
}//end if
}//end for
}//end do/while
while (swap);
}//end function
int getAvg(int scores[], int numTest, int total)
{
float average;
int count;
for (count = 0; count < numTest; count++)
{
total += scores[count];
}
average = total/numTest;
return average;
}