My assignment is: Write a program that dynamically allocates an array large enou
ID: 3646082 • Letter: M
Question
My assignment is: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 into ascending order. Another function should be called that calculates the average score. The program should display the sorted 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.
My code is listed below but what do I do to not accept negative numbers?
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void arrSelectSort(double *, 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 are you entering? ";
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 scores 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];
//Sort the elements of the array of pointers
arrSelectSort(TestScores, TestScores[count]);
//Display the Test Scores in ascending order
cout << "The test scores in ascending order are: ";
return 0;
}
//This function performs an ascending order selection sort
void arrSelectSort(double *arr, int size)
{
int startScan;
double minIndex;
double 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[(int)minIndex] = arr[startScan];
arr[startScan] = minElem;
}
}
Explanation / Answer
#include 002 #include 003 004 using namespace std; 005 006 //Function prototypes 007 void arrSelectSort(double *, int); 008 void arrAvgScore (double *, int); 009 010 int main() 011 012 { 013 014 double *TestScores, //To dynamically allocate an array 015 total = 0.0, //Accumulator 016 average; //To hold average test scores 017 018 int numTest, //To hold number of test scores 019 count; //Counter variable 020 021 //Get the number of test scores you wish to average and put in order 022 023 cout numTest; 026 027 //Dynamically allocate an array large enough to hold that many scores 028 029 TestScores = new double[numTest]; 030 031 //Get the test scores 032 033 coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.