Jump to content
  • Advertisement
Sign in to follow this  
Out-Take

C++ help

This topic is 2854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to take inputs from a data.dat file and compute the average and standard deviation but I'm having trouble getting it to calculate correctly.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
ifstream fin;
fin.open("data_test.dat");

if (fin.fail())
{
cout<<"fail to open \n";
exit(1);
}

char num;
double average_num = 0;
double average;
double std;
double holder;


while (!fin.eof())
{

++average_num;
holder += num;
fin.get(num);
}

average = holder / average_num;

fin.close();
fin.clear();
fin.open("data_test.dat");


while (!fin.eof())
{
holder += pow((num + average), 2);
fin.get(num);
}

std = sqrt(average);

fin.close();
fin.clear();
fin.open("data_test.dat");

cout << "Average = " << average << endl;
cout << "Standard Deviation = " << std << endl;

return(0);
}



This is what I have so far. The numbers that I'm testing with are:
3.80
0.80
1.30
1.50
0.10
2.70
1.00

and the output SHOULD be:
Average = 1.6000
Standard deviation = 1.1588

but for some reason its giving me 40.2222 and 6.3421. If you could help me out and tell me where I went wrong I'd greatly appreciate it.

Share this post


Link to post
Share on other sites
Advertisement
You've got multiple problems going on. First off, initialize all your variables before using them. You're adding to holder when it has an indeterminate value. You're also adding num to holder before you actually input any values into num. And it doesn't help that when you calculate the average you divide num by the number of values you've read instead of holder. In addition fin.get(num) reads the character code of the next character in the stream instead of actually interpreting the character values as strings. You probably want to use the >> operator with a double variable instead. Again in the second read loop you're adding values to holder before initializing it with proper data, reading after you add and reading a character code rather than a number. Also, the standard deviation formula uses the square of the difference of the mean and the individual values. Not that it really matters, because you're taking the square root of the average rather than the sum that you've miscalculated.

Share this post


Link to post
Share on other sites
To add on top of what SiCrane has said, what does holder do? You assign values to it but that's it. You just assign values to it. It has no use except accepting those values.

As SiCrane mentioned, you're not initializing your values and you're getting random junk assigned to them, therefore, you get improper answers. You get 40.2 for average and I got 1.37 :D

Share this post


Link to post
Share on other sites
I got a proj4.cc:51: error: invalid use of member (did you forget the `&' ?) reply from my compiler when I changed fin.get(num); to fin.get >> num; I also set holder to 0.

Share this post


Link to post
Share on other sites
boogyman would it be possible for you to post your working code? And for you to also explain it to me? If its not too much to ask for. I'm more of a visual learner.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!