Code help - Only returning 0 for sum and average

I'm creating a program with arrays and I can't figure out why my sum and average are only outputting 0. Please let me know if you find any errors!

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
 

#include <iostream>
#include <fstream>

using namespace std;

const int ROWS = 4;
const int COLS = 4; 

float findSumAndAve(int box[][COLS]);
bool findValue(int box[][COLS], int);

int main()
{
    int sum=0;
    double average=0;
    int v;

    int box[ROWS][COLS] = { {11,8,0,-4}, {74,5,13,42}, {29,-7,45,4}, {100,23,-3,61} };

    findSumAndAve(box);

    cout << "The sum of the array is: " << sum << endl;
    cout << "The average of the array is: " << average << endl;

    cout << "Please enter an integer: ";
    cin >> v;

    bool found = findValue(box, v);

    if (found==true)
        cout << "The integer " << v << " has been found in the array" << endl;
    else
        cout << "Integer not found" << endl;

	system("pause");
	return 0;
}


float findSumAndAve(int box[][COLS])
{
    int sum = 0;
    double average = 0;

    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            sum += box[i][j];
        }
    }

    return sum;
    average = sum / (ROWS * COLS);
    return average;
}


bool findValue(int box[][COLS], int v)
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            if (box[i][j] == v)
                return true;
        }
    }
    return false;
}

(1) You can only return one thing from a function.
Once a function returns, it's over. Nothing after the return point is executed

(2) You need to use the return value of the function. Currently, you are doing:
1
2
3
int sum = 0;
findSumAndAve(box);
cout << "The sum of the array is: " << sum << endl;

You are never setting sum to a value other than 0.
You should also think about how you are going about getting information. You never need to get “the sum and average”.

The goal is to produce the average.

But in order to get the average, you first need the sum. Consequently you need two functions: one to get the sum, and another to get the average.

The average function will naturally call the sum function to get one of the pieces of information it needs, but the caller shouldn’t care. All the caller cares is that you are computing the average.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  int student_ages_array[ 500 ];
  int number_of_students = 0;

  // add each student's age here
  student_ages_array[ number_of_students++ ] = 17;
  student_ages_array[ number_of_students++ ] = 22;
  student_ages_array[ number_of_students++ ] = 48;
  student_ages_array[ number_of_students++ ] = 21;
  ...

  std::cout
    << "The mean age of a student is "
    << average( students_ages_array, number_of_students )
    << ".\n";

And so on.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.