Dynamically allocated array/sorting

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.

return 0;
}


Thank you for any advice that is offered.
Last edited on
I would say that simple buble sort could help.

function:(which will be some where below main function )
1
2
3
4
5
6
7
8
9
10
11
12
 void bubbleSort(double x[], int n) {
    bool exchanges;
    do {
       exchanges = false;  // assume no exchanges
       for (int i=0; i<n-1; i++) {
          if (x[i] > x[i+1]) {
             double temp = x[i]; x[i] = x[i+1]; x[i+1] = temp;
             exchanges = true;  // after exchange, must look again
          }
       }
    } while (exchanges);
 }


so just before the main you have to put function definition:
 
void bubbleSort(double x[], int n);


and just before freeing memory put this code:
1
2
3
4
5
6
bubbleSort(scores,numScores);
for (count = 0; count < numScores; count++)
{
   cout<<"score:"<<count<<" "<<scores[count]<<endl;
}
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);
}

bubbleSort(scores,numScores);
for (count = 0; count < numScores; count++)
{
cout<<"score:"<<count<<" "<<scores[count]<<endl;
}


//Free dynamically allocated memory
delete [] scores;
scores = 0; //Make scores point to null.

return 0;
}




When compiling it I get the following 2 errors:

1>lastchance.obj : error LNK2019: unresolved external symbol "void __cdecl bubbleSort(double * const,int)" (?bubbleSort@@YAXQANH@Z) referenced in function _main

1>C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\lastchance\Debug\lastchance.exe : fatal error LNK1120: 1 unresolved externals
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.
jmc, thank you for your comments.

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.
It does occur in the source code I posted above just below the section that says //Display the results. Should it be placed somewhere else?

Does it have to be under the call to the function? I think I tried that in the middle of the night last night and it didn't work either.
Here the whole working program again.

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
#include <iostream>
#include <iomanip>
using namespace 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 = 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;


 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 so much. I am not able to run it right now, but will be able to later today. Thanks again!
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
if you still have time try to use insertion, it hs similar characteristics, but unlike bubble sort, it should be used occasionally.
Topic archived. No new replies allowed.