|
|
|
|
|
|
|
Problem-solving case study Weighted Average of Test Scores |
|
|
|
|
|
|
|
|
Problem: Find the weighted average of three test scores. The data for each test is a score (an integer number) followed by its associated weight (a floating point number); each pair of numbers is on a separate line. The data is stored in a file called scoreFile. |
|
|
|
|
|
|
|
|
Input: Three lines of data, each listing a test score (integer) and weight (floating point). |
|
|
|
|
|
|
|
|
Output: Print the input data with headings (echo printing). Print the weighted average with an explanation. All floating point values are to be displayed to two decimal places. |
|
|
|
|
|
|
|
|
Discussion: It is common to give different weights to tests in order to arrive at a student's grade in a course. For example, if two tests are worth 30 percent each and a final exam is worth 40 percent, we multiply the first test grade by 0.30, the second test grade by 0.30, and the final grade by 0.40. We then add these three values to get a weighted average. We use this by-hand algorithm to solve the problem. |
|
|
|
|
|
|
|
|
Because the data is going to be read from a file, we have to #include the header file fstream.h, declare an input file stream, prepare the file for reading (open it), and remember to use the file stream instead of cin in the input statements. |
|
|
|
|
|
|
|
|
Assumptions: The three weights add up to 1.00, and the input data is correct (checking for erroneous input data is not done). |
|
|
|
|
|
|
|
|
Prepare file for reading
Get data
Print data
Find weighted average
Print weighted average |
|
|
|
|
|
|
|
|
|
Prepare File for Reading Level 1 |
|
|
|
|
|