In: Computer Science
/*
The following function receives the class marks for all student in
a course and compute standard deviation. Standard deviation is the
average of the sum of square of difference of a mark and the
average of the marks. For example for marks={25, 16, 22}, then
average=(25+16+22)/3=21. Then standard deviation = ((25-21)^2 +
(16-21)^2+(22-21)^2)/3.
Implement the function bellow as described above. Then write a test
console app to use the function for computing the standard
deviation of a list of marks that user inputs as floats in the
range of 0.f to 100.f. User signals end of the list by entering -1
as final mark. Make sure to verify user input marks are valid and
in the range, before processing them.
*/
float ComputeMarksStandardDeviation(std::array<float>
marks)
{
}
PROGRAM:
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
/**The following function receives the class marks
* for all student in a course and compute standard deviation.
* function for computing the standard deviation of a list of
* marks that user inputs as floats in the range of 0.f to 100.f.*/
float ComputeMarksStandardDeviation(std::vector<float> marks){
float sum = 0;
float MarksStandardDeviation;
// find the size of the vector
int n = marks.size();
// calculate the sum of all marks
for(int i=0; i<n; i++){
sum+=marks[i];
}
/**For example for marks={25, 16, 22}, then average=(25+16+22)/3=21. Then
* standard deviation = ((25-21)^2 + (16-21)^2+(22-21)^2)/3.
* calculate the average of marks*/
float average = sum/n;
// calculate the standard deviation
for(int i=0; i<n; i++){
MarksStandardDeviation += pow(marks[i] - average, 2);
}
// return the standard deviation
return MarksStandardDeviation/n;
}
// main() function
int main()
{
float num;
vector<float> marks;
while(true){
cout<<"Enter marks: ";
cin>>num;
// User signals end of the list by entering -1 as final mark.
if(num==-1) break;
// Make sure to verify user input marks are valid and in the range,
// between 0-100f
while(num<0 || num>100){
cout<<"Out of range"<<endl;
cout<<"Enter marks between 0-100: ";
cin>>num;
}
// push_back into the vector marks
marks.push_back(num);
}
// calling and print the standard Deviation
// Standard deviation is the average of the sum
// of square of difference of a mark and the average of the marks
cout<<"Standard Deviation: "<<ComputeMarksStandardDeviation(marks);
return 0;
}
OUTPUT :
Enter marks : 25
Enter marks : 16
Enter marks : 22
Enter marks : -1
Standard deviation : 14