Test Average

Writing Exams
Creative Commons License photo credit: ccarlstead

This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (3) was titled “Expressions and Interactivity”.

I post these old academic challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it.

3.3: 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.

My solution:

/* Chapter 3 Challenge 3
Written by Kirk Hingsberger
September 26, 2007
Test Average
*/

#include
#include
using namespace std;

int main()
{
float test1,test2,test3,test4,test5;
float avg;

cout << "Please enter five test scores, each separated by a space." << endl;
cin >> test1 >> test2 >> test3 >> test4 >> test5;
avg = (test1 + test2 + test3 + test4 + test5) / 5.0;

cout << "The average of your five test scores is ";
cout << setprecision(1) << fixed << avg << endl;

return 0;
}

Here is the capture script I turned in for grading:

Script started on Thu 27 Sep 2007 01:21:11 PM MDT
$ cat chap3chlg3.cc
/* Chapter 3 Challenge 3
Written by Kirk Hingsberger
September 26, 2007
Test Average
*/

#include
#include
using namespace std;

int main()
{
float test1,test2,test3,test4,test5;
float avg;

cout << "Please enter five test scores, each separated by a space." << endl;
cin >> test1 >> test2 >> test3 >> test4 >> test5;
avg = (test1 + test2 + test3 + test4 + test5) / 5.0;

cout << “The average of your five test scores is “;
cout << setprecision(1) << fixed << avg << endl;

return 0;
}
$ g++ -o chap3chlg3 chap3chlg3.cc -s
$ chap3chlg3
Please enter five test scores, each separated by a space.
76
67 67 67 67
The average of your five test scores is 68.8
$ exit
exit

Script done on Thu 27 Sep 2007 01:22:07 PM MDT

  • Share/Bookmark




Leave a Reply