SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Functions For All Subtasks
Exercise:
Programming Projects
Question:4 | ISBN:9780321531346 | Edition: 7

Question

Write a function that computes the average and standard deviation of four scores. The standard deviation is defined to be the square root of the average of the four values: (s i − a) 2 where a is average of the four scores s 1 , s 2 , s 3 , and s 4 . The function will have six parameters and will call two other functions. Embed the function in a driver program that allows you to test the function again and again until you tell the program you are finished.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

#include<iostream>
#include<cmath>
using namespace std;
double calcAverage(double s1, double s2,double s3, double s4);
double calcStandardDeviation(double s1, double s2,double s3, double s4,double average,int n);
void main()
{
 	double s1,s2,s3,s4;
	double avg,sdeviation;
	char option;
    do
	{
	 cout<<"Enter s1:";
	 cin>>s1;
	 cout<<"Enter s2:";
	 cin>>s2;
	 cout<<"Enter s3:";
	 cin>>s3;
     cout<<"Enter s4:";
	 cin>>s4;
	 avg=calcAverage(s1,s2,s3,s4);
      sdeviation=calcStandardDeviation(s1,s2,s3,s4,avg,4);
	cout<<"Standard deviation:"<<sdeviation<<endl;
	 cout<<"Do you want to continue then press y:";
     cin>>option;
	}while (option='y');
}
double Average(double s1, double s2,double s3, double s4)
{
	return (s1+s2+s3+s4)/4;
}
double calcStandardDeviation(double s1, double s2,double s3, double s4, double mean,int n)
{
	double sd;
	sd=(pow((s1-mean),2)+pow((s2-mean),2)+
                      pow((s3-mean),2)+pow((s4-mean),2))/n;
	sd=sqrt(sd);
	return sd;
}

Output:

Enter s1:4
Enter s2:8
Enter s3:16
Enter s4:12
Standard deviation:4.47214
Do you want to continue then press y:n

 

0 0

Discussions

Post the discussion to improve the above solution.