I am looking for assistance in finishing a problem where I need to write a program to dynamically allocate an array large enough to hold a user-defined number of test scores. Once all the numbers are entered, I need to average the test scores and sort them in ascending order. I have managed to accomplish the first two but have been trying for two days to figure out how to sort them. I would like help with the code on how to get this array sorted. This is what I have so far...
//This program averages test scores and displays them in ascending order
//The number of test scores is user defined and the program uses a dynamically allocated array
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double *scores, //To dynamically allocate an array
total = 0.0, // Accumulator
average; // To hold average scores
int numScores, //To hold the number of test scores
count; //Counter variable
//Get the number of test scores
cout << "How many test scores would you like to enter? ";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many test scores
scores = new double[numScores];
//Get the test scores
cout << "Enter the test scores below.\n";
for (count = 0; count < numScores; count++)
{
cout << "Test Score " << (count + 1) << ": ";
cin >> scores[count];
}
//Calculate the total of the scores
for (count = 0; count < numScores; count++)
{
total += scores[count];
}
//Calculate the average test score
average = total / numScores;
//Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Average score is: " << average << endl;
//Free dynamically allocated memory
delete [] scores;
scores = 0; //Make scores point to null.
Thank you jupe for your help. I have tried to add the bubble sort as you suggested but wasn't exactly sure where to put the body of the function (from bool through (exchanges)). Here is what I have after trying:
//This program averages test scores and displays them in ascending order
//The number of test scores is user defined and the program uses a dynamically allocated array
#include <iostream>
#include <iomanip>
using namespace std;
void bubbleSort(double scores[], int numScores);
int main ()
{
double *scores, //To dynamically allocate an array
total = 0.0, // Accumulator
average; // To hold average scores
int numScores, //To hold the number of test scores
count; //Counter variable
//Get the number of test scores
cout << "How many test scores would you like to enter? ";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many test scores
scores = new double[numScores];
//Get the test scores
cout << "Enter the test scores below.\n";
for (count = 0; count < numScores; count++)
{
cout << "Test Score " << (count + 1) << ": ";
cin >> scores[count];
}
//Calculate the total of the scores
for (count = 0; count < numScores; count++)
{
total += scores[count];
}
//Calculate the average test score
average = total / numScores;
//Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Average score is: " << average << endl;
{
bool exchanges;
do {
exchanges = false; // assume no exchanges
for (int i=0; i<numScores-1; i++) {
if (scores[i] > scores[i+1]) {
double temp = scores[i]; scores[i] = scores[i+1]; scores[i+1] = temp;
exchanges = true; // after exchange, must look again
}
}
} while (exchanges);
}
There is a declaration of bubbleSort in your code but the function body can't be found anywhere you've forgotten to insert that code.
You could also use a quick_sort which would be much faster (as the name suggests...) or even a radix_sort, sorting first the precision bits and afterwards the exponent bits. Though it won't really matter for this small number of elements and a bubble_sort sure is the easiest way.
I have the following code which I thought was the body of the function:
{
bool exchanges;
do {
exchanges = false; // assume no exchanges
for (int i=0; i<numScores-1; i++) {
if (scores[i] > scores[i+1]) {
double temp = scores[i]; scores[i] = scores[i+1]; scores[i+1] = temp;
exchanges = true; // after exchange, must look again
}
}
} while (exchanges);
}
I am brand new at C++. I'm taking an online course and the instructor is not helpful whatsoever. I have tried numerous things, but am really having difficulty with this chapter on pointers.
Yes, it is. But it doesn't occur in the source code which you posted above.
When building your project the declaration void bubbleSort(double scores[], int numScores); can be found but the function with its body doesn't follow.
You have to include the bubbleSort-function, not only the declaration.
#include <iostream>
#include <iomanip>
usingnamespace std;
void bubbleSort(double scores[], int numScores);
int main (void){
double *scores, //To dynamically allocate an array
total = 0.0, // Accumulator
average; // To hold average scores
int numScores, //To hold the number of test scores
count; //Counter variable
//Get the number of test scores
cout << "How many test scores would you like to enter? ";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many test scores
scores = newdouble[numScores];
//Get the test scores
cout << "Enter the test scores below.\n";
for (count = 0; count < numScores; count++){
cout << "Test Score " << (count + 1) << ": ";
cin >> scores[count];
}
//Calculate the total of the scores
for (count = 0; count < numScores; count++){
total += scores[count];
}
//Calculate the average test score
average = total / numScores;
//Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Average score is: " << average << endl;
bubbleSort(scores,numScores);
for (count = 0; count < numScores; count++){
cout<<"score "<<(count+1)<<": "<<scores[count]<<endl;
}
//Free dynamically allocated memory
delete [] scores;
scores = 0; //Make scores point to null.
cin.ignore(INT_MAX, '\n');
cin.ignore(INT_MAX, '\n');
return 0;
}
void bubbleSort(double scores[], int numScores){
bool exchanges;
do{
exchanges = false; // assume no exchanges
for(int i = 0; i < numScores - 1; i++){
if(scores[i] > scores[i + 1]){
double temp = scores[i];
scores[i] = scores[i+1];
scores[i+1] = temp;
exchanges = true; // after exchange, must look again
}
}
}while(exchanges);
}
Thank you again to jupe and jmc. The two of you combined to get me the help I needed. As a 16-year old just starting out with C++ this environment is very intimidating. I appreciate the kindness you showed to me. I was able to finish this problem and go on to do the following problem as well. Ryan