Q:

Program that calculates sum and average using functions

I am doing a C++ question where I need to generate a random number, then this random number is how many integers the program will prompt the user to enter.
Then I need to use two separate functions: 1 to calculate and return the sum, and another to calculate and return the average. 
Here is my code so far:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int calculateSum(int, int);
int calculateAvg(int, int, int);

int main()
{
    int sum = 0;
    int average = 0;
    int x;
    int y;
    int n = 0;

    srand(time(0));
    y = rand() % 10;

    for (x = 1; x <= y; x++) {
        cout << "Enter integer " << x << ": ";
        cin >> n;
    }
    cout << "Sum: " << calculateSum(n, sum) << endl;
    cout << "Average: " << calculateAvg(average, sum, y) << endl;

    system("pause");
    return 0;
}

int calculateSum(int n, int sum)
{
    sum = sum + n;
    return sum;
}

int calculateAvg(int average, int sum, int y)
{
    average = sum / y;
    return average;
}

The problem with the code is whenever I run it, it only takes the last integer to calculate the sum (for example, if the last integer entered was 5, the sum of all the integers would be 5 which is incorrect because it ignores the previous integers from the user), and the average is always 0, any help to fix these issues would be appreciated.

A:

Your int n only has room for a single integer, so your for-loop just overwrites n with each integer you input. Naturally, the last integer you enter will be the only one saved by the time you leave the for-loop.
You need to call calculateSum() in the for-loop, so that it increments sum every iteration of the loop. So instead of:
for (x = 1; x <= y; x++) {
    cout << "Enter integer " << x << ": ";
    cin >> n;
}
cout << "Sum: " << calculateSum(n, sum) << endl;

You would set it up like this:
for (x = 1; x <= y; x++) {
    cout << "Enter integer " << x << ": ";
    cin >> n;
    sum = calculateSum(n, sum); // increment sum every iteration
}
cout << "Sum: " << sum << endl;

And somewhat unrelated to this particular issue, your calculateSum() and calculateAvg() functions don't actually update the sum and average variables you've defined in main(); they only return the results of those functions.
To pass sum and average in by reference (what you're trying to accomplish here), you need to change your function signatures from:
int calculateSum(int n, int sum);
int calculateAvg(int average, int sum, int y);

To:
void calculateSum(int n, int &sum);
void calculateAvg(int &average, int sum, int y);

(I've also changed the return type from int to void since you don't need to return sum nor average - you'd have already modified them in their respective functions)

