The following programs demonstrate C++'s array syntax by filling an array with numbers and calculating their average. The array elements can operate as l- and r-values, appearing on the left and right sides of the assignment operator. Each version is slightly more complex than the previous one. The first two versions fill the array with a fixed number of pseudo-random values, while the third fills the array with an indeterminate number of values input from the console. The first program consists of a single function, while the second and third introduce an average function. The function's first implementation isn't very flexible. However, the final program demonstrates a general, flexible implementation that manages an array of any size, potentially less than its capacity.
Array Averaging Program 1
Array Averaging Program 2
The second version of the program adds an average function to demonstrate passing a one-dimensional array as a function argument. The example illustrates two significant aspects of passing arrays as function arguments:
By default, C++ passes most data types to functions by value. Arrays are the exception to this rule - C++ always passes arrays by-pointer. The example shows two versions of the function prototype and the function header. The different versions are equivalent, and programs only need one.
Only the array's name appears in the function call because the name, without the square brackets, is a pointer - the array's address.
Array Averaging Program 3
There is a potential advantage to making the average operation a function: it could be used wherever a program needs that functionality. However, we must generalize the average function before we can realize the advantage.
In Java, an array is an object that consists of data and length variables. So, when a Java program passes an array to a method, its length is part of it. In C++, an array is a simple, fundamental type with no attributes - it only contains the array elements. So, C++ programs must pass two arguments to the average function: the data and the array's length.
Following that modification, the average function will accept a one-dimensional integer array of any length
The function will also work with partially filled arrays. The data must start at location 0 and must be contiguous (i.e., no empty array elements in the data), but the data does not need to extend to the end of the array.
We could further generalize the function by putting it in a separate .cpp file, making it even easier to use wherever the average functionality is needed. C++ does something similar to this with functions like sqrt and pow.
Pseudo-random numbers are not random - they arise from deterministic calculations - but look random (i.e., pass some statistical tests of randomness). Pseudo-random number generators produce a long cycle of numbers (i.e., a sequence of numbers that eventually repeats) using a recurrence relation: Rn=f(Rn-1). Programs typically use a relatively small part of the cycle, increasing its random appearance. Programs start a random number generator at some point in the cycle by specifying a "seed" value: an initial Ri.
Pseudo-random number generators frequently use the computer's clock to get the elapsed time since the epoch. The elapsed time is a monotonically increasing value - it remains unchanged between clock "ticks" but never decreases - making it an effective, low-cost seed value. Some generators can produce various distributions - a value's frequency of occurrence in a given range. The values in a uniform distribution are equally likely.
Please see the optional Random Number Generators video at the top for more detail.