Write a function, named avg, that receives an array of integer values and return
ID: 3822776 • Letter: W
Question
Write a function, named avg, that receives an array of integer values and returns the average of those values in that array. Since this function is computing the average, it should return a float. Remember, when giving an array to a function you must also give the function the size of the array because it will not be able to determine the size of an array on its own. Test your function with three different integer arrays of sizes, 5, 7, and 10. You do not have to ask the user for values. Since the function returns a value you should assign its result to a variable and print it out. Perform this is code blocks using C.
Explanation / Answer
#include <stdio.h>
float avg(int data[], int size)
{
int i;
int sum;
float aver;
sum = 0;
for (i=0; i<size; i++)
{
sum = sum + data[i];
}
aver = sum/size;
return aver;
}
void main()
{
int data1[5] = {1,2,3,4,5};
int data2[7] = {1,2,3,4,5,6,7};
int data3[10] = {1,2,3,4,5,6,7,8,9,10};
float avg1, avg2, avg3;
avg1 = avg(data1,5);
avg2 = avg(data2,7);
avg3 = avg(data3,10);
printf("%f %f %f ", avg1, avg2, avg3);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.