In: Computer Science
Directions:
Write a C++ program that will create an array of four integers.
It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100)
It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F.
It will display the scores and letter grade to the screen.
NOTE: No menu is required.
2. Write a function called inputData that takes in a pointer to the array of integers as a parameter, prompts the user for four valid score values and stores these values in the array of integers. (15 points).
4. Write a function called calcGrade that takes in a pointer to the array of integers as a parameter, computes and returns the letter grade based upon the scale listed above. (15 points).
5. Write a function called displayResults that takes in a pointer to the array of integers and the character letter grade as a parameters, computes, and displays these values in the format shown in the sample run below. (15 points).
6. Add comments wherever necessary. (5 points)
Sample Runs:
NOTE: not all possible runs are shown below.
Run 1:
Welcome to the compute letter grade program
Test score 1: Please enter in a score...-9
Please enter in a score (0-100)...999
Please enter in a score (0-100)...90
Test score 2: Please enter in a score...98
Test score 3: Please enter in a score...96
Test score 4: Please enter in a score...95
Test 1: 90
Test 2: 98
Test 3: 96
Test 4: 95
Grade : A
Run 2:
Welcome to the compute letter grade program
Test score 1: Please enter in a score...90
Test score 2: Please enter in a score...70
Test score 3: Please enter in a score...85
Test score 4: Please enter in a score...76
Test 1: 90
Test 2: 70
Test 3: 85
Test 4: 76
Grade : B
General Requirements:
1) Include the header comment with your name and other information on the top of your files.
2. Please make sure that you're conforming to specications (program name, print statements, expected inputs and outputs etc.). Not doing so will result in a loss of points. This is especially important for prompts. They should match mine EXACTLY.
3. If we have listed a specication and allocated point for it, you will lose points if that particular item is missing from your code, even if it is trivial.
4. No global variables (variables outside of main() ) unless they are constants.
5. All input and output must be done with streams, using the library iostream
6. You may only use the iostream and iomanip libraries. Including unnecessary libraries will result in a loss of points.
7. NO C style printing is permitted. (Aka, don't use printf). Use cout if you need to print to the screen.
8. When you write source code, it should be readable and well-documented (comments).
9. Make sure you either develop with or test with CLion (to be sure it reports no compile errors or warnings!) before you submit the program.
// do comment if any problem arises
// code
#include <iostream>
using namespace std;
// this function reads valid scores in given array
void inputData(int *scores)
{
for (int i = 0; i < 4; i++)
{
// read ith score
cout << "Test score " << i + 1 << ": Please enter in a score...";
cin >> scores[i];
// check for valid scores
while (scores[i] < 0 || scores[i] > 100)
{
cout << "Please enter in a score (0-100)...";
cin >> scores[i];
}
}
}
char calcGrade(int *scores)
{
// total score
float score = 0;
// compute total score
for (int i = 0; i < 4; i++)
score += scores[i];
// compute average
score /= 4;
// check for grade
if (score >= 90)
return 'A';
else if (score >= 80)
return 'B';
else if (score >= 70)
return 'C';
else if (score >= 60)
return 'D';
return 'B';
}
void displayResults(int *scores, char grade)
{
for (int i = 0; i < 4; i++)
cout << "Test " << i + 1 << ": " << scores[i] << endl;
cout << "Grade: " << grade << endl;
}
int main()
{
int scores[4];
cout << "Welcome to the compute letter grade program\n";
inputData(scores);
char grade = calcGrade(scores);
displayResults(scores, grade);
}
Output: