Please write your own source code and make sure that the code compiles. C++ Prog
ID: 3840857 • Letter: P
Question
Please write your own source code and make sure that the code compiles. C++ Programming. If possible, please also include comments on the source code.
Grade Calculator: GradeCalculator.cpp
Write a program that reads student scores, gets the best score (BestScore), and then assigns letter grades based on the following scheme:
A: [BestScore, BestScore – 10]
B: (BestScore – 10, BestScore – 20]
C: (BestScore – 20, BestScore – 30]
D: (BestScore – 30, BestScore – 40]
F: Lower than BestScore – 40.
The program should prompt the user to enter the total number of students, then prompt the user to enter all of the scores, and conclude by displaying the letter grades. Here is a sample run:
Enter the number of students: 4
Enter 4 scores: 40 55 70 58
Student 0 – Score: 40, Letter: C
Student 1 – Score: 55, Letter: B
Student 2 – Score: 70, Letter: A
Student 3 – Score: 58, Letter: B
Explanation / Answer
#include<iostream.h>
void main(){
int *array = NULL;
int n; // holds the number of scores
int max; // this holds the best score i.e the max of all the scores
char grade; //holds the grade
cout << " Enter the number of students:";
cin >> n;
array = new int[n];
cout << "Enter " << n << " scores:";
max = 0;
for (int i = 0; i<n; i++){
cin >> array[i];
if (array[i] > max)
max = array[i];
}
for (int i = 0; i<n; i++){ // the below conditions are as given in the question. Scores are compared with max to set //the grade
if (array[i] <= max && array[i] >= (max - 10))
grade = 'A';
if (array[i] < (max - 10) && array[i] >= (max - 20))
grade = 'B';
if (array[i] < (max - 20) && array[i] >= (max - 30))
grade = 'C';
if (array[i] < (max - 30) && array[i] >= (max - 40))
grade = 'D';
if (array[i] < (max - 40))
grade = 'F';
cout << "Student " << i << "-" << "Score:" << array[i] << "," << "Letter:" << grade << endl;
}
delete [] array; //deallocating the memory
}