Write a C++ Program to calculate the average and standard deviation of a list of
ID: 3671826 • Letter: W
Question
Write a C++ Program to calculate the average and standard deviation of a list of numbers. You may work in pairs but with a new partner that you have not worked together before this lab.
The standard deviation of a list of numbers is a measure of how much the numbers differ from the average. A smaller standard deviation indicates that the average is an accurate reflection of the tendencies of the I ist. That is, when the standard deviation is small, the numbers in the List are grouped close to the average.
The standard deviation of a list of N numbers, xi, X2, X3, . • •XN, can be computed as follows. Let a be the average of the N numbers.
Let v be the sum (xi - a )2 + (x2 - a )2 + (x3 - a )2 + . . . + (x N - a )2 .
Extra Libraries:
#include <cmath> double sqrt(double r) to calculate square root of r.
#include <f stream> for an instance of ifstream and of stream to read from a fi le and write to a file.
Here is the list of functions your program should implement.
Part I : Read the size and the list from "input.txt"
Your program should first read the size of the list from the file, "input.txt". Then, the program should allocate a double type dynamic array with the same size as the list to hold the numbers in the list. It will then read the list one by one from the same input file.
Part I I : Calculate the average and standard deviation of the list
Design and implement fol I owi ng two functions that wi I l calculate the average and stqandard deviation of list aa respectively. Call them in proper order in your main function.
double average(double* aa, int n);
double standardDeviation(double*aa, int n, double avg);
Part Ill : Output the size of the lid, the list itself, and the average and standard deviation of the list to both the screen and to a file named, "output.txt"
Implement following function and called it twice, once with cout as parameter for out, another with an instance of of stream as parameter.
void output(double* aa, int n, double avg, double deviation, ostream& out);
/*output the size (n), the list (aa), and the average (avg) and the standard deviation (deviation) to the ostream out.*/
If the input contains following data : 5
then the output should lkook like :
numbers in1 the list is 5:
10.2 15.3 12.4 11.8 12.5
Average: 12.44
Standard deviation: 165
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
void output(double* aa, int n, double avg, double deviation, ostream& out);
//add the rest function declaration here (functins prototypes) 0
int main(){
string f="input.txt";
ifstream fin("input.txt");
ofstream fout("output.txt");
//declare proper variables here
int len=0; //size of the list
double* aa = NULL; //dynamic array to hold the list
double avg = 0; //local variable to hold the average
double deviation = 0; //local variable to hold the standard deviation
if(!fin.is_open()){ //check whether fin is opened correctly
cout <<"Error to read input.txt ";
exit(1);
}
//you can then proceed with reading from fin instead of cin
fin >> len; //read the size of the list from fin
//allocate dynamic array with size len
//read the list one by one from fin
fin.close(); //close the input file
//call proper functions to calculate the average and
//standard deviation and store returned values to variables
if(! fout.is_open()){// check whether fout is opened correctly
cout <<:"Error to write to output.txt ";
exit(1);
}
//you can now write to fout.
output(aa, len, avg, deviation, fout); //output to "output.txt"
output(aa, len, avg, deviation, cout); //output ot the screen
fout.close(); //close the output file
//release the dynamic array here
return 0;
}
//implement all functions here.
//Inseert the size of the list, the list, the average and standarded
//deviation to the ostream out instead of cout.
void output(double* aa, int n, double avg, double deviation, ostream& out){
out << "there are " << n << " numbers in the list." << endl;
//insert the list and the average and strandarded deviation to the ostream out
}
Explanation / Answer
Hi below i have included a C++ program which calculates Average and Standard deviation with a seperate example code for your reference to understand it better,
//Calculate Average of Numbers Using Arrays
//Calculate Standard Deviation
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.