Arrays are another structured type, like objects. They are also very different from objects. Arrays are sort of like tables:
|
In the above table, the election years are the index, and the winners are the indexed values. The difference between the above array and arrays that you will make in C++ is that in C++, arrays always start at 0 and don't skip. (i.e., indices go 0, 1, 2, 3, ...) You don't have to use all the entries, if that makes it more convenient for you. Incidentally, strings of characters are very closely related. You'll see those later, but keep them in mind.
To declare an array of
10 ints: typeint bozo[10];
int declares the type
of element stored in the array, bozo is the name of the array, and the
number in the brackets is the number of elements in the array. This
declaration defines bozo[0], bozo[1], bozo[2],...,
bozo[9]
. If instead of 10 ints, you wanted to declare an array
of 30 doubles, you would typedouble ranford[30];
The element type of an array can be any type...even objects...or other
arrays. You'll see tomorrow how to do
that.
The program shortList.cpp demonstrates three very important array concepts:
Consecutively in the memory within each array. There is no guarantee about what preceeds or follows any array. For example, the two lines
int a[4]; int b[4];
Could create something like this:
|
But it might not. You are guaranteed that the array a
will be stored consecutively, and the same goes for the array
b
. But either array may precede the other, and there may
be other data stored between them. This method of putting all
elements in an array consecutively makes accessing any element of an
array easy for the computer, if it knows:
The formula for figuring out where the ith element is then is just base + (i * eltSize). This method works because arrays start at zero. If they didn't, then i would be replaced by (i-1), and that was just too much math for the computer to do, or so decided the designers of the language. And so we have arrays which begin at zero. This leads to the practice that loops start at 0, to make it easy to use for-loops to access array elements.
Neither C nor C++ check whether the element that you're trying to access is actually a member of the array that you're trying to access it in. Try the demo boundsError.cpp with the debugger turned on. Notice that values which shouldn't change do. On a Mac, which does not have memory protection, you can seriously screw up things if you go out of the bounds of your array, then right out of the bounds of your program into the memory of the operating system. It can happen. It's not pretty. Check all array accesses carefully to make sure that they fit in the array.
Remember the recursive method of calculating fibonacci numbers?
int fib(int n) { if (n <= 1) return 1; else return fib(n-1) + fib(n-2); }
This was slow (exponential in n) for pretty much the same reason as the recursive choose method (coming up tomorrow in an improved form), and it can be greatly sped up by using arrays to store the values. Check out fastFib.cpp. It creates an array, adds up the numbers, and is all set to give you any Fibonacci number up to fib(46) almost instantly, because it eliminates all the redundant addition done by the other version.
This is a method of finding all primes from 2 to N-1.
This is actually Short Assignment #11. There is a contest attached to see who can find the largest prime number. The winner gets to choose whether or not there will be a short assignment next Wednesday.
To Index Previous Next