/* data is an array of doubles. len is the number of elements in
the array.
The return value should be the Average of the doubles in the data
array.
The Average is defined as the average value from among the
elements, e.g.
if the data array was { 6.0, 3.0, 2.5, 20.7 }, the
function should return
the value 2.5. */
double average(double data[], int len) {
double min_val = data[0];
/* TODO: Write the code that goes here */
return Avg_val;
}
Please answer the above code in C-programming
This question seems wrong.
2.5 can only be returned if the function returns the minimum value because the average of the array is 8.05 and not 2.5
Here is the program for average:
#include <stdio.h>
double average(double data[], int len)
{
double Avg_val=0.0;
for(int i=0; i<len; i++)
{
Avg_val = Avg_val + data[i];
}
Avg_val = Avg_val/len;
return Avg_val;
}
int main()
{
double data[]= { 6.0, 3.0, 2.5, 20.7 };
int len = 4;
printf("%.2f", average(data, len));
return 0;
}
Output:
Different code should be written for the minimum value .
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
Get Answers For Free
Most questions answered within 1 hours.