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
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
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...
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
Question Recursive Maximum: In this question you must develop and use a recurive function to find...
Question Recursive Maximum: In this question you must develop and use a recurive function to find the maximum value in array of integers. Complete the C program, Maximum.c, by implementing the recursive function maximumValue, and the regular function max, and completing the main function using the comments provided. Open the file Maximum.c, complete, compile and run it. When you are sure it is correct, include the c file in your final submission. Note that the function max is not recursive....
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
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...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
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...
Write in C++ program and write a function named add. It should accept two const double...
Write in C++ program and write a function named add. It should accept two const double type references as arguments. The first argument should be named num1; the second argument should be named num2. The function should return by value of num1 plus num2.
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...