Function declaration help

No matter what I do, I keep getting the same error:
finalarrays.cpp:(.text+0x18a): undefined reference to `stats(int*, int)'
collect2: error: ld returned 1 exit status

Can someone help identify what's wrong here?

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
    1 #include <iostream>
  2 using namespace std;
  3
  4 int stats(int array[], int n);
  5
  6 int main()
  7 {
  8   int stats(int [], int);
  9   int array[100], n, i, num, output[2];
 10
 11    cout << "How many elements (maximum = 100): ";
 12    cin >> n;
 13
 14    for(i = 0;i < n; i++)
 15    {
 16     cout << "Enter number " << i + 1 << ": ";
 17     cin >> array[i];
 18    }
 19
 20    for(i = 0; i < n; i++)
 21    {
 22     cout << array[i] << " ";
 23    }
 24
 25    cout <<  endl;
 26
 27    cout << "Position      Value" << endl;
 28    cout << "-------------------" << endl;
 29
 30    for(i = 0; i < n; i++)
 31    {
 32     cout << i + 1 << "             " << array[i] << endl;
 33    }
 34
 35    stats(array, n);
 36
 37
 38 return 0;
 39 }
 40
 41 int stats(int array[], int &n)
 42 {
 43    int min, max, sum, i;
 44    double ave;
 45
 46    for(i = 0; i < n; i++)
 47    {
 48     sum += array[i];
 49    }
 50
 51    ave = double(sum) / n;
 52
 53    cout << "Sum = " <<  sum << endl;
 54    cout << "Average = " << ave << endl;
 55
 56    return 0;
 57 }
I've managed to get rid of the error, but now the sum and average values are all messed up
1
2
3
4
5
6
7
8
9
10
[mbedoya@storm cisc1600r042018]$ ./a.out
How many elements (maximum = 100): 6
Enter number 1: 69
Enter number 2: 420
Enter number 3: 8675
Enter number 4: 30
Enter number 5: 9
Enter number 6: 42
69 420 8675 30 9 42
Position      Value
1             69
2             420
3             8675
4             30
5             9
6             42
Sum = 41791
Average = 6965.17


I have a final in 12 hours and if i don't start understanding arrays i'm gonna fail. Please help
On line 48 the sum variable is uninitialized. A golden rule always initialize your variables, preferably when you have a sensible value to assign to them. Here though you need to set to 0 first.

There is no point in the function returning 0 every time, make it void seen as it does the output.

Good Luck
Topic archived. No new replies allowed.