Write a C++ program named, gradeProcessor.cpp , that will do the following tasks
ID: 3912535 • Letter: W
Question
Write a C++ program named, gradeProcessor.cpp, that will do the following tasks:
-Print welcome message
-Generate the number of test scores the user enters; have scores fall into a normal distribution for grades
-Display all of the generated scores - no more than 10 per line
-Calculate and display the average of all scores
-Find and display the number of scores above the overall average (previous output)
-Find and display the letter grade that corresponds to the average above (overall average)
-Calculate an display the average of the scores after dropping lowest score
-Find and display the number of scores above the dropped score average
-Find and display the letter grade that corresponds to the average above (dropped score average)
-Find and display the minimum of all scores
-Find and display the maximum of all scores
-Display a histogram of all scores in terms of their corresponding letter grades (see examples)
-Display the occurrence counts for every score appearing in the list more than once
-Print termination message
The example output shows several executions of this program. Make your output look like these examples, although you input and generated values will be different.
There are requirements for these tasks that will now be explained. Some of the requirements also imply certain restrictions you must follow in your design and implementation. There is a specific list of functions that you must implement. The requirements give you their exact names, parameter lists, and return types. You follow these exactly to get full credit for this assignment.
int generateOneScore()
This first function is given to you for free!! All you have to do is copy the code below into your own gradeProcessor.cpp file. As the function's prolog states, seed the random number generator in main just after beginning execution of your application, then call this function whenever you need another score to put into the array of scores. You should understand when and where to use this function as you understand more about this program. Here is the code:
NOTE: You may want to use the MIN_A, MIN_B, etc. named constants elsewhere in your code, so you should remove them from this function and paste them before your main where named constants are usually declared.
void getNumberOfScores( int& )
One Parameters: type int, passed by reference
-Description: Prompts the user to enter the number of scores that will be used during this execution of the application
-USE: Caller invokes this function to get the number of scores to process at the beginning of the application's execution
void generateScores( int list[], int n )
Two Parameters: array of integers and number of integers currently stored in the array
-Description: This function will write n scores into the array of scores named, list. The scores are generated one at a time by calling generateOneScore.
-USE: Caller invokes this function to populate the array, list, with n scores
double calcAverage (int list[], int n, bool drop)
Three Parameters: array of integer scores, number of scores currently in list, and boolean flag, drop
-Description: Calculate the average of the first n scores stored n the array. If drop is true, do not include the lowest score in this calculation. If drop is false, include all n scores. When this function needs to drop the lowest score, it should call the function, findLowest (see below). The average should be accurate to fractional values and NOT always be an integer: in other words, watch the expression you use to do the calculation of the average.
-USE: Caller invokes this function to have computed the average of the first n elements of an array; may do so with or without lowest grade.
int findLowest(int list[], int n)
Two parameters: array of integers and an integer holding the number of values currently in the array
-Description: searches the list to find the smallest (lowest or minimum) value, returns that value
-USE: This function is called when the caller need to have the lowest value in the array returned.
int findHighest(int list[], int n)
Two parameters: array of integers and an integer holding the number of values currently in the array
-Description: searches the list to find the highest (largest or maximum) value, returns that value
-USE: This function is called when the caller need to have the highest value in the array returned.
int countAboveAverage (int list[], int n, double average)
Three Parameters: array of integers, number of integers stored in the array, and average of values in array
-Description: count and return the number of values in the array that are above the average.
-USE: Caller invokes this function to have computed the number of the first n scores of the list that are greater than the average of those n scores.
-Note: This function does NOT call a function to calculate the average. The caller passes the value of the average in as the third parameter.
void displayScores(int list[], int n)
Two Parameters: array of integers and number of integers stored in the array
-Description: print the n scores found in the array, list. Print 10 on each line. If n is not divisible by 10, then there should be fewer than 10 scores on the last line only.
-USE: Caller invokes this function to display the contents of the array of scores
char findLetterGrade(double grade)
One Parameter: value of type, double
-Description: determine the letter grade (type, char) for the numeric score, grade
-USE: Caller invokes this function to determine the letter grade appropriate for a given numeric score
void displayHistogram( int list[], int n )
Two Parameters: array of integers and number of integers stored in the array
-Description: Displays a histogram of all scores in terms of the letter grade for each score. The display heading states how many scores ( n ) are in the histogram, then displays one star for each score that is an A on line one of the histogram. On line two there should be one star for each score that is a B. Proceed this way for all five letter grades (A, B, C, D, F). Each line should begin with a label containing the letter grade for that line. See several examples in the example output. Make sure your output is exactly the same.
-USE: Invoked by main to display the histogram of all scores
void displayDuplicateScores( int list[], int n )
Two Parameters: array of integers and number of integers stored in the array
-Description: For all scores appearing in the list more than one time, this function displays those scores and their occurrence counts. See several examples in the example output.
-USE: Invoked by main to display the occurrence counts for scores appearing in the list more than one time
***function: main
Your main function should control the execution of each of the tasks in the list at the top of this page. Use the example output as a guide to help developing main so that this application works as required and looks like the examples in the example output.
***Notice: while the array of scores is always declared to have 100 elements, the number of elements actually used during an execution is the number entered by the user (2..100). This is why the user's input value (n) is passed around to the various functions: so these functions know how many elements of the array to use. The unused elements should be ignored.
Coding Requirements and Restrictions
1) All variables must be declared locally to a function. No global variables.
2) Make sure your program has good prompts and clearly labeled output. Add blank lines to the output to improve its readability.
OR: Make your output look exactly like the examples.
3) Do NOT use any language constructs that have not been covered in class.
4) Make your code readable by using meaningful variable names, indentation and spacing – in other words, ALL of the coding conventions for this course.
5) All functions must be documented with a description just before the function's header line.
Explanation / Answer
#include<iostream>
#include<time.h>
using namespace std;
//function declarations
int generateOneScore();
void getNumberOfScores(int&);
void generateScores(int list[], int n);
double calcAverage(int list[], int n, bool drop);
int findLowest(int list[], int n);
int findHighest(int list[], int n);
int countAboveAverage(int list[], int n, double average);
void displayScores(int list[], int n);
char findLetterGrade(double grade);
void displayHistogram(int list[], int n);
void displayDuplicateScores(int list[], int n);
int main()
{
int n;
int list[100];
double Avg;
getNumberOfScores(n);
generateScores(list, n);
displayScores(list, n);
Avg = calcAverage(list, n, false);
cout << "Average of all scores: " << Avg << endl;
cout << "Number of scores above this average: " << countAboveAverage(list, n, Avg) << endl;
cout << "Lette grade for all scores: " << findLetterGrade(Avg) << endl;
Avg = calcAverage(list, n, true);
cout << "Average of all scores without lowest: " << Avg << endl;
cout << "Number of scores above this average: " << countAboveAverage(list, n, Avg) << endl;
cout << " Minimum of all scores : " << findLowest(list, n) << endl;
cout << "Maximum of all scores : " << findHighest(list, n) << endl << endl;
cout << "Displaying histogram for " << n << " scores: " << endl;
displayHistogram(list, n);
displayDuplicateScores(list, n);
cout << " Thanks for using Grade Processor application." << endl;
}
int generateOneScore()
{
const int A = 14; // ~14% of scores are A
const int B = 39; // ~25% of scores are B
const int C = 75; // ~36% of scores are C
const int D = 89; // ~14% of scores are D
const int F = 100; // ~11% of scores are F
const int MIN_A = 90;
const int MIN_B = 80;
const int MIN_C = 70;
const int MIN_D = 60;
// generate a number bewteen 1 and 100, inclusive
// to determine which letter grade to generate
int whichLetter = rand() % 100 + 1; // 1..100
// Set min and max based on the letter grade
int min, max;
if (whichLetter <= A) {
min = MIN_A;
max = 101;
}
else if (whichLetter <= B) {
min = MIN_B;
max = MIN_A;
}
else if (whichLetter <= C) {
min = MIN_C;
max = MIN_B;
}
else if (whichLetter <= D) {
min = MIN_D;
max = MIN_C;
}
else {
min = 0;
max = MIN_D;
}
// Generate a random score for the chosen letter grade
int score = rand() % (max - min) + min;
return score;
}
void getNumberOfScores(int&n)
{
cout << "How many scores do you want to process: ";
cin >> n;
}
void generateScores(int list[], int n)
{
for (int i = 0; i < n; i++)
{
list[i] = generateOneScore();
}
}
double calcAverage(int list[], int n, bool drop)
{
int min,sum=0;
if (drop)
{
min = findLowest(list, n);
}
int count = 0;
for (int i = 0; i < n; i++)
{
if (drop)
{
if (list[i] != min)
{
sum += list[i];
}
else
++count;
}
else
{
sum += list[i];
}
}
return (double)sum / (n-count);
}
int findLowest(int list[], int n)
{
int min=list[0];
for (int i = 0; i < n; i++)
{
if (min > list[i])
{
min = list[i];
}
}
return min;
}
int findHighest(int list[], int n)
{
int max = list[0];
for (int i = 0; i < n; i++)
{
if (max < list[i])
{
max = list[i];
}
}
return max;
}
int countAboveAverage(int list[], int n, double average)
{
int aboveAvg = 0;
for (int i = 0; i < n; i++)
{
if (list[i] > average)
aboveAvg++;
}
return aboveAvg;
}
void displayScores(int list[], int n)
{
cout << "All scores: " << endl;
for (int i = 0; i < n; i++)
{
if ( (i % 10) >= 1)
{
cout << list[i] << " ";
}
else
{
cout << " "<<list[i] << " ";
}
}
}
char findLetterGrade(double grade)
{
char ret;
if (grade >= 90 && grade <= 100)
ret = 'A';
if (grade >= 80 && grade < 90)
ret = 'B';
if (grade >= 70 && grade < 80)
ret = 'C';
if (grade >= 60 && grade < 70)
ret = 'D';
if (grade < 60)
ret = 'F';
return ret;
}
void displayHistogram(int list[], int n)
{
int countA=0, countB=0, countC=0, countD=0, countF=0;
for (int i = 0; i < n; i++)
{
char grade = findLetterGrade(list[i]);
if ( grade == 'A')
++countA;
if (grade == 'B')
++countB;
if (grade == 'C')
++countC;
if (grade == 'D')
++countD;
if (grade == 'F')
++countF;
}
cout << "A: ";
for (int i = 0; i < countA; i++)
{
cout << "*";
}
cout << " B: ";
for (int i = 0; i < countB; i++)
{
cout << "*";
}
cout << " C: ";
for (int i = 0; i < countC; i++)
{
cout << "*";
}
cout << " D: ";
for (int i = 0; i < countD; i++)
{
cout << "*";
}
cout << " F: ";
for (int i = 0; i < countF; i++)
{
cout << "*";
}
cout << endl;
}
void displayDuplicateScores(int list[], int n)
{
int score;
struct occurence
{
int number;
int count;
};
struct occurence occurances[100] = { 0 };
for (int i = 0; i < n; i++)
{
score = list[i];
for (int j = 0; j < n; j++)
{
if (score == list[j])
{
occurances[i].number = score;
occurances[i].count++;
}
}
}
cout << "Score occurences counts >1 :" << endl;
int j = 1;
cout << occurances[0].number << ": " << occurances[0].count << " ";
for (int i = 1; i < n; i++)
{
if (occurances[i].count > 1)
{
if (j % 5 >= 1)
cout << occurances[i].number << ": " << occurances[i].count << " ";
else
{
cout << endl;
cout << occurances[i].number << ": " << occurances[i].count << " ";
}
++j;
}
}
}
/*output:
How many scores do you want to process: 100
All scores:
77 80 74 68 74 97 67 71 2 86
24 100 22 86 85 76 78 72 79 84
97 83 74 71 78 74 77 89 81 88
85 42 66 72 78 75 9 70 94 8
83 64 70 76 88 79 83 88 82 81
85 78 98 66 76 85 82 79 63 12
60 76 77 74 82 70 74 70 97 97
77 73 79 91 88 86 96 97 99 75
91 87 73 21 97 56 70 21 79 74
64 79 88 94 87 83 73 91 96 100 Average of all scores: 74.93
Number of scores above this average: 63
Lette grade for all scores: C
Average of all scores without lowest: 75.6667
Number of scores above this average: 61
Minimum of all scores : 2
Maximum of all scores : 100
Displaying histogram for 100 scores:
A: *****************
B: **************************
C: ***************************************
D: ********
F: **********
Score occurences counts >1 :
77: 4 74: 7 74: 7 97: 6 71: 2
86: 3 100: 2 86: 3 85: 4 76: 4
78: 4 72: 2 79: 6 97: 6 83: 4
74: 7 71: 2 78: 4 74: 7 77: 4
81: 2 88: 5 85: 4 66: 2 72: 2
78: 4 75: 2 70: 5 94: 2 83: 4
64: 2 70: 5 76: 4 88: 5 79: 6
83: 4 88: 5 82: 3 81: 2 85: 4
78: 4 66: 2 76: 4 85: 4 82: 3
79: 6 76: 4 77: 4 74: 7 82: 3
70: 5 74: 7 70: 5 97: 6 97: 6
77: 4 73: 3 79: 6 91: 3 88: 5
86: 3 96: 2 97: 6 75: 2 91: 3
87: 2 73: 3 21: 2 97: 6 70: 5
21: 2 79: 6 74: 7 64: 2 79: 6
88: 5 94: 2 87: 2 83: 4 73: 3
91: 3 96: 2 100: 2
Thanks for using Grade Processor application.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.