Getting the largest/smallest value, sum, average, standard deviation from a set of numbers

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream> 
using namespace 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;
}
> This is how I have the numbers in the file: "50 40 30 25 80 70 65"

There are only seven values in the file; and you are trying to read 1000 values.
Input fails after seven numbers have been read.

When the number of elements is not known at compile-time, favour std::vector<> over c-style arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

double largest( const std::vector<double>& seq )
{
    if( seq.empty() ) { /* error */ }

    double maxv = seq.front() ;

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( double d : seq ) if( d > maxv ) maxv = d ;

    return maxv ;
}

double average( const std::vector<double>& seq )
{
    if( seq.empty() ) { /* error */ }

    double sum = 0 ;
    for( double d : seq ) sum += d ;

    return sum / seq.size() ;
}

int main()
{

    std::ifstream data_stream( "data.txt" ) ;

    // http://www.mochima.com/tutorials/vectors.html
    std::vector<double> data ;

    double v ;

    // for each number read from the file
    // http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
    while( data_stream >> v ) data.push_back(v) ;

    std::cout << "largest: " << largest(data) << '\n'
              << "average: " << average(data) << '\n' ;

    // etc.
}
Line 18. How many numbers do you attempt to read and what would happen if the file does not have that many?
Topic archived. No new replies allowed.