Posting the same question within 20 minutes of each other will not yield faster replies. It is a waste of other people's time. Don't do it.
1 2 3 4 5
|
while(fin >> next)
{
cout<<setw(6)<<next<<endl;
}
|
This will read everything in inputData.dat, so when you do
1 2 3 4 5 6 7 8 9 10 11 12
|
while (fin >> next )
{
fin>>next;
sum = sum + next;
count ++;
avg = sum / count ;
variance = pow((avg - next),2);
stdDev = sqrt(variance);
cout<<" The Average of the numbers is "<<avg<<endl;
cout<<" whilst their standard deviation is "<<stdDev<<endl;
}
|
there will be nothing to read, as you've already read everything.
Your logic is also incorrect here.
First, you want to read all the numbers in the file to a vector.
Then you loop through all the values in that vector to calculate the average.
Then you loop through the vector again, calculate the squared differences from the average, of each number. This is your variance.
Your standard deviation calculation is correct, however.
Reading and storing the numbers in a vector.
1 2 3 4 5
|
vector<double> numbers; // to store all the numbers in inputData.dat
while(fin >> next)
{
numbers.push_back(next); // insert next into the vector
}
|
Looping through all the numbers in the vector.
numbers.size() returns the number of elements (what your count variable is used for, but now it's unnecessary) in numbers.
1 2 3 4 5
|
for(int i = 0; i < numbers.size(); i++)
{
sum += numbers[i];
}
average = sum / numbers.size();
|
A less verbose and clearer way of looping through the vector would be to use a range-based for loop.
1 2 3 4 5
|
// for every d in numbers
for( double d : numbers )
{
sum += d;
}
|
Now you can apply the same method and logic to calculate the variance.