Skip to content

Instantly share code, notes, and snippets.

@Abiola-Farounbi
Created August 27, 2021 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abiola-Farounbi/9115c065e100061e59337873defeae5a to your computer and use it in GitHub Desktop.
Save Abiola-Farounbi/9115c065e100061e59337873defeae5a to your computer and use it in GitHub Desktop.
To calculate the Mean and Standard Deviation of five values
#include <iostream>
#include <cmath>
using namespace std;
// Function Prototype
void FillArray(float ArrayValues[]);
void CalculateMeanandStddev(float ArrayValues[], double *Mean_ptr, double *Stddev);
void PrintResult(float ArrayValues[], double Mean, double Stddev);
// Main Function
int main()
{
// Intialize the variables
float ArrayValues[5];
double Mean, Stddev;
char answer;
cout << "Welcome to Mean and Standard Deviation Application" << endl ;
cout << "To calculate the Mean and Standard Deviation of five values " << endl;
do{
// Function Call
FillArray(ArrayValues);
CalculateMeanandStddev(ArrayValues,&Mean,&Stddev);
PrintResult(ArrayValues, Mean, Stddev);
// To Calculate again
cout << endl << "Do you want to do calculate again? (Y = Yes & N = No)" << endl;
cin >> answer;
}
while(answer == 'Y');
system("PAUSE");
return 0;
}
void FillArray(float ArrayValues[]){
cout << "Enter Five values" << endl;
for(int i = 0; i < 5; i++){
cout << "Enter value for " << i+1 << " - " ;
cin >> ArrayValues[i];
cin.ignore();
}
}
void CalculateMeanandStddev(float ArrayValues[], double *Mean_ptr, double *Stddev){
double totalValues = 0;
double totalDifference = 0;
for(int i =0; i <5; i++)
{
totalValues += ArrayValues[i];
}
cout.precision(2);
cout.setf(ios::fixed);
*Mean_ptr= (totalValues / 5);
for(int i =0; i <5; i++)
{
totalDifference += pow(( ArrayValues[i] - *Mean_ptr ),2);
}
cout.precision(2);
cout.setf(ios::fixed);
*Stddev = (totalDifference / 5);
}
void PrintResult(float ArrayValues[], double Mean, double Stddev)
{
cout << endl << "The values are : " ;
for(int i =0; i <5; i++)
{
cout << ArrayValues[i] << ", ";
}
cout << "The Mean is: " << Mean << endl;
cout << "The Standard Deviation is " << Stddev << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment