Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with C++ please. Thank you. Problem Write program that dynamically all

ID: 3601705 • Letter: N

Question

Need help with C++ please. Thank you.

Problem

Write program that dynamically allocates an array large enough to hold a user-input number of test scores (No one dimensional array declaration is allowed in the solution). Once all the test scoresare entered, the array should be passed to a function that sorts the in ascending order. Another function should be called that calculates the average score. The program should display sorted list of scores and averages with headings shown in the output sample. Use pointer notation rather array notation whenever possible.

Requirements:

You are required to implement the following functions:

//*******************************************
// Function sort *
// This function performs a selection sort *
// on the array pointed to by the score *
// parameter into ascending order. The size *
// parameter holds the number of elements. *
//*******************************************

void sort(double* score, int size)

NOTE: Use the Selection sort to sort the arrays. It's on pages 469- 472.

//********************************************
// Function average *
// This function calculates and returns the *
// average of the values stored in the array *
// passed into the scores parameter. The *
// parameter numScores holds the number of *
// elements in the array. *
//********************************************

double average(double* score, int numScores)

Output sample

How many test scores will you enter? 5
Enter test score 1: 80
Enter test score 2: 90
Enter test score 3: 70
Enter test score 4: 60
Enter test score 5: 50

The test scores in ascending order, and their average, are:

Score
-----

50.00
60.00
70.00
80.00
90.00

Average score: 70.00

Explanation / Answer

#include <iostream>
using namespace std;
void sort(double* score, int size) {
for (int i = 0; i < size - 1; i++)
{
int index = i;
for (int j = i + 1; j < size; j++)
if (score[j] < score[index])
index = j;
  
double smallerNumber = score[index];
score[index] = score[i];
score[i] = smallerNumber;
}
}
double average(double* score, int numScores) {
double sum = 0;
for(int i=0;i<numScores;i++) {
sum += score[i];
}
return sum/numScores;
}
int main() {
int n;
cout<<"How many test scores will you enter? "<<endl;
cin >> n;
double *scores = 0;
scores = new double[n];
for(int i=0;i<n;i++) {
cout<<"Enter test score "<<i+1<<": "<<endl;
cin >> scores[i];
}
sort(scores, n);
cout<<"The test scores in ascending order, and their average, are:"<<endl;
cout<<"Scores: ---------"<<endl;
for(int i=0;i<n;i++) {
cout<<scores[i]<<endl;   
}
cout<<"Average score: "<<average(scores, n);
  
return 0;
}

Output: