Example for an odd sized array 5 7 8 9 2 sum 310 average me

Example for an odd sized array: {5, 7, 8, 9, 2} sum = 31.0 average (mean) = 6.2 Variance = [(5 - 6)^2 + (7 - 6.2)^2 + (8 - 6.2)^2 + (9 - 6.2)^2 + (2 - 6.2)^2]/5.0 = [(1.44) + (0.64)+ (3.24)+ (7.84)+ (17.64)]/5.0 = 30.8/5.0 = 6.16 Standard Deviation = the squareroot of 6.16 = 2.482 The median for {2, 5, 1, 8, 9} is 7 Write a C++ program that declares an array like so: double *grades; Your program should then prompt the user for an input file name like such: Enter an input file name: grades.txt Next your program should use the file name entered in the prompt and attempt to open the input file. If your program is unable to open the input file the program should display an error message saying so and stop execution. If the program successfully opens the file it should read the first line in the file as an integer indicating how many real numbers follow in the file. Your program should then dynamically allocate an array of doubles to hold the real numbers. Your program should then read the real numbers into the dynamically allocated array. You can assume the file will have at least two real numbers. You should program your solution with the possibility that the most real numbers in the file can be 2^32-1. The actual amount of real numbers will be much less. Your dynamically allocated array should allocate exactly the amount of space needed. After you read the real numbers into the dynamically allocated array your program should compute the following properties on the set of numbers: the sum, the average (mean), the variance, the standard deviation, and the median. Don\'t try to do the entire program at once. Build a little, test a little. Your program should contain 5 functions(methods). One method to compute the sum, a method to compute the average (mean), a method to compute the variance, a method to compute the standard deviation, and finally a method to compute the median. The method to compute the median should work regardless of whether n is even or odd, where n is the length of the array.

Solution

Here is the code for you:

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
double sumOfNumbers(double *array, int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
sum += *(array + i);
return sum;
}
double averageOfNumbers(double *array, int n)
{
return sumOfNumbers(array, n) / n;
}
double varianceOfNumbers(double *array, int n)
{
double sum = 0;
double avg = averageOfNumbers(array, n);
for(int i = 0; i < n; i++)
sum += pow(*(array + i) - avg, 2);
return sum / n;
}
double standardDeviationForVariance(double variance)
{
return sqrt(variance);
}
int main()
{
string fileName;
cout<<\"Enter the name of the file to read from: \";
cin>>fileName;
ifstream fin;
fin.open(fileName);
if(!fin.is_open())
{
cout<<\"Unable to open the file.\"<<endl;
return 0;
}
int numOfValues;
fin>>numOfValues;
double *array = (double *)malloc(sizeof(double) * numOfValues);
for(int i = 0; i < numOfValues; i++)
fin>>*(array + i);
double sum = sumOfNumbers(array, numOfValues);
double avg = averageOfNumbers(array, numOfValues);
double var = varianceOfNumbers(array, numOfValues);
double std = standardDeviationForVariance(var);
cout<<\"The sum of values in the array is: \"<<sum<<endl;
cout<<\"The average of values in the array is: \"<<avg<<endl;
cout<<\"The variance of values in the array is: \"<<var<<endl;
cout<<\"The standard deviation of values in the array is: \"<<std<<endl;
}

 Example for an odd sized array: {5, 7, 8, 9, 2} sum = 31.0 average (mean) = 6.2 Variance = [(5 - 6)^2 + (7 - 6.2)^2 + (8 - 6.2)^2 + (9 - 6.2)^2 + (2 - 6.2)^2]/
 Example for an odd sized array: {5, 7, 8, 9, 2} sum = 31.0 average (mean) = 6.2 Variance = [(5 - 6)^2 + (7 - 6.2)^2 + (8 - 6.2)^2 + (9 - 6.2)^2 + (2 - 6.2)^2]/

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site