## Description

Week 5 – Weighted Average

Overview This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, analysis, test plan, design (using pseudocode), and implementation with C code. The example provided uses sequential, selection and repetition statements.

Program Description

This program will calculate the weighted average of 5 numbers. The program will ask the user to enter 5 numbers treated as weights and 5 numbers to be averaged. The program will use a loop to input the data.

Analysis

I will use sequential, selection and repetition programming statements. I will define one integer number: count count will store how many times valid weights and values have been entered. I will define a variety of doubles: weight, value, sum, sumw, avg value will store the input value weight will store the input weight for that value sum will store the running sum of values and weights sumw will store the running of the weights avg will store the weighted average of the 5 numbers and 5 weights input. The running sum will be calculated by this formula: sum = sum + value * weight For example, if the first value and weight pair entered is 4, 1 and second pair is 10, 3: sum = sum + value*weight = 0 + 4*1 sum = 4 + 10*3 = 34 sumw = 1 + 3 = 4 Values and sum are to be input and calculated within a repetition loop: while count < 5 Input value, weight sumw = sumw + weight sum = sum + value count = count + 1 End while Avg is calculated by: avg = sum / sumw

2

A selection statement can be used inside the loop to make sure the input weight is positive.

If weight >= 0 then sumw = sumw + weight sum = sum + value * weight count = count + 1 Else report error