Sie sind auf Seite 1von 7

3.

ARRAYS
3.1 INTRODUCTION It is possible to use variable identifiers which are related visually, e.g. score1, score2, ... score100. However, the code to find the highest score out of even five variables that are named in this fashion is both repetitive and long. high_score = score1; if ( score2 > high_score ) then high_score = score2; if ( score3 > high_score ) then high_score = score3; if ( score4 > high_score ) then high_score = score4; if ( score5 > high_score ) then high_score = score5 It would become absolutely unbearable to write and read code, which would repeatedly access these variables for various purposes, such as finding the lowest, the average, the median, the mode, or reading the values from the keyboard, or printing them out on the screen. Often in programming we need to deal with large amounts of data. We could declare individual variables to hold each of these, coming up with some clever naming scheme like: Var1, Var2,..Var100. But that is tedious to type, and has some built in limitation. High-level computer languages, such as Pascal and C, solve this problem by using a data type called an array. This data type allows you to use a single identifier for a whole set of values of the same type, e.g., all integers or all chars. An array is a collection of elements of the same type that are reference by a common name. Each element of an array can be referred to by an array name and a subscript or index. Each element can be integer, float, character or any other valid data type. However, an array cannot contain element of different type of data. Arrays are used to store collections of the same type of elements. An array is a collection of memory locations. Each location holds the same type of data. An array is a group of elements or components, each having the same type referenced by an index or subscript. The whole group of elements is given a single name known as the array name. An array used with an iterative construct makes for a particularly efficient program because this combination allows you to store a number of entities of the same type and process each one using the same rules.

MT 512: Programming Design

Page no: 31

3.2. TYPES OF ARRAYS


One -Dimensional Arrays Consider an array of integers as declared (in c++) below. int score [100]

scores is the identifier for this array which can hold up to 100 integer values. Individual elements are accessed using the identifier and a subscript, which must be one of a range of integer. Here the integer subscripts range from 1 to 100. This array can be diagrammed as follows:

Declaring an Array An array is declared by writing the type of the array, followed by the array name, followed by the number of elements in the array. For e.g. int score[5]; The compiler sets aside enough memory locations to hold 5 integers. The array score will have the following elements (subscripted variables): Score[1], Score[2], Score[3], Score[4], Score[5] The number inside the brackets such as 1, 2, 3, , 5 are referred to as subscripts which indicate the position of an element. Subscripts can be expressions as well as constants. Thus score[i+j] refers to the element of score designated by the value of the integer expression i+ j. The array score below has five elements which are numbered 1 through 5. The elements which are themselves data objects, are named score[1] through score[5].

MT 512: Programming Design

Page no: 32

Array Score Score[1] Score[2] Score[3] Score[4] Score[5] Initialising an Array An array can be initialised when it is declared. To initialise an array, after the array name, put an equal sign and a list of comma-separated data values within curly braces. For e.g. int arrayname[5] = {10,15,20,25,30}; Writing To an Array To write a value to the second position in our array we would write: arrayname[2] = 15; What happens if I have a 5 element array called exap and we write to the 7 th position of our 5 element array. int exap[5]; exap[7] = 100; It would be good if the compiler checked this and gave us an error. In most cases the compiler will give a subscript range error, meaning the referenced element is outside the declared range. Array Elements An array element is one of the scalar data items that make up an array. A subscript list (appended to the array or array component) determines which element is being referred to. A reference to an array element takes the following form: arrayname(subscript-list) arrayname : Is the name of the array. subscript-list: Is a list of one or more subscripts separated by commas. The number of subscripts must equal the rank of the array (size of the array). MT 512: Programming Design Page no: 33 75 25 100 45 60

Each subscript must be a scalar integer (or other numeric) expression with a value that is within the bounds of its dimension. Two-Dimension Arrays In this section we will introduce a data structure which allows us to group together all such information into a single structure - a two dimensional array. For a database application, we can think of this 2D organisation as an array of arrays. As another example of where such a structure is convenient, consider an array of names. We have seen that we can store a name in a string, which is an array of characters. Then an array of strings is also an array of arrays, or a two dimensional array. In this section we will discuss how we can declare two-dimensional arrays, and use them in applications. We will see how we can access the data in such a structure using indices and pointers, and see how this concept can be extended to multi-dimensional arrays. We will present examples of 2 dimensional arrays for data base applications, string sorting and searching, and solutions to systems of simultaneous linear algebraic equations, useful in scientific, engineering, and other applications, e.g. electronic circuit analysis, economic analysis, structural analysis, etc. The one restriction in the use of this data type is that all of the data stored in the structure must be of the same type. It is convenient to think of such data as if it were arranged in five rows each of four columns. 1 2 1 -8 4 5 0 0 9 1 -6 4 2 3 -3 9 -2 -6 1 5

