Getting the sum of an array?

Hi guys, writing a code where the use inputs the 10 elements of the array and I have to write a function that gets the sum of the 10 elements. I can't really tell what I'm doing wrong. Here is my 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

#include <iostream>
using namespace std;

void display (int a[], int s){

        for (int i=0; i < s; i++){
                cout << a [i] << endl;
                a[i]=-1;
        }
}

void input(int a [], int A_SIZE){

        for (int i=0; i < A_SIZE; i++){
                cout << "Enter element " << (i+1) << ": ";
                cin >> a [i];
        }
}

void stats(const int a[], int A_SIZE, int&sum){


        for (int i = 0; i < A_SIZE; i++){
                sum += a[i];
        }

}

main () {

        const int A_SIZE = 10;
        int arr [A_SIZE] = {};
        void display (int[], int);
        void input (int[], int);
        void stats (const int[], int, int&);
        int sum;

        input (arr, A_SIZE); //input loop

        display (arr, A_SIZE); //output of loop

        stats (arr, A_SIZE, sum); //calculates sum of array elements
                cout << "Sum of all ten variable is: " << sum;

}

                                                                                                          
Well the problem here is this function :

1
2
3
4
5
6
7
void display(int a[], int s)
{
	for (int i = 0; i < s; i++){
		cout << a[i] << endl;
		a[i] = -1;      //problem here
	}
}


You set each array element to -1 just before the call to stats (arr, A_SIZE, sum);
That's why it calculates the sum wrong because all elements are actually -1 and not the values inserted by the user.
Topic archived. No new replies allowed.