Sorting: C++
how would i display sorting im at a loss , thanks people
// This program Enter test scores and sort them in oder and Averages them
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double *score, total = 0, average;
int TestScores, count;
cout << "How many Test Scroes do you wish to enter ";
cin >> TestScores;
score = new double[TestScores]; // Allocate memory
// Get the Test Scores
cout << "Enter each students test scores.\n";
for (count = 0; count < TestScores; count++)
{
cout << "Student " << (count + 1) << ": ";
cin >> score[count];
}
for (int i=0; i<TestScores; ++i)
for (int j=i; j<TestScores; ++j)
if (score < score[j])
{
double tmpscore = score;
score = score[j];
score[j] = tmpscore;
}
// Calculate the total Scores
for (count = 0; count < TestScores; count++)
{
total += score[count];
}
// Calculate the average Test Scores
average = total / TestScores;
// Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
// Free dynamically allocated memory
delete [] score;
return 0;
}
// This program Enter test scores and sort them in oder and Averages them
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double *score, total = 0, average;
int TestScores, count;
cout << "How many Test Scroes do you wish to enter ";
cin >> TestScores;
score = new double[TestScores]; // Allocate memory
// Get the Test Scores
cout << "Enter each students test scores.\n";
for (count = 0; count < TestScores; count++)
{
cout << "Student " << (count + 1) << ": ";
cin >> score[count];
}
for (int i=0; i<TestScores; ++i)
for (int j=i; j<TestScores; ++j)
if (score < score[j])
{
double tmpscore = score;
score = score[j];
score[j] = tmpscore;
}
// Calculate the total Scores
for (count = 0; count < TestScores; count++)
{
total += score[count];
}
// Calculate the average Test Scores
average = total / TestScores;
// Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
// Free dynamically allocated memory
delete [] score;
return 0;
}
0
Comments
M
// This program Enter test scores and sort them in oder and Averages them
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double *score, total = 0, average;
int TestScores, count;
cout << "How many Test Scroes do you wish to enter ";
cin >> TestScores;
score = new double[TestScores]; // Allocate memory
// Get the Test Scores
cout << "Enter each students test scores.\n";
for (count = 0; count < TestScores; count++)
{
cout << "Student " << (count + 1) << ": ";
cin >> score[count];
}
//bubble sort
for (int i=0; i<TestScores; ++i)
for (int j=i; j<TestScores; ++j)
if (score < score[j])
{
double tmpscore = score;
score = score[j];
score[j] = tmpscore;
}
// Calculate the total Scores
for (count = 0; count < TestScores; count++)
{
total += score[count];
}
// Calculate the average Test Scores
average = total / TestScores;
// Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
// Free dynamically allocated memory
delete [] score;
return 0;
}
The missing #include's are iostream.h and iomanip.h
Your bubble sort is partially missing, and wrong
[PHP]
for(int i = 0; i < TestScores; i++)
{
for(int j = 0; j < TestScores - 1; j++)
{
if(score[j] > score[j+1])
{
double temp = score[j+1];
score[j+1] = score[j];
score[j] = temp;
}
}
}
[/PHP]
then you should be in good shape. If you look through the description on the site I linked to it explains how this works.
Now in order to display them in ascending order you need to iterate through the score values after sorting them and output them as you do. You could add another loop, or just hijack the 'total score' loop:
[PHP]
cout << "\nThe scores in ascending order:\n";
for (count = 0; count < TestScores; count++)
{
total += score[count];
cout << score[count] << "\n";
}
[/PHP]
And finally, when you say you dont know how to display the code ... Is this referring to outputting the scores? Or is it compiling and running the code..
M
edit:// -- to put code between PHP Tags:
before you copy and paste the code type: [ PHP ] without any spaces
after you're done, type [ / PHP ] again without spaces to close out the code section.