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

Help I\'m struggling with this, I need in c++ Write a program that dynamically a

ID: 3599986 • Letter: H

Question

Help I'm struggling with this, I need in c++

Write a program that dynamically allocates an array large enough to hold a user defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sored list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible. Input validation: Do not accept negative numbers for test scores.

Explanation / Answer

Here is stuctured code in c++, with comments in necessary places.

#include <iostream>
// sort() in STL.
#include <bits/stdc++.h>

using namespace std;

//To find the average
float average (int *elem, int n)
{
    float avg=0;

    for(int i=0; i<n; ++i)
    {
        avg += elem[i];
    }
  
    return avg/n;

}

//To display the total number of elements
void display (int *elem, int n, float avg )
{
    cout<<" Sorted array of Test score: ";
    for (int j = 0; j < n; ++j)
    {
        cout << elem[j] << endl;
    }
    cout<<"Average of the Test score is: "<<avg<<endl;


}

//Driver Function
int main()
{
    int* x = NULL; //dynamically declaring
    int n, testScore;
    cout<<"Enter the total number of elements you want to eneter in the array: ";
    cin >> n; //

    x = new int[n]; // Allocating size

    for (int i=0;i<n;)
    {
        cin >> testScore;
        if (testScore >= 0 )
        {
            x[i] = testScore;
            ++i;
        }
        else
            cout<<"Enter the number greater than 0 Enter Again ";  
    }

    sort(x, x+n);
    float avg;
    avg = average(x,n);
    display (x,n,avg);

    delete [] x; // Deallocating value of dynamica array
    return 0;
}

//end of code

Output of the code

xyz@pc:~/Documents/C++$ g++ sort_array.cpp -o here
xyz@pc:~/Documents/C++$ ./here
Enter the total number of elements you want to eneter in the array: 5
15
8
655
0
-8
Enter the number greater than 0
Enter Again
1

Sorted array of Test score:
0
1
8
15
655
Average of the Test score is: 135.8
xyz@pc:~/Documents/C++$