Fill in the code for the function below called
containsAvgValue. It takes as input a
float array of any length and returns 1 if the
array happens to contain a value that equals the average of all the
values in the array or 0 otherwise. For example,
when give the array [2.0, 2.0] it should return 1 since the average
value is 2.0 and the array contains that value. To help you, I’ve
included a function that computes the average of a float
array.
float calcAverage(float x[ ], int len)
{
/*CODE NOT SHOWN
FUNCTION RETURNS THE AVERAGE
OF THE VALUES IN ARRAY X. LEN IS
THE
LENGTH OF X.*/
...
}
(float x[ ], int len)
{
float avg;
=
calcAverage(x, );
int i;
int retval;
retval=0;
i = 0;
while(i< )
{
if( == )
{
=1;
}
i = i +1;
}
return ;
}
If you have any problem with the code feel free to comment.
Program
#include <stdio.h>
float calcAverage(float x[ ], int len)
{
float sum =0.0;
//calculating sum
for(int i=0; i<len; i++)
{
sum += x[i];
}
//calculating average of the array
float avg = sum / len;
//checking if the array contains its average
for(int i=0; i<len; i++)
{
if(avg == x[i])
return 1;
}
return 0;
}
int main()
{
float x[] = {2.0, 2.0};
//testing the method
if(calcAverage(x, 2) == 1)
printf("Array contain its average");
else
printf("Array does not contain its average");
printf("\n");
return 0;
}
Output
Get Answers For Free
Most questions answered within 1 hours.