Not yet complete... Sorry.
To this point, we have seen single instances of each of the built-in data types: an int, a double, a pointer. In reality, we are often more interested in collections of objects:
Almost all programming languages allow a list of objects to be stored under a single variable name. In C/C++/Java/C#, we can define such an object, or array which stores N instances of the given type. In this case, the array is said to be of size N. Like other variables, an array has a name. The size of the array is included in the declaration using square brackets after the variable name, e.g.,
int a[5]; // an array of size 5
To access the N entries (in this example, N = 5), as in the declaration, you add the position using square brackets after the variable name. The N entries may be accessed using a[0], a[1], a[2], ..., a[N - 1].
In the above declaration where we declare a to be an array of 5 integers, we access the five integers by using: a[0], a[1], a[2], a[3], and a[4].
Not realizing this is the single biggest cause of problems with arrays. Program 1 demonstrates the use of arrays, both to assign values ot the entries and to access them.
Program 1. Example of an array.
#include <iostream> using namespace std; int main() { int a[5]; // 'a' is declared to be an array of 5 integers a[0] = 95; // the first (index 0) entry is set to 95 a[1] = 14; a[2] = 72; a[3] = 84; a[4] = 36; // the last (index 4) entry is set to 36 for ( int i = 0; i < 5; ++i ) { cout << "The entry at index " << i << " is " << a[i] << endl; } return 0; } |
You can use for loops to store the values as well, as is shown in Program 2.
Program 2. Assigning the entries of an array dynamically.
#include <iostream> using namespace std; int main() { double a[101]; // 'a' is declared to be an array of 101 doubles cout << "Starting to assign array entries..."; // assign 1/0, 1/1, 1/2, 1/3, ..., 1/100 for ( int i = 0; i < 101; ++i ) { a[i] = 1.0 / static_cast<double>( i ); } cout << "and finishing he assignment of array entries." << endl; for ( int i = 0; i < 101; ++i ) { cout << "The entry at index " << i << " is " << a[i] << endl; } return 0; } |
If you declare your array as is shown above, you can initialize it using a sequence of numbers placed in braces, as is shown in Program 10.
Program 10. Initializing the entries of an array.
#include <iostream> using namespace std; int main() { int a[5] = {95, 14, 72, 84, 36}; // an array of 5 integers for ( int i = 0; i < 5; ++i ) { cout << "The entry at index " << i << " is " << a[i] << endl; } return 0; } |
If you try to initialize more entries than are specified by the array, the compiler will give an error. If you initialize fewer entries than are in the array, the compiler will set the balance to zero. This is demonstrated in Program 11 where you can select one of the variations on initialization.
Program 11. Initializing with variaing numbers of initial values.
#include <iostream> using namespace std; int main() { // int a[5] = {95, 14}; // okay, same as {95, 14, 0, 0, 0}; int a[5] = {95, 14, 72, 84, 36}; // an array of 5 integers // int a[5] = {1, 2, 3, 4, 5, 6}; // bad: too many initial values for the array for ( int i = 0; i < 5; ++i ) { cout << "The entry at index " << i << " is " << a[i] << endl; } return 0; } |
The array int a[5] = {1, 23, 45, 67, 89}; will be displayed graphically as shown in Figure 1, choosing whichever orientation is more appropriate. The first box is assumed to contain a[0], then next a[1], and so on.
Figure 1. Graphical reprsenation of an array with 5 entries.
1. In Program 2, what is the output for the first entry of the array, namely, what do you get when you divide 1 by 0?
2. Write a function which approximates the square root of a positive double a supplied by the user by applying 19 iterations of Newton's method. The initial approximation should be x[0] = 1.0; Each subsequent approximation should take the previous result and calculate:
x[i] = x[i - 1] - (x[i - 1]*x[i - 1] - x) / (2.0 * x[i - 1]);
Print the 19th iteration (note that because index 0 holds the initial approximation, the ith approximation is stored in location x[i]).
Recall that, in order to find the square root of a positive real value a, we must find the positive root of the function f(x) = x2 − a.
Note that it is not necessary to keep track of all of the intermediate values, however, there are some circumstances where this may be useful.
To prompt the user for a real number, you can use:
cout << "Enter a positive real: "; cin >> x;