Question

Fill in the code for the function below called containsAvgValue. It takes as input a float...

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  ;

}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function called read floats(int n) that reads n float values from the user into...
Write a function called read floats(int n) that reads n float values from the user into an array and return the array back to the caller. Recall that a regular array will get deallocated when the function returns. (C++ the simplier the better)
Given the following function prototypes: float asciiAvg(char mystring[], int length); Write the function definition. The asciiAvg...
Given the following function prototypes: float asciiAvg(char mystring[], int length); Write the function definition. The asciiAvg function should return the average of the ascii values for the characters in the array mystring of length characters.
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
Complete following function which receives an array of integers and the length of the array, and...
Complete following function which receives an array of integers and the length of the array, and then returns the sum of all the positive numbers in the array. For example, if an array that is passed to this function contains following numbers: -1, 2, 0, 3, 4, -3, 0, 2, 0, and then the return value of the function should be 11. Will this function be working correctly? Yes or No? int sumPositive(int a[],int length) { int s=0;     for(int...
float sum( float x, float y, float z ) ​program in c++
Here are the function prototypes for your homeworkWrite out the actual functions themselves Also, create a main function that demonstrates that all four functions work The user should be able to provide four values to your program (three floats and an int) Your program should then use your four new functions and also display the results to the user NONE of these functions should print values themselvesfloat sum( float x, float y, float z ); // returns the sum of three floatsfloat mean( float...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
""" ''' Write a python code to push all zeors to the end of an array...
""" ''' Write a python code to push all zeors to the end of an array ''' import numpy as np def Move_a(i):    num = len(a)    for k in range (i, num-1): a[k] = a[k+1] a[num-1] = 0    return a a = np.array([0,1,4,7,0,9,12,0,0,15,0,21]) #length of array (len) num = len(a) print (num) for i in range(0,num): if (a[i] == 0): #Functioon call to Move_a() a = Move_a(i)       print ("the array looks like") print (a) My...
C++ What is the value of average after the following code executes? double average; average =...
C++ What is the value of average after the following code executes? double average; average = 4.0 + 3.0 * 2.0 + 1.0; Consider the following function: int compute(int n) { int x = 1; for (int i = 1; i <= n; i++) { x *= i; } return x; } What is the returned value for the following function call? int value = compute(4); What is the value of number after the following statements execute? int number; number...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...