This is the question: Define a function that takse a partially filled
array of numbers as its arguments and returns the standard deviation of
the numbers in the partially fille array. Since a partially filled array
requires two arguments, the function will automatically 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. Embeded your function in a suitable test program.

I am having some problems regarding filling the array and getting the standard deviation. Is there something wrong with my function or how can I declare my array and fill it? This chapter in class is driving me crazy..

``````#include <fstream>
#include <iostream>
#include <cmath>

using namespace std;

double get_standard_dev( double Num [], int x );

const int size= 100;

int main ()
{
int x, size;
double Num[x];
ifstream in_stream;

cout << "We are going to find the standard deviation for the file stddev.dat" << endl;

// Opens the input file.
in_stream.open ( "stddev.dat" );

// If the file fails to open it will close the program and output the statement.
if ( in_stream.fail( ) )
{
cout << "Please check if the file is saved properly. It could not open." << endl;
}

cout << get_standard_dev( Num, size) << endl;

return 0;
}

double get_standard_dev( double Num [], int x )
{
double average, temp, mean;
double sum = 0;
int i;

for( i = 0; i < x;i++ )
{
sum += Num[i];
}

average = sum / x;

for( i = 0; i < x;i++ )

{
temp = Num[i] - average;
sum += temp * temp;
}

mean = sum / x;

return sqrt(mean);
}``````

## Recommended Answers

Garbage in, garbage out. You can't test your standard deviation function until you fill it with actual data, which you are not doing anywhere.

You have a file that you open called "stddev.dat". You open it in line 20 and you check to make sure it opened correctly in …

Without seeing the file layout and the entire new program, it's hard to say, but assuming the file is numbers separated by white space, it looks good. I guess you've replaced "x" with "count". Seems about right. Again, I'd have to see the whole program to see your changes. Does …

You didn't put the code where my comment is (line 31). You close it on line 33, then try to read it on line 36. Won't work. Move lines 36 to 40 to where my comment is (line 31).

## All 11 Replies

Garbage in, garbage out. You can't test your standard deviation function until you fill it with actual data, which you are not doing anywhere.

You have a file that you open called "stddev.dat". You open it in line 20 and you check to make sure it opened correctly in lines 23 to 26. However, you never actually READ from the file (and while you check for error and provide an error message correctly in lines 23 to 26, you don't actually end the program upon encountering the error. Change lines 23 to 26 to this...

``````// If the file fails to open it will close the program and output the statement.
if ( in_stream.fail( ) )
{
cout << "Please check if the file is saved properly. It could not open." << endl;
return 0; // you want to end the program here.  You can't calculate correctly if the file does not open
}

// now read from in_stream into the Num[] array. Fill in x with the number of numbers in the file.

// now close the file
in_stream.close();``````

Beyond that, look at lines 9, 13, and 14. You declare a constant called size on line 9, then again on line 14. Get rid of the declaration of size on line 13.

Line 14

``double Num[x];``

This is no good. x is a VARIABLE. The value inside of the brackets needs to be a CONSTANT. Fortunately you have a constant declared called "size" on line 9. Chang eline 14 to this...

``double Num[size];``

Finally, line 28...

``cout << get_standard_dev( Num, size) << endl;``

Remember this is a PARTIALLY filled array. What is the actual number of numbers in the file? Replace "size" with that VARIABLE (i.e. not a constant -- remember, you are deleting the "size" variable declared on line 14).

So do you suggest I use a while loop in main to count the number of elements that are being used then output the count and the Standard Deviant?

Sorry about the double post, this is the while loop i came up with.

``````while (in_stream >> next)
{
Num[count] = next;
count++;
}

cout << count << endl;``````

Without seeing the file layout and the entire new program, it's hard to say, but assuming the file is numbers separated by white space, it looks good. I guess you've replaced "x" with "count". Seems about right. Again, I'd have to see the whole program to see your changes. Does it work?

It compiles but outputs a 0. Here's what I changed:

``````#include <fstream>
#include <iostream>
#include <cmath>

using namespace std;

double get_standard_dev( double Num [], int x );

const int size= 100;

int main ()
{
int size, next;
int count = 0;
int sum = 0;
double Num[size];
ifstream in_stream;

cout << "We are going to find the standard deviation for the file stddev.dat" << endl;

// Opens the input file.
in_stream.open ( "stddev.dat" );

// If the file fails to open it will close the program and output the statement.
if ( in_stream.fail( ) )
{
cout << "Please check if the file is saved properly. It could not open." << endl;
return 0; // you want to end the program here.  You can't calculate correctly if the file does not open
}

// now read from in_stream into the Num[] array. Fill in x with the number of numbers in the file.

// now close the file
in_stream.close();

while (in_stream >> next)
{
Num[count] = next;
count++;
}

cout << count << endl;

return 0;
}

double get_standard_dev( double Num [], int x )
{
double average, temp, mean;
double sum = 0;
int i;

for( i = 0; i < x;i++ )
{
sum += Num[i];
}

average = sum / x;

for( i = 0; i < x;i++ )

{
temp = Num[i] - average;
sum += temp * temp;
}

mean = sum / x;

return sqrt(mean);
}``````

The file is a .dat that has numbers like so:

1.720
1.981
1.954
1.594
1.242
1.058
1.608
1.668
1.548
1.997

Sorry about the double post, I put the while loop before the close.

You didn't put the code where my comment is (line 31). You close it on line 33, then try to read it on line 36. Won't work. Move lines 36 to 40 to where my comment is (line 31).

And you still have a local variable `int size;` declared near the top of your main(), which masks the global constant `const int size = 1000;` . Get rid of the one inside main(), it's not needed and it's causing problems.

``````#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 << "We are going to find the standard deviation for the file stddev.dat" << endl;

// Opens the input file.
in_stream.open ( "stddev.dat" );

// If the file fails to open it will close the program and output the statement.
if ( in_stream.fail( ) )
{
cout << "Please check if the file is saved properly. It could not open." << endl;
return 0; // you want to end the program here.  You can't calculate correctly if the file does not open
}

while(in_stream >> next)
{
Num[count]=next;
count++;
}

get_standard_dev ( Num , count );

// now read from in_stream into the Num[] array. Fill in x with the number of numbers in the file.

// now close the file
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;
}``````

It compiles but I do not understand where I am messing up. I'm outputting a value of 0 and cannot figure out why

In your main, you are assigning decimal values to an int value. Is that intentional?

line 13:

``int next;``

line 34:

``while(in_stream >> next)``

line 36:

``Num[count]=next;``

That's going to cause all of your data to be 1's. With that in mind, puting that information into the function line 58, sum will be 10, average will be 1 on line 61.

Line 66:

``s =pow((Num[i]-average),2);``

Since all values Num through Num is 1, that function is pow(1-1,2) which equals 0 which in turn sets sum = to 0.

so line 71 is outputting the square root of 0/10 which is 0.

Ok, I figured it out. Thanks for the help. I didn't declare temp as double which made it so it didn't read the decimals in my file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.