Sorting: C++

edited April 2006 in Internet & Media
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;
}

Comments

  • LeonardoLeonardo Wake up and smell the glaciers Eagle River, Alaska Icrontian
    edited April 2006
    "Sorting" - for what? What program, what application? Your post is very vague.
  • dawinnerdawinner Osgood, OH
    edited April 2006
    His other thread was about programming in C++ so im goin on a limb and saying he wants to show sorting for the program in C++ code. which i havn't got a clue. Sorry!!
  • mondimondi Icrontian
    edited April 2006
    there are good descriptions of various sorting algorithms here: http://www.cprogramming.com/tutorial/computersciencetheory/sortcomp.html

    M
  • edited April 2006
    this is a program that takes test scores from a user adds them up to get the average then displays the test scores in an asending order with the bubble sort. i have the code but i am at a loss if it is right and how to display it. sorry i am new to programing....

    // 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;
    }
  • mondimondi Icrontian
    edited April 2006
    It would really help if you put the code between PHP tags. When you dont it makes it hard to know what are errors in your program and what is cut off by vBulletin.

    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.
Sign In or Register to comment.