Write a program that asks for five test scores. The program
should calculate the average
test score and display it. The number displayed should be formatted
in fixed-point notation,
with one decimal point of precision.
Display:
(1) your (1) interactive dialog with the console,
(2) the five test scores entered, and
(3) the computed average.
the pseudo code is:
you will need to include a library for the output
formatting
for specifying decimal point display:
#include <iomanip> in std namespace
assume number of test scores does not change, so
declare a constant for the number of test scores:
const int N(5)
declare memory for N number of test scores:
int t1,t2, ....
get test scores from keyboard input:
cout << "Enter Test Score 1: "
cin >> t1
echo the result for visual validation:
cout << "You entered " << t1 << "
do for N test scores ...
declare variable for sum of all scores:
int sum(0)
compute sum:
sum = t1 + ....
compute average test score
----------------------
if we should perform the operation sum/N...
we will not get the correct result because
division on two interger operands, (sum & N) ,
results in a whole number -- we lose the
fraction part of the result.
we need to cast one of the operands in its
register so we can get a floating point result.
the syntax for doing this is:
double average = static_cast<double>(sum) / N;
display results
--------------------------------------------------------------------------------------------------------------------------------------------------
SOURCE CODE
#include <iostream>
using namespace std;
int main() {
const int N=5;
int t1,t2,t3,t4,t5;
cout << "Enter Test Score 1: ";
cin >> t1;
cout << "Enter Test Score 2: ";
cin >> t2;
cout << "Enter Test Score 3: ";
cin >> t3;
cout << "Enter Test Score 4: ";
cin >> t4;
cout << "Enter Test Score 5: ";
cin >> t5;
cout << "\nYou entered " << t1 << "
"<< t2 << " "<< t3 << " "<< t4
<< " "<< t5;
float sum = 0.0;
sum = t1+t2+t3+t4+t5;
double average = static_cast<double>(sum) / N;
cout<<"\nAverage of all array elements is
"<<average;
return 0;
}
OUTPUT SCREENSHOT
please give a upvote if u feel helpful.
Get Answers For Free
Most questions answered within 1 hours.