Problem with Total() function Depending on Amount of Numbers in File


The following code has been giving me much trouble. It is supposed to be a program which computes different types of statistical data such as standard deviation and mean. However, my total function (which computes the sum of all numbers) has decided to act up a bit. If the file being inputted into the program is of 4 integers or less, it works correctly. If the file being inputted into the program is of 5 integers, the total of all integers is off by 6. If the file being inputted into the program is 6 integers or greater, it seems to return a number off by 2 million. Please tell me if you can spot my mistake. Much appreciated!

In order to have this program run, create a file named randomnumbers.txt with about 3 to 8 integers so the program can analyze the numbers and the problem becomes obvious.





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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  #include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int largest(int numberstorage[], int x)
    {
    int maximum = 0;
    maximum = numberstorage[0];
    for (int b = 1; b <= x ; b++)
    {
        if(numberstorage[b]> maximum)
        {
            maximum = numberstorage[b];
        }
    }
    return maximum;
}

int smallest(int numberstorage[], int x)
    {
    int minimum = 0;
    minimum = numberstorage[0];
    for (int b = 1; b <= x; b++)
    {
        if(numberstorage[b] < minimum)
        {
            minimum = numberstorage[b];
        }
    }
    return minimum;
}

int total(int numberstorage[], int x)
{
    int filesum = 0;
	for (int b = 0; b <= x; b++)
	{
		filesum+=numberstorage[b];

	}

	return filesum;
}

int mean (int numberstorage[], int x)
{
    int mean = 0;
    mean = ((total(numberstorage, x))/x);
    return mean;
}

int variance (int numberstorage[], int x)
{
    int variance = 0;
    int variancetotal = 0;
    for (int b = 0; b < x; b++ )
    {
        variancetotal += pow(((numberstorage[b]) - (mean(numberstorage, x))), 2);
    }
    variance = (variancetotal/ (x-1));
    return variance;
}

int standardDev (int numberstorage[], int x)
{
    int standardDev = 0;
    standardDev = sqrt(variance(numberstorage,x));
    return standardDev;
}


int main()
{

    fstream infile;
    infile.open("randomnumbers1.txt");
    int num, x = 0;

    int numberstorage[x];

    while(infile >> num)
    {
    numberstorage[x] = num;
    cout << num << " ";
    x++;
    }
    char selection;
    cout << endl;
    cout << "Please choose one of the following options:" << endl;
    cout << "A) Count the Total Number of Integers in File (Sample Size)" << endl;
    cout << "B) Retrieve the Minimum & Maximum of Integers in List" << endl;
    cout << "C) Compute the Total Sum of Numbers in List" << endl;
    cout << "D) Compute the Mean of Numbers in List" << endl;
    cout << "E) Compute the Variance of Numbers in List " << endl;
    cout << "F) Compute the Standard Deviation of Numbers in List" << endl;
    cin >> selection;
    cout << endl;
    switch(selection)
    {
    case 'A':
    case 'a': cout << "Total Number of Integers from File: " << x << endl;
        break;

    case 'B':
    case 'b': cout << "Maximum Integer from File:" << largest(numberstorage, x) << endl;
              cout << "Minimum Integer from File:" << smallest(numberstorage, x) << endl;
        break;
    case 'C':
    case 'c':
        cout << "Total Sum of Numbers from File:" << total(numberstorage, x) << endl;
        break;
    case 'D':
    case 'd': cout << "Mean of Numbers from File:" << mean(numberstorage, x)  << endl;
        break;
    case 'E':
    case 'e': cout << "Variance of Numbers from File:" << variance(numberstorage, x) << endl;
        break;
    case 'F':
    case 'f': cout << "Standard Deviation of Numbers from File:" << standardDev(numberstorage, x) << endl;
        break;
    }



    infile.close();
    return 0;
}
How big is the variable length array numberstorage that gets created at line 81?
Registered users can post here. Sign in or register to post.