CIS3355:
Business Data Structures |
Spring, 2005
How are Arrays declared and how is that an advantage over scalar variable declarations?
Must know terms to better understand tutorial:
Simply stated locating individual elements in storage can become very difficult and time consuming when not using an Array data structure. By using an Array data structure it facilitates the process by allowing us to locate the address of any element in storage with the use of a very simple calculation. This calculation requires that we know the base address and the offset. The calculation is as follows:
Element address = base address + offset * (number bytes/element)
To better demonstrate the advantages of using Arrays, I will illustrate what the process for searching for an element under scalar variable declarations and then under Array declaration. First, I must explain what I mean by Array declaration. Arrays can be declared as follows: Array Declaration #1: Automatic Arrays: Local Arrays 1. Defined within the function 2. Local to the function in which declared (i.e., references to the Array from other arrays cannot be made). 3. Exist only during the existence of the function 4. Not initialized #2: External Arrays: Global Arrays 1. Defined outside the function8 2. Known to ALL functions 3. Do NOT expire (as long as the program is running) 4. Initialized when declared (set to 0 (zero) if numeric; null if character) #3: Static Arrays: Static Arrays 1. Like automatic arrays, local to the function which declared them 2. Like external arrays, retain values between calls 3. Like external arrays, initialized at the time of declaration (set to 0 (zero) if numeric; null if character)
Using Scalar declaration: Declaring the square roots of the numbers 1 to 10 would be:
float sqrt1=1.000, sqrt2=1.414, sqrt3=1.732, sqrt4=2.000, sqrt5=2.236, sqrt6=2.449, sqrt7=2.646, sqrt8=2.828, sqrt9=3.000, sqrt10=3.162 And searching for element 2.646 would require the repetitious entries below: If(sqrt1==2.236)then printf(“Stored in sqrt1\n”); Else if(sqrt2==2.236)then printf(“Stored in sqrt2\n”); Else if(sqrt3==2.236)then printf(“Stored in sqrt3\n”); Else if(sqrt4==2.236)then printf(“Stored in sqrt4\n”);
The if statements would continue until you found the location of the element you where looking for. This program only has 10 elements to look in which would not take to long to go through but as we all know, programs can become long,...very long. Looking for a single element of data in particular within millions of bytes could be a little like asking you to jump into the crowd of clowns and randomly begin searching for a clown in the picture below wearing a particular color in a particular pattern with a red nose.
It could be done but why would we do it that way if we could have the clowns line up in an orderly fashion. Similar to Array structuring we could begin by grouping the clowns by those that have red noses, then by color of custom and then by patterns. This would narrow down and speed up our search.
Using Array declaration:
Declaring the square roots of the numbers 1 to 10 would be:
#include <stdio.h> void main (){ float sqrts[10] = {1.0, 1.414, 1.732, 2.0, 2.236, 2.449, 2.646, 2.828, 3.0, 3.162};
int i; // offset indexfor (i = 0; i < 10; i++) // set the loop parametersif (sqrts[i] == 2.646) // address contains 2.646?printf(“Stored in sqrts[%d]\n”, i); // if so, print the variable offset
else if (i == 9) // if we haven’t found it …
printf(““The value does not exist\n”); // display the not found message
The use of an Array data structure is the equivalent of using the Dewey Decimal System to organize books in a library. The books are maintained in a certain section of the library filed by a unique number but can be looked up in the card catalog by several different ways (i.e. author, topic). Similarly, an Array structure allows the elements to be stored in an orderly fashion that allows them to be called upon easily. Q Why is it better to use an array instead of individual variables? A With arrays, you can group like values with a single name. In Listing 8.3, 1,000 values were stored. Creating 1,000 variable names and initializing each to a random number would have taken a tremendous amount of typing. By using an array, you made the task easy Q What happens if I use a subscript on an array that is larger than the number of elements in the array? A If you use a subscript that is out of bounds with the array declaration, the program will probably compile and even run. However, the results of such a mistake can be unpredictable. This can be a difficult error to find once it starts causing problems, so make sure you're careful when initializing and accessing array elements. 1.If an array is declared with 10 elements, what is the subscript of the first element? Answer: 0 2.What is the name of the array below, and how many elements does it have? float expenses[12]; Answer: The array is named expenses, and it contains 12 elements.
3.What what are the 2 components needed to calculate the address of an element?
a. base address and element address b. offset and vector c. base address and offset
Answer: C References: http://java.sun.com/docs/books/tutorial/java/data/arrays.html http://home.kc.rr.com/glangner/arrays/tsld001.htm http://www.daniweb.com/code/search.php?query=array
|