((C++)) The purpose of this assignment is to jog your memory in working with arr
ID: 3661790 • Letter: #
Question
((C++))
The purpose of this assignment is to jog your memory in working with arrays and functions and to give you practice working with the .NET C++ compiler. Be sure to implement a clean, welldesigned, and commented C++ program solution. Every program that you write must have a comment-heading at the beginning that identifies you and gives a brief synopsis of the purpose of the program. Each function must have a comment in front of its definition that states the purpose of the function, a brief description of the input parameters required by the function, and a clear description of what values the function returns, if any.
If x’ denotes the mean (average) of a sequence of numbers x0, x1, …, xn-1 the variance is the average of the squares of the deviations of the numbers from the mean. In other words,
variance = ( (x0 – x’)2 + (x1 – x’)2 + … + (xn-1 – x’)2 ) / n
The standard deviation is the square root of the variance.
Write a program to input an array of 10 real numbers. The program will output the mean and the standard deviation of the numbers. Input the array values and display the mean and standard deviation in the main( ) function. The main( ) function must call a function to calculate and return the mean and another function to calculate and return the standard deviation.
Use the following numbers for a test case: 2.54, 7.0, 5.367, 1.2, 3.85, 2.453, 4.5, 6.34, 3.738, 5.23
Be sure to test your program and use a calculator to check your answers.
Guidelines: • All of this program input and output should occur in main().
• Your program should trivial to modify for a different size array and data table.
• Design functions to have parameters for all necessary data values. For example: for this program, each function should have 2 parameters (an array parameter and another parameter specifying the number of elements in the array).
• Well-designed functions try to minimize the assumptions necessary in the main( ) program to call them properly. In software systems, this is called "coupling" between modules or functions, and a design goal is to "minimize coupling requirements". For example, it should be possible for a main( ) program to call the standard deviation function without having to first call the mean function. The standard deviation function should be designed to call the mean function itself to determine the x’ value.
• Well-designed programs generally avoid using global variables.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<cmath.h>
// below function calculates Mean
double mean(double a[], int n)
{
int i;
double s=0, mm;
for(i=0;i<n;i++)
{
s=s+a[i];
}
mm=s/n;
return mm;
}
// below function calculates Standard Deviation
double stddev(double a[], int n)
{
int i;
double sum=0, sd;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
return sqrt(((pow(sum,2.0)) - ((1.0/n) * (pow(sum,2.0)))) / (n-1.0));
}
void main()
{
double a={2.54, 7.0, 5.367, 1.2, 3.85, 2.453, 4.5, 6.34, 3.738, 5.23}
double m = mean(a, 10);
cout<<"Mean ="<<m<<endl;
double sd=stddev(a,10);
cout<<"Standard Deviation = "<<sd;
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.