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;
}``````

It's because you are performing integer division instead of floating point division.

Are the only acceptable test scores integers (i.e. 66, 85, 98, etc.)? Or can you get a decimal grade on a test (i.e. 98.6, 82.5, etc...)? It actually makes a big difference here.

No, I'm assuming they can only use whole numbers.

I got it to work, I just changed everything to a float; except for *scores.
This is the new code. Does it look clean? It compiles and does what it needs to.

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

void bubSort(int [], float);
float getAvg (int[], float);
int main() {//start main

int *scores;     //dynamically allocate an array
float numTest = 0; //hold number of test

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);	//start function for sorting

cout << "Sorted Test:" << endl;
for (count = 0; count < numTest; count++) //This will print the sorted list of scores
{//start for
cout<< scores[count] <<endl;

}//end for

cout << endl << "Average Test Score is: " ;

average = getAvg(scores, numTest); //start function to gather the average test score

cout << average;					//print out the average

cout << "Press any button to Exit";
cin >> x;						//hold open the window

}//end main

void bubSort (int scores[], float numTest)

{//start function

bool swap;
int temp;

do
{//start do/while
swap = false;

for (int count = 0; count < (numTest - 1); count++) //counts through the elements
{ //start for

if (scores[count] > scores[count + 1] ) //if left element is bigger then the right element
{//start if								//then they swap

temp= scores[count];                //stores the value of the left element in a temporary variable
scores[count] = scores[count + 1];  //stores value of the right element into left element
scores[count +1] = temp;			//then stores what was orginally in the left element
//into the right element
swap = true;

}//end if
}//end for

}//end do/while

while (swap);

}//end function

float getAvg(int scores[], float numTest) //obtain average function
{

float average;
int count;
float total = 0;
for (count = 0; count < numTest; count++) //counts through the number of elements
{
total += scores[count];  //adds each score, one by one, as it goes through each element
}	                     //holding the total scores in the total variable

average = total/numTest;  //getting the average by dividing the total by the number of test scores

return average;
}``````
commented: Nice code for a beginner! +1

Does it look clean?

The code is actually pretty clean for a beginner! +1 for you. There are a few things you could do to improve it further.

1. Put the score input and score display sections in their own functions. It is always preferable to keep the main function very sparse and easy to read. This way, you can quickly look at main(), and (assuming your functions are named well) quickly understand the logic and data flow.

2. Align your comments.

``````int dummy1;                       // A dummy integer
double aRealNumberButStillDummy;  // A dummy real number``````

It's much easier to read code if all of the comments are aligned. This also helps the reader quickly seperate each line of code from its explanation. The exception here would be your block (//begin/end) comments. By the way, I'm impressed that you use this idea. It can help when your nested logic gets deep. Of course, it is best to avoid deep nesting, but....

3. Split long lines into two. If you have a long line of code, try to find a logical break point, and seperate it into two lines. This will make your code easier to read. Don't split lines for comments, however. If you need to read a comment, it's ok to have to scroll right.

Pretty good altogether. Keep up the good work. You will probably be a successful programmer!

Oh, and use a better sort method. Just kidding. But seriously, bubble sort makes the algorithm gods sad. I realize you probably have to use it for this assignment. However, it would be useful to learn about other sort methods on your own time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.