Hi guys Can you please show me how this thing is done? Thank you. Here is the pr
ID: 3536730 • Letter: H
Question
Hi guys
Can you please show me how this thing is done? Thank you. Here is the problem.
Write a program tha dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are enterered, the array should be passed to a function that sorts them in ascending order.. Another function should be callled that calculates the average score. The program should display the stored list of scores and average s in appropriate headings. Use pointer notation rather than array notation. whenever possible. Input validation: Do not accept negative numbers for test scores.
Thank you guys!
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
void bubbleSort(double x[], int n);
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 test scores
count; //Counter variable
//Get the number of test scores
cout << "How many test scores would you like to enter? ";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many test scores
scores = new double[numScores];
//Get the test scores
cout << "Enter the test scores below. ";
for (count = 0; count < numScores; count++)
{
cout << "Test Score " << (count + 1) << ": ";
cin >> scores[count];
}
//Calculate the total of the scores
for (count = 0; count < numScores; count++)
{
total += scores[count];
}
bubbleSort(scores,numScores);
for (count = 0; count < numScores; count++)
{
cout<<"score:"<<count<<" "<<scores[count]<<endl;
}
//Calculate the average test score
average = total / numScores;
//Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Average score is: " << average << endl;
//Free dynamically allocated memory
delete [] scores;
scores = 0; //Make scores point to null.
return 0;
}
void bubbleSort(double x[], int n) {
bool exchanges;
do {
exchanges = false; // assume no exchanges
for (int i=0; i<n-1; i++) {
if (x[i] > x[i+1]) {
double temp = x[i]; x[i] = x[i+1]; x[i+1] = temp;
exchanges = true; // after exchange, must look again
}
}
} while (exchanges);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.