Question

1) Develop a C++ function that determines the average value of an array of type double...

1) Develop a C++ function that determines the average value of an array of type double elements

  • double GetAverage(double array[], int size)
  • The function should accept as input an array of double values
  • The function should accept as input the number of elements in the array of double values
  • The function should return a double value which is the array's average value

2) Develop a C++ function that determines the variance of an array of type double elements

  • double GetVariance(double array[], int size)
  • The function should accept as input an array of double values
  • The function should accept as input the number of elements in the array of double values
  • The function should return a double value which is the array's variance
  • Use the average value function you created above to compute the variance value
  • σ2 = variance
  • ∑ (X - µ)2 = The sum of (X - µ)2 for all data points
  • X = individual data points
  • µ = average/mean of the data points
  • N = number of data points

3) Develop a C++ program that determines and displays the average value and variance of an array of type double elements

  • For your testing, please copy/paste the following data into your program:

const int NUMBER_OF_TEST_SCORES = 19;
double TestScores[NUMBER_OF_TEST_SCORES] = { 180, 122.5, 153, 180, 180, 157.5, 178, 178, 198, 168, 150, 162, 154, 188, 195, 180, 176, 168, 153 };

  • With this data, your results should display as follows:
    • The average is 169.5263158
    • The variance is 316.3808864

Homework Answers

Answer #1

CODE -

#include<bits/stdc++.h>

using namespace std;

// Function to compute average of the array.

double GetAverage(double array[], int size)

{

    float sum = 0;

    // This loop computes sum of all elements of the array.

    for(int i=0;i<size;i++)

        sum += array[i];

    return (sum/size);          // Returning sum divided by size of array which is equal to average of the array.

}

// Function to compute variance of the array.

double GetVariance(double array[], int size)

{

    double sum_sq_diff = 0;

    double avg = GetAverage(array,size);        // Calling GetAverage function to get the average of the array.

    //This loop computes the Sum Squared Differences with mean.

    for(int i=0;i<size;i++)

        sum_sq_diff += ((array[i] - avg) * (array[i] - avg));

    return (sum_sq_diff/size);                  // Returning sum_sq_diff divided by size of array which is equal to Variance.

}

// Main function to test above functions.

main()

{

    const int NUMBER_OF_TEST_SCORES = 19;

    double TestScores[NUMBER_OF_TEST_SCORES] = { 180, 122.5, 153, 180, 180, 157.5, 178, 178, 198, 168, 150, 162, 154, 188, 195, 180, 176, 168, 153 };

    cout << "The average is " << fixed << setprecision(7) << GetAverage(TestScores,NUMBER_OF_TEST_SCORES) << endl;

    cout << "The variance is " << fixed << setprecision(7) << GetVariance(TestScores, NUMBER_OF_TEST_SCORES) << endl;

    // setprecision was used so that the values don't get trimmed after decimal places.

}

SCREENSHOT -

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
/* 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...
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...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an...
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. you have to ask the user for the size of the array and ask the user to input the values in the main. The function has the following prototype. int maximum ( int *p [ ], int n);
Develop a C++ program that determines the largest and second largest positive values in a collection...
Develop a C++ program that determines the largest and second largest positive values in a collection of data Prompt the user to enter integer values until they enter any negative value to quit You may presume the user will input at least two valid integer values Create a loop structure of some sort to execute this input cycle Maintain the largest and second largest integer as the user inputs data This logic should be placed inside your loop structure Arrays...
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...
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Write in C++ program and write a function named computeCostPlusGST. It should accept two double type...
Write in C++ program and write a function named computeCostPlusGST. It should accept two double type variable as arguments. The first argument should be named cost; the second argument should be named rate. The argument rate should have a default value of 0.15. The function should return by value the value of cost plus the product of cost and rate
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT