write a program that dynamically allocates an array large enough tohold a user d
ID: 3610837 • Letter: W
Question
write a program that dynamically allocates an array large enough tohold a user defined number of test scores. Once all the scores areentered the array should be passed to a function that sorts them inascending orders. another function should be called that calculatesthe average score. the program should display the sorted list ofscores and averages with appropriate headings. Use pointersnotation rather than array notation whenever possible. Input validation : Do not accept negative numbers for testscores. Input validation : Do not accept negative numbers for testscores.Explanation / Answer
Dear, #include <iostream>
#include <iomanip>
using namespace std;
void arrAscendingOrder(double *[], int);
void showArrPtr(double *[], int);
void showAverage(double , int);
int main()
{
double *scores,
total=0.0,
average; intnumScores;
//Get the number of testscores.
cout<<"How many testscores would you liketo process?";
cin>>numScores;
//Dynamically allocate anarray large enough tohold that many test scores scores = newdouble[numScores];
//Get the test score for eachtest
cout<<"Enter the testscores below. ";
for (int count=0; count<numScores; count++)
{
cout<<"Test "<<(count+1)<<": ";
cin>> scores[count];
}
//Calculate the total scores for (int count=0; count<numScores; count++)
{
total += scores[count];
}
arrAscendingOrder (scores,numScores);
showArrPtr (scores,numScores);
showAverage(total, numScores);
//Free dynamically allocatedmemory.
delete [] scores;
scores =0;
return 0;
}
void arrAscendingOrder(double *array[], int size)
{
int startScan, minIndex;
int *minElem;
for (startScan=0; startScan<(size-1); startScan++)
{
minIndex =startScan;
minElem = array[startScan];
for (int index=startScan+1; index<size; index++)
{
if (*(array[index])<*minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrPtr(double *array[], int size)
{
for (int count=0; count< size; count++)
cout<< *(array[count])<<" ";
cout<<endl;
}
void showAverage(double total, int numScores)
{
double average;
//Calculate the average
average = total / numScores;
//Display the results.
cout<<fixed<<showpoint<<setprecision(2);
cout<<"Average Score: "<<average<<endl;
}
I hope this will helpful foryou................... for (int count=0; count<numScores; count++)
{
total += scores[count];
}
arrAscendingOrder (scores,numScores);
showArrPtr (scores,numScores);
showAverage(total, numScores);
//Free dynamically allocatedmemory.
delete [] scores;
scores =0;
return 0;
}
void arrAscendingOrder(double *array[], int size)
{
int startScan, minIndex;
int *minElem;
for (startScan=0; startScan<(size-1); startScan++)
{
minIndex =startScan;
minElem = array[startScan];
for (int index=startScan+1; index<size; index++)
{
if (*(array[index])<*minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrPtr(double *array[], int size)
{
for (int count=0; count< size; count++)
cout<< *(array[count])<<" ";
cout<<endl;
}
void showAverage(double total, int numScores)
{
double average;
//Calculate the average
average = total / numScores;
//Display the results.
cout<<fixed<<showpoint<<setprecision(2);
cout<<"Average Score: "<<average<<endl;
}
I hope this will helpful foryou...................
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.