The program needs to collect 5 scores from the user and a name for each score. T
ID: 3646762 • Letter: T
Question
The program needs to collect 5 scores from the user and a name for each score. The user may also enter no scores. If no scores are input, the program should not attempt to manipulate the array of scores and should quit with a message that no scores were input. If the user inputs 1, 2, 3, 4, or 5 scores and a name for each score, the program will display the following: The name associated with the highest score and the score. The name associated with the lowest score and the score. The average of all scores. To keep the program simple, assume that no duplicate values will be input.
-Use an array to store scores.
-Use functions to process the scores.
-Use a sentinel value to stop looping for scores.
-Provide the following test cases:
1. No scores
2. Less than 5 scores
3. Five scores
Here is my code: The problem I am having is that I don't know how to assign a name to a score in the array.
#include <sstream>
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
// Prototypes
void getScores(double scores[], int numTests);
int tests();
double average(double scores[], int numTests);
double highest(double scores[], int numTests);
double lowest(double scores[], int numTests);
int main()
{
int numTests;
// Funtion call tests
numTests = tests();
// Dynamically allocated array
double* scores = new double [numTests];
// Function call scores
getScores(scores, numTests);
average(scores, numTests);
highest(scores, numTests);
lowest(scores, numTests);
delete [] scores;
system("pause");
return 0;
}
// Gets the scores
void getScores(double scores[], int numTests)
{
cout << "Enter a number for each test score: ";
// fills the array with scores
for (int i = 0; i < numTests; i++)
{
cout << "Enter test score number " << i + 1 << " :" << endl;
int temp;
// test score imput
cin >> temp;
// test score imput goes into the array
if (temp >= 0)
{
scores[i] = temp;
}
else {
cout << "Test scores can not be negative. " << i--;
}
}
}
// Gets the number of tests
int tests()
{
int numTests = 0;
cout << "Enter the number of test scroes: ";
while (true)
{
int temp;
cin >> temp;
if (temp >= 0)
{
numTests += temp;
return numTests;
}
else
{
cout << "The number can not be negative. ";
}
}
}
// Averages test scores
double average(double scores[], int numTests)
{
if(numTests == 0) return 0;
double numScores = 0;
for (int i = 0; i < numTests; i++)
{
int temp;
temp = scores[i];
numScores += temp;
}
double average = numScores/numTests;
cout << "Average score: " << average << endl;
}
// Returns highest score
double highest(double scores[], int numTests)
{
if(numTests == 0) return 0;
double highestScore = 0;
for (int i = 0; i < numTests; i++)
{
if(scores[i] > highestScore)
{
highestScore = scores[i];
}
}
cout << "Highest score: " << highestScore << endl;
}
// Returns lowest score
double lowest(double scores[], int numTests)
{
if(numTests == 0) return 0;
double lowestScore = 0;
for (int i = 0; i < numTests; i++)
{
if(i==0)
{
lowestScore = scores[i];
}
if(scores[i] < lowestScore)
{
lowestScore = scores[i];
}
}
cout << "Lowest score: " << lowestScore << endl;
}
Please if you are going to answer this question, guide me to the answer don't just give the answer. I want this to be a learning experiance.
Explanation / Answer
-Use an array to store scores. double* scores = new double [numTests]; -Use functions to process the scores. // Gets the scores void getScores(double scores[], int numTests) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.