? Write a function that dynamically allocates an array of integers. The function
ID: 3533959 • Letter: #
Question
?Write a function that dynamically allocates an array
of integers. The function should accept an argument
indicating the number of elements to allocate.
Populate the array with 50 test scores between 55
and 100 using the function that you created.
Display scores in rows of five scores.
Write a function to compute the average
and return the value.
Display the average value.
Write a function to sort the array in acending order.
Display the sorted scores in rows of five.
Hint: See Selection Sort Pattern in Lecture.
Write a function to find the median value
and return the median value.
Display the median value.
Write a function to find the lowest and highest
scores and
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void arrSelectSort(int *[], int);
void showArray(int [], int);
void showArrPtr(int *[], int);
int main()
{
double *TestScores, //To dynamically allocate an array
total = 0.0, //Accumulator
average; //To hold average test scores
int numTest, //To hold number of test scores
count; //Counter variable
//Get the number of test scores you wish to average and put in order
cout << "How many test scores do you wish ";
cout << "to enter? ";
cin >> numTest;
//Dynamically allocate an array large enough to hold that many scores
TestScores = new double[numTest];
//Get the test scores
cout << "Enter the test scores below. ";
for (count = 0; count < numTest; count++)
{
cout << "Test Score " << (count + 1) << ": ";
cin >> TestScores[count];
}
//Calculate the total test scores
for (count = 0; count < numTest; count++)
{
total += TestScores[count];
}
//Calculate the average test scores
average = total / numTest;
//Dsiplay the results
cout << fixed << showpoint << setprecision(2);
cout << "The average of all the test score is " << average << endl;
//Free dynamically allocated memory
delete [] TestScores;
TestScores = 0; //make TestScores point to null
//An array of pointers to int
int *arrPtrTestScores[count];
//Each element of arrPtr is a pointer to int. Make each
//element point to an element in the donations array
for (int count = 0; count < TestScores[count]; count++)
arrPtr[count] = &TestScores[count];
//Sort the elements of the array of pointers
arrSelectSort(arrPtr, TestScores[count];
//Display the Test Scores in ascending order
cout << "The test scores, sorted in ascending order, are: ";
showArrPtr(arrPtr, TestScores[count]);
return 0;
}
//This function performs an ascending order selection sort
void arrSelectSort(int *arr[], int size)
{
int startScan, minIndex;
int *minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = arr[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (*(arr[index]) < *minElem)
{
minElem = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minElem;
}
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.