Read Numbers Into Array - Logic Error

Hi Everyone,

I have to write a program that reads a list of integers from a file that are on separate lines into an array and then determines the highest and lowest numbers, the total, and the average.

The numbers are: 47, 89, 65, 36, 12, 25, 17, 8, 62, 10, 87, 62.

The functions for finding the lowest and the average output the correct numbers, but for some reason I get 4741356 for the highest and 4741876 for the total. I can't seem to pin down exactly where my problem is with these two functions.

Here is the code:

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
#include <iostream>
#include <fstream>

using namespace std;

//Function Prototypes
int getLowestNum(int[], int);
int getHighestNum(int[], int);
int getTotal(int[], int);
int getAverage(int[], int);


int main()
{

    const int ARRAY_SIZE = 12;
    int numbers[ARRAY_SIZE];
    int lowest, highest, total, average, count=0;

    //Read the file
    ifstream inputFile;
    inputFile.open("numbers.txt");

    //Loop to read numbers from file into array
    for (count = 0; count < ARRAY_SIZE; count++) {
    inputFile >> numbers[count];
    }

    //find lowest value and display
    lowest = getLowestNum(numbers, ARRAY_SIZE);
    cout << "Lowest value is: " << lowest << endl;

    //find highest value and display
    highest = getHighestNum(numbers, ARRAY_SIZE);
    cout << "Highest value is: " << highest << endl;

    //find sum value and display
    total = getTotal(numbers, ARRAY_SIZE);
    cout << "The total sum is: " << total << endl;

    //find average value and display
    average = getAverage(numbers, ARRAY_SIZE);
    cout << "The average is: " << average << endl;

    //close file
    inputFile.close();

    return 0;

    }

    //Function Definition for getLowestNum
    int getLowestNum(int numbers[], int ARRAY_SIZE)
    {

        int lowest;
        lowest = numbers[12];

        for (int count = 1; count < ARRAY_SIZE; count++)
        {
                if (numbers[count] < lowest)
                lowest = numbers[count];
        }
        return lowest;

    }

    //Function Definition for getHighestNum
    int getHighestNum(int numbers[], int ARRAY_SIZE)
    {

        int highest;
        highest = numbers[12];

        for (int count = 1; count < ARRAY_SIZE; count++)
        {
                if (numbers[count] > highest)
                highest = numbers[count];
        }
        return highest;

    }

    //Function Definition for getTotal
    int getTotal(int numbers[], int ARRAY_SIZE)
    {
        int total = 0;
        total = numbers[12];

        for (int count = 0; count < ARRAY_SIZE; count++)
        {
            total += numbers[count];
        }
        return total;
    }

    //Function Definition of getAverage
    int getAverage(int numbers[], int ARRAY_SIZE)
    {
        int total = 0;
        int average = 0;

        for (int count = 0; count < ARRAY_SIZE; count++)
        {
            total += numbers[count];
        }
        average = total / ARRAY_SIZE;
        return average;
    }
Last edited on
The problem is line 57/73: The index 12 is out of bounds. Use 0 instead.

You can set it directly:

int highest = numbers[0];

Line 88: Either remove that line or (line 90) start with 1

For the function getAverage(...): Better call getTotal(...) instead of repeat the code.
Thank you!
Topic archived. No new replies allowed.