Thursday, May 31, 2012

What is an array and why are they used?

An array is data structure (type of memory layout) that stores a collection of individual values that are of the same data type. Arrays are useful because instead of having to separately store related information in different variables (named memory locations), you can store them—as a collection—in just one variable. It is more efficient for a program to access and process the information in an array, than it is to deal with many separate variables.

All of the items placed into an array are automatically stored in adjacent memory locations. As the program retrieves the value of each item (or "element") of an array, it simply moves from one memory location to the very next—in a sequential manner. It doesn't have to keep jumping around to widely scattered memory locations in order to retrieve each item's value.

Imagine if you had to store—and later retrieve—the names of all of the registered voters in your city. You could create and name hundreds of thousands of distinct variable names to store the information. That would scatter hundreds of thousands of names all over memory. An alternative is to simply create one variable that can will store the same information, but in sequential memory locations.


For example, if you have class with five students, and you want to store their test grades, you will create an array of the integer data type. Since you have five students, you will create a single array. This sets aside five sequential memory locations to hold the five scores. Each score is stored as an "element" in the array. The first score will be store at location (or "index") zero. The second score will be stored at array index equal to one. The third score will be stored at index equals two, and so on.

Let's name the array "Scores."

The student grades are: 70, 75, 80, 85, 90, and 100.

Let's store ( or "assign") the first grade: Scores (0) = 70.

Now, the second grade: Scores (1) = 75.

Assign the third grade: Scores (2) = 80, and so on.

Now the computer code can access the array Scores to get the value of each score.

For instance, to retrieve the first score, you need to tell the program in which memory location the data is stored, and then retrieve the data from that particular memory location. You do this by creating a new, named memory location (called a variable) . Let's name this variable firstScore.

Example: firstScore = Scores (0)


Let's say that you want to find the average score. You instruct the program to retrieve all of the grades, and then divide by five.

First create two more variables) : Sum and Average.

Initialized these new variables Sum = 0, Average = 0.

Now, let's find the average:

Sum = Score (0) + Score (1) + Score (2) + Score (3)
+ Score (4)
Average = Sum / 5


Further below is a shorter way to compute the average score—by using a loop. This is really where the power of arrays come into play.

// The code will automatically increase i by one, each time it finishes added a score to the cumulative sum.
// It will then loop back up, and get the next array element's value, located at array index equal to i + 1.
// It does this until all of the elements in the array have been processed. Then it automatically exits the loop.

For i = 0 to 4
Sum = Sum + Scores (i)
Next i

//Now compute the average:
Average = Sum / 5