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 =...
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
A program is already given to you.  There are five problems in this skeleton version of the...
A program is already given to you.  There are five problems in this skeleton version of the program, each is 10 points. All you got to do is complete the missing code in each function. What the function does is clearly stated in the name of the function.   // ASSIGNMENT ON FUNCTIONS #include <stdio.h> // Problem 1: Compile with gcc func_assignment.c -Wall // There are some warnings because there is a mismatch between // data type passed and defined. // Find...
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...
Your assignment is to write a c++ function that can calculate values for any 5 th...
Your assignment is to write a c++ function that can calculate values for any 5 th order polynomial function. ?(?) = ?0 + ?1? + ?2? 2+?3? 3 + ?4? 5 + ?5? 5 Your function should accept only two parameters, the first parameter should be the value of x at which to calculate y(x). The second parameter should be an array with 6 elements that contain the coefficients a0 to a5. The output of your function should be the...
/* data is an array of doubles. len is the number of elements in the array....
/* 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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT