By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

sorting

P: n/a
hi guys how would i sort this in ascending order im not understanding
the *Ptr

// 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];
}

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

Apr 13 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
littlegirl wrote:
hi guys how would i sort this in ascending order im not understanding
the *Ptr
What are you not understanding about pointers?
To sort the array, you can use std::sort from the <algorithm> header. It
takes iterators (which is just a regular pointer for arrays) to the first
and one past the last element of the sequence to be sorted.
// 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;
This is quite a confusing way of defining those variables. It's considered
bad style. Write each one in its own line.

Btw: It's good practice to define local variables just where they are needed
for the first time, not all at the beginning of the function.
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];
}

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


Apr 14 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.