Arrays; Containers for Binary Things.


      Walls and Mirrors Definition: Is a data structure that contains a fixed maximum number of elements of the same data type that are referenced directly by means of an index, or subscript.

       Reading the definition given by that lovely textbook, we only seem a little more confused. What are elements, data types, and indexes? Even if you had skipped a few of the "to be, or not to be" fundamentals of C++ section, that's okay. Stuck on a few definitions? Click on the link to clarify a few things before we go on. Let's assume that our array is one-dimensional before we go on, for simplicity. Elements can be anything you want it to be, whether they be strings, integers, or characters. They are simply bits of information stored in certain parts of memory. Unlike linked lists, the size is determined at compilation time as a constant variable and therefore, cannot be changed. Linked lists can always change - all you have to do is allocate memory for a new variable, thus many different recursive techniques that can be used for arrays to sort and search cannot be applied to linked lists. To put the above in context, let's consider an array for temperature values for the city of Athabasca.

        const int MAX_SIZE = 4;
        int Athabasca[MAX_SIZE];
        int j;
        for(int k; k < MAX_SIZE; ++k)
        {
             cout << "Enter in the temperature." << endl;
             cin >> j;
             Athabasca[k] = j;
        }

       As you can see here so far, the array will be holding elements of type int, as declared earlier. If the array had been declared differently, then elements of type integer would not be found in this particular array.

       Be careful of what type of array you're working with, whether they be one-dimensional or multidimensional; this is where you reference the element by more than one index. Every array's index will start at zero. So, when you declare the size of your array to be 30, you actually have 31 spaces to put elements in.

      When you're using arrays in functions, you must always pass them by reference. You don't need an '&', though - that will just cause your compiler to get nasty again on you. Just by putting the name, and the square brackets means that it is passed by reference - never by value!!

        #include...
        #include...
        
         void getdata (int list[], int &length);     //Note that in the declaration, the array list has the brackets.

        int main()
        {
            ...
            getdata(list, length);     // Note that in the call, there are NO brackets!
            ...
        }

       Errors, boo boos, and anomalies. What happens if you try to assign the temperature of 25 degrees for the 32nd spot in the array? Your compiler gets nasty and returns to you a boundary error - because there is no such spot! It is like trying to put things into a full container. There is a specific spot for each type of bead. When you run out of room, you can't force anything else into another invisible spot, and you end up running into another error. C++ does not check for this type of error, so when you are writing your program, you should be very careful. Also, the most common mistake is forgetting that the array addresses always start at zero, not one. Going back to the Athabasca example that was written earlier at the top of this page, it has 4 spots for things. Before, it prompts for keyboard input, but for the sake of this little scenario, we'll assign them at compilation time.

              Athabasca[0]= 21.
              Athabasca[1]= 5.
              Athabasca[2]= 19.
              Athabasca[3]= -12.
              Athabasca[4]= 10. - note that here, there is no such thing as a 5th value, resulting in a boundary error.

              So far, throughout this entire page, we have assumed that we are working with one-dimensional arrays. Multidimensional arrays are those that require more than one index to reference a certain element. For the class, it is more important that you understand a one-dimensional array, as strings are really just arrays of char. They are very similar to linked lists. Multidimensional arrays are used throughout society and universal problem solving. For example, we can consider a student's marks.

Name English Math
Jason 84 77
Cassandra 91 80
Daniel 56 66

              Even keeping score of student marks can prove to be quite a challenge when it isn't organized in any specific order. When it is placed into a spreadsheet, however, the mountainous task turns into a breeze. Arrays are meant for organizing data so that a sort can be used to find a specific item with respect to a certain search key. For example, the search key we use in the table above might be for the name of the person "Cassandra". The field, or search key that will be traversed will be Name.

              In conclusion, arrays are very nice in terms of understanding and coding. It helps organize data in a fashion so that when a search key is used, the data is easily found by either a binary or a sequential search. The downfall to this is that the size of the array is a constant value, and is determined at compile time. If you know how much space you need for the array, this is a small thing that doesn't matter. If you don't, however, you run into problems such as your array is too small, or that you have unused space which is wasted. This space will be later referred to as memory in the linked list section. When you're inserting, or deleting items, you cannot just put them in any old place, nor can you delete any one without leaving holes in your array. To eliminate this problem, you'll have to shuffle things around, which becomes really awkward. Linked lists are even better, but if this is your first time in C++, I strongly suggest you stick to arrays.

Looking for that computer definition? Check out Foldoc for the definition you seek.

Designed by Sandi Lau
Last altered: April 18, 1999
Footnotes, Bibliography, References: Information Bank

Recursion; Where Bunnies Meet Trekkers
The Sailormoon Doorway | The Stellar CrossRoads | The World of C++