Question: Q: (C++) Write a program that uses a STL vector to hold a user-defined number of test scores. Once all the scores are entered, the

Q: (C++) Write a program that uses a STL vector to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a STL function that sorts them in ascending order. Your program should also calculate the average score. Display the sorted list of scores and averages with appropriate headings.

This is for my own practice and to familiarize myself with STL vectors.

I have entered the testScore vector values of 70, 90, 80. But, it appears that the sorting function does not sort the vector because the output comes out to be the same as the original vector: 70, 90, 80. A revised code with helpful comments will help me understand the STL material better. Thank you! (Also, if there is a better simpler syntax please let me know!)

-----------------------------------------------------------------------

#include #include #include //Needed to define vectors using namespace std;

float Average(vector scores, int number); void SortTestScores(vector scores, int n);

void main() { //Variable declaration. vector testScores; //A vector of integers float averageScore, score; int number, i; //size to store number of test scores, i to loop variable

//Input how many test scores. cout << "How many test scores are you entering?: "; cin >> number;

//Tell user to input the data. cout << "Enter the test scores: " << endl;

for (i = 0; i < number; i++) { cout << "Test Score of Student #" << (i + 1) << ": "; cin >> score; //Pushing onto back (end) of the vector. testScores.push_back(score); } //Call the function to sort test scores. SortTestScores(testScores, number);

//Outputting sorted testScores cout << "Sorted TestScores:" << " " ;

for (i = 0; i < number; i++) { cout << testScores.at(i); }

//Function call to avg the scores & to return the avgscore

double avgScore = Average(testScores, number); //Outputting Average test score cout << " Average testScore is:" << avgScore << endl; system("pause"); } // end of main

//Sort Function definition void SortTestScores(vector scores, int number) { int temp; int i, j; for (i = 1; i < number; i++) { for (j = 0; j < number - i; j++) { if (scores.at(j) > scores.at(j + 1)) { temp = scores.at(j); scores.at(j) = scores.at(j + 1); scores.at(j + 1) = temp; } //end if } // end loop k }// end loop i }

//Function to calculate avg test score float Average(vector score, int n) { int i; float avg = 0.0;

//to calculate sum of scores for (i = 0; i < n; i++) { avg += score.at(i); }//end for

avg = avg / n; //returning avg value return avg; } //end the Average

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!