C++ Write a program that calculates the total grade for N classroom exercises as
ID: 3850988 • Letter: C
Question
C++
Write a program that calculates the total grade for N classroom exercises as a percentage. I need to use the Decimal Format class to output the value as a percent. The user should input the value for N followed by each of the N scores and totals. Calculate the overall percentage (sum of the total points earned divided by the total points possible) and output it as a percentage. Sample output:
How many exercises to input? 3
Score received for exercise 1: 10
Total points possible for exercise 1: 10
Score received for exercise 2: 7
Total points possible for exercise 2: 12
Score received for exercise 3: 5
Total points possible for exercise 3: 8
Your total is 22 out of 30, or 73.33%.
Explanation / Answer
NOTE: I have used iomanip header file for formatting decimal places.
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int i, n, st_total, act_total;
int score[100], total[100];
double per;
cout << "Enter how many exercises to input? ";
cin >> n;
for(i = 0; i < n; i++){
cout << "Score received for exercise "<< i << ": ";
cin >> score[i];
cout << "Total points possible for exercise "<< i << ": ";
cin >> total[i];
cout << endl;
}
for(i = 0; i < n; i++){
st_total += score[i];
act_total += total[i];
}
per = (float(st_total) / act_total) * 100;
cout << "Your total is "<< st_total << " out of " << act_total << ", or "<< setprecision(4) << per << "%";
return 0;
}
Execution and output:
Enter how many exercises to input? 3
Score received for exercise 0: 10
Total points possible for exercise 0: 10
Score received for exercise 1: 7
Total points possible for exercise 1: 12
Score received for exercise 2: 5
Total points possible for exercise 2: 8
Your total is 22 out of 30, or 73.33%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.