function problems

the program works fine when I comment out my "larger" and "smaller" functions, but when I try to use them it messes up my sum and average function outputs.

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
#include <iostream>
#include <fstream>
#include <iomanip>
//Array Work-Out
using namespace std;
double read(int[], int);
double sum(int [], int);
double average(double, double);
double larger(double, int [], int);
double smaller(double, int [], int);
ifstream infile;
ofstream outfile;
int main()
{
    const int MAX = 100;
    int num[MAX];
    double SM, I, AV, LRG, SML;
    infile.open("C:\\Users\\Jacob\\Desktop\\workoutData.txt");
    outfile.open("workout.txt");
    I = read(num, MAX);
    cout << "\n\nTotal numbers: " << I << endl;
    SM = sum(num, MAX);
    cout << "\nSum: " << SM << endl;
    AV = average(SM, I);
    cout << "\nAverage : " << setprecision(2) << fixed << AV << endl;
    LRG = larger(AV, num, MAX);
    cout << "\nAmount Larger than average: " << LRG << endl;
    SML = smaller(AV, num, MAX);
    cout << "\nAmount Smaller than average: " << SML << endl;
    infile.close();
    outfile.close();
    return 0;
}
double read(int nums[], int len)
{
    int x;
    int i = 0;
    while (infile)
    {
        infile >> x;
        nums[i] = x;
        cout << setw(4) << nums[i] << "  ";
        i++;
        if (i % 10 == 0)
            cout << endl;
        if (infile.eof())
            break;
    }
    return i;
}
double sum(int nums[], int len)
{
    int j = 0;
    int total = 0;
    for (j = 0; j < len; j++)
    {
        total = total + nums[j];
    }
    return total;
}
double average(double sums, double i)
{
    double avg;
    avg = sums / i;
    return avg;
}
double larger(double aver, int number[], int len)
{
    int k;
    int count = 0;
    for (k = 0; k < len; k++)
    {
        if (number[k] > aver)
        {
            count++;
        }
    }
    return count;
}
double smaller(double AVG, int number[], int len)
{
    int l;
    int c = 0;
    for (l = 0; l < len; l++)
    {
        if (number[l] < AVG)
        {
            c++;
        }
    }
    return c;
}
What exactly do you mean by "messes up outputs"?
line 22 is fishy:

 
SM = sum(num, MAX);


You are assuming 'num' contains MAX elements, which it probably doesn't. That probably should be I, not MAX.
Yeah changing it to I worked, Thank you!
Topic archived. No new replies allowed.