Define a function that takes a partially filled array of numbers as it arguments
ID: 3684742 • Letter: D
Question
Define a function that takes a partially filled array of numbers as it arguments and returns the standard deviation of the numbers in the partially filled array.Since a partially filled array requires two arguments ,the function will actually have two formal parameters:An array parameter and a formal parameter of type int that gives the number of array positions used,The numbers in the array will be of type double.Embed your function in a suitable test program.
Read the following data into your program from a text file that you make. (Use the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, and 189.)
Explanation / Answer
#include <fstream>
#include <iostream>
#include <cmath>
using namespace std;
double get_standard_dev( double Num [], int x );
const int size= 100;
int main ()
{
int next;
int count = 0;
double Num[size];
for(int i =0; i < size; i++)
{
Num[i]=0;
}
ifstream in_stream;
cout << "Here we are finding the standard deviation for the file xxxx.dat" << endl;
in_stream.open ( "xxxx.dat" );
if ( in_stream.fail( ) )
{
cout << "Ufff....!Please check the file is saved properly. It could not open." << endl;
return 0;
}
while(in_stream >> next)
{
Num[count]=next;
count++;
}
get_standard_dev ( Num , count );
in_stream.close();
return 0;
}
double get_standard_dev( double Num [], int x )
{
double average, s;
double sum = 0;
int i;
for( i = 0; i < x;i++ )
{
sum = sum + Num[i];
}
average = sum / x;
sum = 0;
for( i = 0; i < x; i++ )
{
s =pow((Num[i]-average),2);
//cout << s<<" ";
sum = sum + s;
}
cout <<(sqrt (sum / x));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.