Q: (C++) Write a program that uses a STL vector to hold a user-defined number of
ID: 3679694 • Letter: Q
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 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
Explanation / Answer
Here is the code i can help you with
#include <iostream>
#include <iomanip>
using namespace std;
double calcAverage(double, int);
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 days of scores
count; // Counter variable
// Get the number of days of scores.
cout << "How many days of test scores do you wish ";
cout << "to process? ";
cin >> numScores;
// Dynamically allocate an array large enough to hold
// that many days of scores amounts.
scores = new double[numScores];
// Get the scores figures for each day.
cout << "Enter the test scores below. ";
for (count = 0; count < numScores; count++)
{
cout << "Day " << (count + 1) << ": ";
cin >> scores[count];
}
// Calculate the total scores
for (count = 0; count < numScores; count++)
{
total += scores[count];
}
bubbleSort(scores,numScores);
average = calcAverage(total, numScores);
cout << endl;
for (count = 0; count < numScores; count++)
{
cout<<"Day "<<(count+1)<<": "<< *(scores + count) <<endl;
}
// Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Average Test Score: " << average << "%" << endl;
// Free dynamically allocated memory
delete [] scores;
scores = 0; // Make scores point to null.
return 0;
}
double calcAverage(double total, int numScores)
{
double avg;
// Calculate the average scores per day
avg = total / numScores;
return avg;
}
void bubbleSort(double array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
}
while (swap);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.