In terminology previously used for several examples, the array consists of four column vectors, each of length 5. This closely parallels the mathematical construction known as a matrix. Here are some samples:

A two-dimensional matrix is a collection of data arranged in a rectangular fashion. An individual element is referenced by its row and its column as Ai,j and Bi,j . Both rows and

MT 512: Programming Design

Page no: 34

columns are numbered starting from one If the maximum indices for the row and column are m and n, it is called an m by n matrix (written .) NOTE: This text will follow normal mathematical conventions and capitalise the first letter of a matrix identifier. This is not a rule; but a question of taste. The identifier of a two-dimensional array in program will be capitalised only if it represents a matrix, and not otherwise. By convention, the row index is always given first. In the above examples, A is a 4 3 matrix, and B is a 2 2 matrix. Likewise, A3, 2 is the number 9, and A2,3 is the number -5. Sample declarations for A and B are given below. Some programming languages declare two-dimensional array as follows: Int: Matrix[14], [13] Int: Matrix[4,3] When referring to the individual two-dimensional array elements in program, both index references may be placed inside a single pair of brackets. Thus, one might write:

A [1, 3]= 5 B [2, 2]= A [3, 1]


rather than

A [1] [3] = 5 B [2] [2] = A [3] [1]


reflecting the fact that the data being abstracted is a single two-dimensional array with a unified indexing scheme, rather than an array of arrays, as the one with which this discussion began. One or the other may be a better description of the actual structure of the data being abstracted, though the former is more compact and is easier to write. However, it makes no difference to the compiler or to the eventual program which of the two one uses, for the interpretation of both declarations produces exactly the same code. Having constructed such a data type and assuming rowCount and colCount to be of type integer and to have values in the correct range, loops can be constructed with references to individual elements in the form:

int = B [rowCount, colCount] or int = A [rowCount, colCount]

MT 512: Programming Design

Page no: 35

Also, notice that the assignment

A [1, 3] = B [2, 1]
is perfectly legal, because both individual elements are of the same type, whereas the assignment B = A; is illegal because the arrays themselves are not of the same type, and the assignment

B [1, 3] = A [1, 3]
is also illegal because the indexing of B in this statement exceeds the correct bounds for the type. The first of these two errors is detected by the compiler, whereas the latter is normally found at run time when array index values are checked against the maximum and minimum allowable bounds. If it were done literally this way with 1 and 3, the compiler would catch the error. Normally, variables are used, however, and their values are not known until run time. Array Element Order The elements of an array form a sequence known as array element order. The position of an element in this sequence is its subscript order value. The elements of an array are stored as a linear sequence of values. A one-dimensional array is stored with its first element in the first storage location and its last element in the last storage location of the sequence. A multidimensional array is stored so that the leftmost subscripts vary most rapidly. This is called the order of subscript progression. Figure 3-1 shows array storage in one-dimensional, and two-dimensional arrays

For example, in two-dimensional array BAN, element BAN(1,2) has a subscript order value of 4.

MT 512: Programming Design

Page no: 36

Exercise 1. Write a pseudocode program, which declares and inputs a one-dimensional array called ABC with 10 elements, and then find the largest element and the smallest element in the array. 2. The sum S of two m n two matrices A and B are given by S i , j = Ai , j + Bi , j Write a Pseudocode program to read these two matrices, evaluate the sum and finally display the matrix S 3. You are given the following segment of a program. K=3 M=2 J=4 REPEAT IF(J <= 13) THEN K=K+2*J END IF J=J+M UNTIL (J>=15) K=3*K a. What will be the value of J at the completion of the program segment? b. What will be the value of K at the completion of the program segment? 4. You are given the following segment of a program.

int M[5] = {10,15,20,25,30} int j; j=1; WHILE (J<5) M[j+1]=M[j] j=j+1 END WHILE
What will be the content of the array M at the completion of the program segment?

MT 512: Programming Design

Page no: 37

Das könnte Ihnen auch gefallen