C++ Code Example: average of an array

This code example calculates the average of all values in an array. First, the user is asked to enter any number of numbers. These are stored in an array. Then the average of the array is calculated and output.

#include <iostream>
using namespace std;

int main () {
    int quantity, i;
    float numbers[20], result;

    cout << "How many numbers do you want to store in array? (1 - 20) " << endl;
    cin >> quantity;

    // Store numbers in array (user input)
    for(i = 0 ; i < quantity ; i++) {
        cout << "Please enter a number: " << endl;
        cin >> numbers[i];
    }
    
    // Loop over array and sum up the array entries
    for(int i = 0; i < quantity; i++) {
        result += numbers[i];
    }

    result = result / quantity;

    cout << "Average of array is: " << result << endl;

    return 0;
}
Output
How many numbers do you want to store in array? (1 - 20) 
5
Please enter a number: 
54
Please enter a number: 
22
Please enter a number: 
89
Please enter a number: 
33
Please enter a number: 
12
Average of array is: 42