I am trying to run some code that I found which gets a set of numbers from a text file and calculates all the above, but when I do this is my output:
"The largest value in this file is 80.
The smallest value in this file is -9.25596e+061.
The sum of the numbers is -9.19117e+064.
The average of the numbers is -9.19117e+061."
and nothing gets displayed for the standard deviation.
This is how I have the numbers in the file: "50 40 30 25 80 70 65", no commas or anything. I am using Visual Studio Express 2012.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>
usingnamespace std;
int main()
{
ifstream data_stream;
data_stream.open("data.txt");
double data[1000];
int size = 1000;
for(int i = 0; i<1000; i++){
data_stream >> data[i];
}
double max = data[0];
int index1 = 0;
for (int i = 1; i < size; i++)
{
if (data[i] > max)
{
max = data[i];
index1 = i;
}
}
cout << endl << "The largest value in this file is " << max << '.' << endl;
double min = data[0];
int index2 = 0;
for (int i = 1; i < size; i++)
{
if (data[i] < min)
{
min = data[i];
index2 = i;
}
}
cout << "The smallest value in this file is " << min << '.' << endl;
//////To find sum//////////
double sum = 0;
double avg;
for (int i = 0; i<1000 != '\0'; i++)
{
sum = sum + data[i];
avg = (double)sum/1000;
}
cout << "The sum of the numbers is " << sum << '.' << endl;
cout << "The average of the numbers is " << avg << '.' << endl;
} // end main
double StandardDeviation(int data[], int Total, float Mean)
{
double StandardDeviation = 0.0;
int Sum2 = 0;
Total = 1000;
for (int i = 0; i < Total; ++i)
{
Sum2 += pow((data[i] - Mean), 2);
}
StandardDeviation = sqrt(Sum2 / (Total - 1));
return StandardDeviation;
cout << "The standard deviation of the numbers in the file is "
<< StandardDeviation << endl;
}