Average Rainfall

rain drops in PUDDLE blurrily mirrors blue sky, brown tree-branches and red-white ringroad-palais in Vienna.
Creative Commons License photo credit: quapan

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

3.4: Write a program that calculates the average rainfall for three months. The program should ask the suer to enter the name of each month, such as June or July, and the amount of rain (in inches) that fell each month. The program should display a message similar to the following:

The average rainfall for June, July, and August is 6.72 inches.

My solution:

/* Chapter 3 Challenge 4
Written by Kirk Hingsberger
September 18, 2007
Average Rainfall
*/

#include
using namespace std;

int main()
{
float m1rain, m2rain, m3rain, avg;
char m1[10], m2[10], m3[10];

cout << "Let's average three consecutive months' rainfall.\n";
cout << "\nEnter the first month name, a single space,\nthat month's ";
cout << "recorded rainfall in inches, then hit enter." << endl;
cin >> m1 >> m1rain;

cout << "\nEnter the second month name, a single space,\nthat month's ";
cout << "recorded rainfall in inches, then hit enter." << endl;
cin >> m2 >> m2rain;

cout << "\nEnter the third month name, a single space,\nthat month's ";
cout << "recorded rainfall in inches, then hit enter." << endl;
cin >> m3 >> m3rain;

avg = (m1rain + m2rain + m3rain) / 3;

cout << "The average rainfall for " << m1 << ", " << m2 << ", and " << m3;
cout << " is " << avg << " inches."<< endl;

return 0;
}

Here is the capture script I turned in for grading:

Script started on Thu 20 Sep 2007 09:48:38 AM MDT
$ cat chap3chlg4.cc
/* Chapter 3 Challenge 4
Written by Kirk Hingsberger
September 18, 2007
Average Rainfall
*/

#include
using namespace std;

int main()
{
float m1rain, m2rain, m3rain, avg;
char m1[10], m2[10], m3[10];

cout << "Let's average three consecutive months' rainfall.\n";
cout << "\nEnter the first month name, a single space,\nthat month's ";
cout << "recorded rainfall in inches, then hit enter." << endl;
cin >> m1 >> m1rain;

cout << "\nEnter the second month name, a single space,\nthat month's ";
cout << "recorded rainfall in inches, then hit enter." << endl;
cin >> m2 >> m2rain;

cout << "\nEnter the third month name, a single space,\nthat month's ";
cout << "recorded rainfall in inches, then hit enter." << endl;
cin >> m3 >> m3rain;

avg = (m1rain + m2rain + m3rain) / 3;

cout << “The average rainfall for ” << m1 << “, ” << m2 << “, and ” << m3;
cout << ” is ” << avg << ” inches.”<< endl;

return 0;
}
$ g++ -o chap3chlg4 chap3chlg4.cc -s
Let’s average three consecutive months’ rainfall.

Enter the first month name, a single space,
that month’s recorded rainfall in inches, then hit enter.
January 100

Enter the second month name, a single space,
that month’s recorded rainfall in inches, then hit enter.
February 200

Enter the third month name, a single space,
that month’s recorded rainfall in inches, then hit enter.
March 300
The average rainfall for January, February, and March is 200 inches.
$ exit
exit

Script done on Thu 20 Sep 2007 09:49:53 AM MDT

  • Share/Bookmark




Leave a Reply