Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

12:14 AM 20% OO Sprint Standard Deviation The Standard DeviM sigma), which has t

ID: 3779893 • Letter: 1

Question

12:14 AM 20% OO Sprint Standard Deviation The Standard DeviM sigma), which has the folowing formula E Work out the Mean (the avenge of the numbers2 Then work out the mean ofthose squared differences. Take the square root of that ookac quiz in which they scored the following grades:9,2,5.4 12.7 & II.93 12. The mean is: R Step2.nen each number ubeact Mesraand square 25 Sepia Then work out how many. First add from the previous soep. We want to addupall the values SomIsoN, where N-20 in our because there are 20 vales Which means sum all values from X topX Wedrendly calculated (XI-7'-4ete in the previous step, so just sum them up: need divide by how many, whichissimply done uitpiyi bSTINE Example (continued) Mean of squared differences (U20) TE IV(Note this value is called the vaane) Step 4. Take the square root of that to find the Standard Deviation: a Program that Deviation using data from impur Ble ealed Use datain the ewmple populate the data file: ORput the avenge and the standard deviation to an extemal fle called resultatt. Hinu The sqrt function is loemed inthe amath library Use casting where necesswy. 6 Increase your program modularity by implementing functions the main contains only function calls

Explanation / Answer

Please refer below code

#include<iostream>

#include<vector>

#include<fstream>

#include<sstream>

#include<cmath>

using namespace std;

float calculate_mean(vector<int> grades)

{

float sum = 0;

for(unsigned int i = 0; i < grades.size(); i++)

{

sum += grades[i];

}

return sum/grades.size();

}

float calculate_sub_mean_square(vector<float> sub_mean_square)

{

float sum = 0;

for(unsigned int i = 0; i < sub_mean_square.size(); i++)

{

sum += sub_mean_square[i];

}

return sum/sub_mean_square.size();

}

vector<float> calculate_square_value(vector<int> grades,float mean)

{

vector<float> arr ;

for(unsigned int i = 0; i < grades.size(); i++)

{

arr.push_back((grades[i] - mean) * (grades[i] - mean));

}

return arr;

}

int main()

{

std::ifstream myfile("grades.txt"); //input file stream

std::ofstream outfile("results.txt"); //output file stream

std::vector<int> grades;

string line;

if(!myfile.is_open())
{
cout<<"Unable to open file"<<endl;
return -1;
}

//reading file line by line
while(std::getline(myfile,line))

{

std::stringstream linestream(line); //converted line into stream of words

int value;
while(linestream >> value) //saving values of each number into value

{

grades.push_back(value); //pushing value into vector

if(linestream.peek() == ',') //ignoring comma

linestream.ignore();

}
}

//step 1

float mean = calculate_mean(grades); //calculating mean given in problem

//step 2

vector<float> sub_mean_square = calculate_square_value(grades,mean); //calculating squared diff between mean and each grade

//step 3
float mean_squared = calculate_sub_mean_square(sub_mean_square); //calculate mean of squared diff

//step 4

float SD = sqrt(mean_squared); //calculating standard deviation

//writing average and SD into file
if(outfile.is_open())

{

outfile << "Average : "<<mean<<endl;

outfile << "Standard Deviation : "<<SD<<endl;

outfile.close(); //closing output file

}
cout<<"Please check results.txt file for answers"<<endl;

myfile.close(); //closing input file

return 0;

}

input file grades.txt

9,2,5,4,12,7,8,11,9,3,7,4,12,5,4,10,9,6,9,4

output file results.txt

Average : 7
Standard Deviation : 2.98329

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote