Question: Objective: Function (with arguments and return). File:hw2 sum2avg.c Write a function that takes as argument a sum and a number, N. You assume that sum
Objective: Function (with arguments and return). File:hw2 sum2avg.c Write a function that takes as argument a sum and a number, N. You assume that sum holds the sum of N numbers. Thus by dividing sum by N you compute the average. The function must calculate, print and retur the average produced by sum/N. The function signature is float avg (int sum, int n); Note that you will need to explicitly cast one of the arguments to float in order to be able to get a float result Otherwise the decimals will be lost. Here is an example of explicit casting to float: a+((float)b). In main() call this function 4 times, with the hardcoded values: a) 55 for sum and 10 for N b) 9 for sum and 3 for N c) O for sum and for N d) 10 for sum and for N Why does the call with this data produce a strange result? Give your explanation as a comment in your code You main() function should only have the 4 function calls in it, nothing else. Sample run 3 This program calls the sum2avg function 4 times with hardcoded data and saves the results and prints them at the end. 55/10 - 5.50 9/3 - 3.00 075 - 0.00 10/0 - 1.0 The 4 averages are: 5.500, 3.000, 0.000, 1.10 In the above output, the lines: This program calls the sum2avg function 4 times with hardcoded data and saves the results and prints them at the end. and The 4 averages are: 5.500, 3.000, 0.000, 1. #10 were printed in main), and the lines: 55/10 - 5.50 9/3 - 3.00 0/5 -0.00 10/0 = 1. Were printed by the function (each function call printed one line). E.g, when the call for $5 and 10 executed, it printed the line 55/10 - 5.50 Grading 3pts - Indentation (make sure the code within the sum avg function is also indented, just like the one in main(). but not the function header itself- also like main() Ipts - Program description at the top given as a comment 11 pts - 4 calls in maino, the results are saved and printed in main(): 4 pts for calls, 4 pts for results saved. 3 pes for results printed with 3 decimals, 8pts - Function definition: 4pts - print correct (with 2 decimals): 2pts-return, 2pts-use the arguments (do not overwrite them) 2pts - Explanation for cased)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
