Number analysis program

How does everything look? I just finished it. I'm worried if I calculated the average correctly.

(I know the named constants for all the numbers may be unnecessary but it's required for my class)


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
//Ashton Dreiling
//Number analysis program exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//function prototypes
void findHighestNumber (int numbers[], int SIZE, int &highest);
void findLowestNumber (int numbers[], int SIZE, int &lowest);
int getTotal(int numbers[], int SIZE);
void getAverage(int numbers[], int SIZE, double &average);

//global constants
const int SIZE=20;
const int ONE_FOR_CAL=1;
const int ZERO_FOR_CAL=0;

int main()
{
    //some variables
    int highest=ZERO_FOR_CAL;
    int lowest=ZERO_FOR_CAL;
    int sum=ZERO_FOR_CAL;
    double  average=ZERO_FOR_CAL;


    //array numbers
    int numbers[SIZE] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};

    //calling functions
    findHighestNumber(numbers, SIZE, highest);
    findLowestNumber(numbers, SIZE, lowest);
    sum=getTotal(numbers, SIZE);
    getAverage(numbers, SIZE, average);

    cout << "The highest number is " << highest << endl;
    cout << "The lowest number is " << lowest << endl;
    cout << "The sum is " << sum << endl;
    cout << "The average is " << average << endl;

    system("Pause");

    return 0;
}//end main


//beginning of functions for calculations
void findHighestNumber(int numbers[], int SIZE, int &highest)
{
    int index;

    highest=numbers[ZERO_FOR_CAL];

    for (index=1; index<=SIZE-ONE_FOR_CAL; ++index)
    {
        if (numbers[index]>highest)
        {
            highest=(numbers[index]);
        }// end if then statement for findHighestNumber

    }//end for loop for findHighestNumber
}//end findHighestNumber

void findLowestNumber(int numbers[], int SIZE, int &lowest)
{
    int index;
    lowest=numbers[ZERO_FOR_CAL];

    for (index=1; index<=SIZE-ONE_FOR_CAL; ++index)
    {
        if (numbers[index]<lowest)
        {
            lowest=(numbers[index]);
        }// end if then statement for findLowestNumber
    }//end for loop for findLowestNumber

}//end findLowestNumber

int getTotal(int numbers[], int SIZE)
{
    int index;

    int total=ZERO_FOR_CAL;

    for (index=0; index < SIZE; index ++)
    {
        total=(total+numbers[index]);
    }// end for loop for getTotal

    return total;
}//end getTotal

void getAverage(int numbers[], int SIZE, double &average)
{
    double total=ZERO_FOR_CAL;
    int index;

    for(index=0; index<=SIZE-ONE_FOR_CAL; index ++)
    {
        total=(total+numbers[index]);
    }// end for loop for getAverage

    average=(total/SIZE);

}// end getAverage
//end of functions for calculations 
Last edited on
Hi,
> I'm worried if I calculated the average correctly.
Are you sure it is a problem?
I'm worried if I calculated the average correctly.

Why don't you take a calculator and check ?
Would doing so mean I did my for loops correctly? When I do it on a calculator it says I did it correctly and the output says I did it correctly.
Hi,
> Would doing so mean I did my for loops correctly? When I do it on a calculator it says I did it correctly and the output says I did it correctly.

¿Your program is correct :]
Oh! Wow. Haha.
Topic archived. No new replies allowed.