Starting from:
$30

$24

Week 5 Weighted Average Solution

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


































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




End If




Test Plan




This program is complex enough to require a variety of test cases – here are a few telling ones:




Test Case
Input: value weight pairs
Expected Output
Notes








1
0141304050
Average = 2
Average 0 and 4






Values 3, 4, 5 just help keep input






easier to follow - since weights are 0,






those values are ignored.








2
1020300151
Average = 2.5
Average of 0 and 5, even weights






Values 1, 2, 3 are ignored








3
01203304050
Average = 15
Weighted average of 0 and 20,






with ¼ to 0 and ¾ to 20








4
2121315151
Average = 3.4
Even weights, so 17/5 = 3.4








5
85 .1 88 .1 93 .2 87 .3 96 .3
Average = 90.8
Weighted average of grades, using






fractions








6
85 10 88 10 93 20 87 30 96 30
Average = 90.8
Same using percentages
















































































Pseudocode




This program will calculate the average of value-weight pairs of doubles



Declare variables



Declare count as Integer




Declare avg, value, weight, sum, sumw as doubles




//Initialize values




Set count=0




Set sum = 0.0




Set sumw = 0.0




Set avg = 0.0;




Loop through 5 value-weight pairs as doubles While count < 5



Print “Enter a value and its weight: ”




Input value, weight if (weight =0)




sum = sum + value*weight sumw = sumw + weight count=count+1

else




Print (“Weight must be positive”);




End if End While




Calculate average



avg = sum/sumw




// Print results




Print “Average is “ + avg



























































































3

C Code




The following is the C Code that will compile to execute in the online compilers.




C code



This program will calculate



the weighted average of 5 numbers.



Developer: Faculty CMIS102



Date: Jan 31, XXXX



#include <stdio.h




int main () {




/* variable definition: */




int count;




double avg, value, weight, sum, sumw; /* Initialize */




count = 0;




sum = 0;




sumw = 0;




avg = 0.0;




Loop through to input values while (count < 5) {



printf("Enter a value and its weight: ");




use %lf for double, %f for float scanf("%lf %lf", &value, &weight); if (weight = 0) {



sumw = sumw + weight;




sum = sum + value * weight; count = count + 1;




}




else {




printf("Weight must be positive\n"); } // end if weight ok




} // end reading input values and weights




Calculate avg if sumw is not 0 avg = sum / sumw; printf("average is %lf\n " , avg ); return 0;



} // end main























































4

Setting up the code and the input parameters in repl.it:




This screen shot shows the program running with the input values of 2 1 2 1 3 1 5 1 5 1, with the resulting weighted average of 3.4. In this case, since all the weights are equal, the result is the simple average of the numbers {2, 2, 3, 5, 5} = 17/5.





















































































































































































5

Learning Exercises for you to complete




Demonstrate you successfully followed the steps in this lab by preparing screen captures of you running the lab as specified in the Instructions above.



Change the code to average 10 value-weight pairs of doubles as opposed to 5. Support your experimentation with screen captures of executing the new code.



Prepare a new test table with at least 3 distinct test cases listing input and expected output for the new code you created averaging 10 doubles.



What happens if you entered a value other than a double? (For example a string). Support your experimentation with screen captures of executing the code.



Modify the code to allow the user to enter any number of pairs and calculate the average. In other words, the user could enter any number of pairs as long as the weight is positive. (Hint: You can prompt the user for how many they want to enter. Or; you could use a sentinel value to trigger when the user has completed entering values). Prepare a new test table with at least 3 distinct test cases listing input and expected output for the code you created. Support your experimentation with screen captures of executing the new code.















































































































6

Grading guidelines




Submission
Points




Demonstrates the successful execution of this Lab within an
20
online compiler. Provides supporting screen captures.






Modifies the C code average 10 doubles as opposed to 5.
20
Support your experimentation with screen captures of


executing the new code.






Prepares a new test table with at least 3 distinct test cases
10
listing input and expected output for the new code you


created averaging 10 doubles.






Describes what happens if you entered a value other than a
10
double? Support your experimentation with screen captures


of executing the code.






Modifies the C code to allow the user to enter any number of
30
positive doubles and calculate the average. Provides a new


test table with at least 3 distinct test cases listing input and


expected output for the code you created. Support your


experimentation with screen captures of executing the new


code.






Document is well-organized, and contains minimal spelling
10
and grammatical errors.






Total
100




































































































7

More products