成人论坛

Data types, structures and operators - EdexcelArrays

Programs use data, known as 鈥榲alues鈥. Variables hold values. Each variable in a program must have a data type. Sometimes a programmer needs to store a lot of related data. To do this they use structures such as arrays.

Part of Computer ScienceApplication of computational thinking

Arrays

Sometimes a programmer needs to store a lot of related . For example, a game might record the scores achieved by players.

One way to do this would be to declare a for each score. So for ten scores, the game program would require ten variables:

score1
                    score2
                    score3

And so on, up to score10. While possible, this is not a practical method of recording this data. Suppose the program needed to record 100 scores? 100 variables would be required!

A better method is to use an . An array is a data structure that holds similar, related data. An array is like a collection of boxes, each of which is called an . Each element has a position in the array and can hold a value. The data in an array must all be of the same .

This way, all data is stored under one . For example, an array called 鈥榮core鈥 could contain all of the scores achieved by players. It might look like this:

0123456789
100110858092726698100120
0100
1110
285
380
492
572
666
798
8100
9120

This is a one-dimensional array with a single set of data.

Declaring an array

In most , an array must be declared before it can be used. To declare an array it must be given at least two properties:

  • an identifier
  • a size - the number of elements it will hold

Many languages will also want the data type in the declaration.

For example, using Visual Basic.NET:

Dim score(9) As Integer would declare an array called 'score' with ten elements (zero to nine). Each element would store an .

Assigning values to an array

Values are assigned to an element in an array by referring to the element's position in the array, eg

score(0) = 100 would assign the value 100 to the first element in the array.

Values in elements can be overwritten at any point, simply by assigning another value to that element.

Retrieving values from an array

Values are retrieved from an element in the array by again referring to the element's position, eg in Visual Basic.NET:

Msgbox(score(7)) would display the eighth value held in the array.

Two-dimensional arrays

A two-dimensional array can hold more than one set of data. This type of array is like a table, with data held in rows and columns.

0123456789
0100110858092726698100120
1909910288781006712088105
0
0100
1110
285
380
492
572
666
798
8100
9120
1
090
199
2102
388
478
5100
667
7120
888
9105

This array would hold ten scores for each of the two players. The first player (0) has data stored in the first row. The second player (1) has data stored in the second row.

A two-dimensional array is declared using two values - the number of rows and the number of columns, eg:

Dim score(1,9) As Integer would declare an array with two rows and ten columns.

Data is assigned or retrieved by referring to an element's row and column number, eg:

score(0,1) = 110

Msgbox(score(1,4)) would display the score 78

Using arrays to structure data