How would I find the sum/average of an array of numbers

I have been writing a program that prints an array of random numbers and then its reverse. It then prints the min and max. Now, I am trying to find the sum and average using functions and I have come close, but I need a little help if anyone knows how. Here is what I have so far; you will notice everything was working except my call and definition for each.
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
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

// Function prototypes
void showArray ( int a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
void showReverse ( int a[ ], int size ); // shows the array in reverse using the format "int a [ ] = { 7, 4, 3, 6, 5, ... , 4, 7, 3 } "
int  lowest ( int a[ ], int size ); // finds and returns the lowest value in the array (should be 7)
int  highest ( int a[ ], int size ); // finds and returns the highest value in the array (should be 3)
int  sumArray ( int a[ ], int size ); // calculates and returns the sum of all values in the array
float averageVal ( int a[ ], int size ); // calculates and returns the average of all values in the array
// **************************************************************************************************************************************
int main ()
{
// Array and reverse array
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25;
    int randvalue[SIZE];


    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(; i < SIZE; i++)
    {
    randvalue[i] = rand () % 5 + 3; // random number between 3 to 7
    }
    cout << "Original array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;

    int j = SIZE-1;
    i = 0;

    while( i <= j)
    {
        swap(randvalue[i], randvalue[j]);
        i++;
        j--;
    }
    cout << "Reversed array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;
// ***********************************************
// Calling the function for max and min
    int returnMax = highest(randvalue, 25);
    cout << "Highest value is " << returnMax << endl;
    int returnMin = lowest (randvalue, 25);
    cout << "Lowest value is " << returnMin << endl;
// ***********************************************
// FUNCTION CALL FOR SUM
    int returnSum = sumArray (randvalue, 25);
    cout << "The sum of all array elements is " << returnSum << endl;
// ***********************************************
// Function call for AVERAGE
    float returnAvg = averageVal (randvalue, 25);
    cout << "The average of all array values is " << returnAvg << endl;
// ***********************************************
    return 0;
}

// Function definition for ARRAY
void showArray ( int a[ ], int size )
{
    int sum = 0;
    for(int i = 0; i < size; i++)
        cout << a[i];
}
// Function definition for LOWEST value
int  lowest ( int a[ ], int size )
{
    int min;
    min = a[0];

    for (int i = 0; i < size; i++)
    {
        if( a[i] < min) // if i-th element is less than min
        {
            min = a[i]; // make that our new min
        }
    }
    return min;
}

// Function definition for HIGHEST value
int highest ( int a[ ], int size )
{
    int max;
    max=a[0];

    for (int i = 0; i < size; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    return max;
}
// Function definition for SUM
int  sumArray ( int a[ ], int size )
{
    for (i=0; i< count; i++)
    {
        cout << numArray[i];
        if (i < count - 1)
            return
    }
}
// Function definition for AVERAGE
float averageVal ( int a[ ], int size )
{

}
// **********************************************
Why are you trying to pass the array into your average function?

You've already computed the sum, can't you just use that value to compute the average?

My sum does not even work.
closed account (48T7M4Gy)
My sum does not even work.

No wonder if sumArray is supposed to get a total. :(
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194887/
updated code. Got the sum to work, but I get errors for the average:
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
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

// Function prototypes
void showArray ( int a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
void showReverse ( int a[ ], int size ); // shows the array in reverse using the format "int a [ ] = { 7, 4, 3, 6, 5, ... , 4, 7, 3 } "
int  lowest ( int a[ ], int size ); // finds and returns the lowest value in the array (should be 7)
int  highest ( int a[ ], int size ); // finds and returns the highest value in the array (should be 3)
int  sumArray ( int a[ ], int size ); // calculates and returns the sum of all values in the array
float averageVal ( int a[ ], int size ); // calculates and returns the average of all values in the array
// **************************************************************************************************************************************
int main ()
{
// Array and reverse array
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25;
    int randvalue[SIZE];


    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(; i < SIZE; i++)
    {
    randvalue[i] = rand () % 5 + 3; // random number between 3 to 7
    }
    cout << "Original array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;

    int j = SIZE-1;
    i = 0;

    while( i <= j)
    {
        swap(randvalue[i], randvalue[j]);
        i++;
        j--;
    }
    cout << "Reversed array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;
// ***********************************************
// Calling the function for max and min
    int returnMax = highest(randvalue, 25);
    cout << "Highest value is " << returnMax << endl;
    int returnMin = lowest (randvalue, 25);
    cout << "Lowest value is " << returnMin << endl;
// ***********************************************
// FUNCTION CALL FOR SUM
    int returnSum = sumArray (randvalue, 25);
    cout << "The sum of all array elements is " << returnSum << endl;
// ***********************************************
// Function call for AVERAGE
    float returnAvg = averageVal (randvalue, 25);
    cout << "The average of all array values is " << returnAvg << endl;
// ***********************************************
    return 0;
}

// Function definition for ARRAY
void showArray ( int a[ ], int size )
{
    int sum = 0;
    for(int i = 0; i < size; i++)
        cout << a[i];
}
// Function definition for LOWEST value
int  lowest ( int a[ ], int size )
{
    int min;
    min = a[0];

    for (int i = 0; i < size; i++)
    {
        if( a[i] < min) // if i-th element is less than min
        {
            min = a[i]; // make that our new min
        }
    }
    return min;
}

// Function definition for HIGHEST value
int highest ( int a[ ], int size )
{
    int max;
    max=a[0];

    for (int i = 0; i < size; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    return max;
}
// Function definition for SUM
int  sumArray ( int a[ ], int size )
{
   int sum=0;
   for(int count = 0; count < size; count++){
        sum = sum + a[count];
   }
    return sum;
}

// Function definition for AVERAGE
float averageVal ( int a[ ], int size )
{
    float avg = 0.0;
    for (int i = 0; i < size; ++i)
 {
     sum += a[count];
 }
     avg = ((float)sum)/size;
     return avg;

}
// ********************************************** 
closed account (48T7M4Gy)
1
2
3
4
5
// Function definition for AVERAGE
float averageVal ( int a[ ], int size )
{
    return (float)sumArray(a, size)/size;
}
Last edited on
@Kemort Thanks, that fixed it!
Topic archived. No new replies allowed.