English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2007-08-18 17:17:51 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

Read the first part to get an overview. The rest is optional: a few simple examples.

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

______________________

2007-08-18 17:51:44 · answer #1 · answered by Einstein 5 · 2 0

In programming languages, an array is a way of storing several items (such as integers). These items must have the same type (only integers, only strings, ...) because an array can't store different items. Every item in an array has a number so the programmer can get the item by using that number. This number is called the index. In some programming languages, the first item has index 0, the second item has index 1 and so on. But in some languages, the first item has index 1 (and then 2, 3, ...).

When the programmer creates an array, he/she must give the size of the array. This is the number of items that can be stored in the array. If the programmer wants to store more items then he/she must create a new array. This is because the size of an array can't be changed.

2007-08-18 17:24:29 · answer #2 · answered by iwlitt 2 · 1 0

An array is a data structure used in computer programming -- an array has the property that items stored in it are stored contiguously and can be accessed randomly (by position or index) They are simple, fast and can be used as the basis for more advanced data structures.

2007-08-18 17:23:34 · answer #3 · answered by mdigitale 7 · 1 0

Let's say that you want an array H elements tall and W elements wide, and that you have a pointer P. Assuming you have subscripts x (in 0 to W-1) and y (in 0 to H-1), you can use either P[y*w+x] or P[x*h+y]. The former is most common, and you must be consistent in your usage. Note also that P[n] is the same as *(P+n).

2016-05-17 05:28:36 · answer #4 · answered by soledad 3 · 0 0

Array -
A variable that stores a series of values that can be accessed using a subscript.
Often in your programs, you'll want to store many values that are related in some way. Suppose you manage a bowling league, and you want to keep track of each player's average. One way to do this is to give each player a variable in your program.

2007-08-18 17:25:32 · answer #5 · answered by Anonymous · 1 0

fedest.com, questions and answers