Anda di halaman 1dari 19

Introduction to Array

Array

Properties of array Index Data type Contiguous One dimensional array Operations using array
Minimum Maximum

Count
Average Summation

What is Array?

A block of memory locations in the internal memory that is assigned to one variable name. Each array memory location is called element. Sometimes called a subscripted variable. Static arrays An array in which the maximum number of elements cannot change during processing Dynamic arrays An array in which the maximum number of elements can change during processing More flexible and use less memory space than static arrays, but.. more time consuming during processing

Array

A series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. Example: To store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. To store 5 different values of the same type, int for example, with a unique identifier. For example, an array to contain 5 integer values of type int called billy could be represented like this:

Array

Each blank panel represents an element of the array, that in this case are integer values of type int. These elements are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length. Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is: type name [elements]; where type is a valid type (like int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies how many of these elements the array has to contain.

Base-Zero vs Base-One Arrays

Base-zero system: A computer system in which array element numbers begin with zero rather than one Base-one system: A computer system in which array element numbers begin with one rather than zero

Base-Zero vs Base-One Arrays


Base 0 A Base 1 A

A[0]
A[1] A[2] A[3] A[4]

0
1 2 3 4

A[1]
A[2] A[3] A[4] A[5]

1
2 3 4 5

A[5]
A[6] A[7] ... A[N]

5
6 7 N

A[6]
A[7] A[8] ... A[N]

6
7 8 N

Base-Zero Array

Example: 100 list of marks of integer type, will be declared as:

int mark1, mark2, mark3, , mark100;

Notes: If we have 100 marks to be stored, you can imagine how long we have to write the declaration part by using normal variable declaration? By using an array, it can be declared as: int mark[100]; This will reserve 100 contiguous/sequential memory locations for storing the integer data type. Graphically can be depicted as follows:

Base-Zero Array (Cont)

Types of array

Three types One-dimensional array Two-dimensional array Multidimensional array

One-dimensional array

The simplest array structure Consists of one column of memory locations. Dimension refers to the array size that is how big the array is. A single dimensional array declaration has the following form: array_element_data_type array_name[array_size];
array_element_data_type:

declares the base type of the array, which is the type of each element in the array. array_name: is any valid C++ identifier name. array_size: defines how many elements the array will

One-dimensional array

Example: To declare an array of 20 characters, named character:

char character[20]

Can be depicted as follows:


The array character can store up to 20 characters with the first character occupying location character[0] and the last character occupying character[19]. Note that the index runs from 0 to 19. An index always starts from 0 and ends with (array size-1).

Eg: One-dimensional array named Age


Array Age Variable Reference Name The index of the first

0
1 2 3 4

32
54 25 36 45

Age[0]
Age[1] Age[2] Age[3] Age[4] Age[4] = 45
Pronounced: Age-sub-4

position in an array is 0.
The number in the parentheses refers to the box number in the array, the element number

5
6 7 8 9

20
28 50 42 20

Age[5]
Age[6] Age[7] Age[8] Age[9] Fifth element of the Age array

Array Initialization

An array may be initialized at the time of its declaration, which means to give initial values to an array. Initialization of an array may takes the following form:

type array_name[size] = {value_list}; int id[7] = {1, 2, 3, 4, 5, 6, 7}; Declares an integer array id and it immediately assigns the values 1, 2, 3, , 7 to id[0], id[1], id[2],, id[6]. float x[5] = {5.6, 5.7, 5.8, 5.9, 6.1}; Assigns the values 5.6 to x[0], 5.7 to x[1], and so on.

For examples:

Array Initialization
char

vowel[6] = {a, e, i, o, u, \0}; Assigns the characters a to vowel[0], e to vowel[1], and so on. The last character in the array vowel is the NULL character (\0). vowel[6] = "aeiou"; When the value assigned to a character array is a string, the compiler automatically supplies the NULL character but we still have to reserve one extra place for the NULL.

char

Array Initialization
char

vowel[6] = {a, e, i, o, u, \0}; Assigns the characters a to vowel[0], e to vowel[1], and so on. The last character in the array vowel is the NULL character (\0). vowel[6] = "aeiou"; When the value assigned to a character array is a string, the compiler automatically supplies the NULL character but we still have to reserve one extra place for the NULL.

char

Exercise 1
1. 2.

3.

4.

5.

6.

7.

How many slots/indexes does this array have? How many characters, counting the null character at the end, does this array hold? The number of an index is also called an index. What is the lowest index? What is the highest index for this array? Is the number for the highest index the same as that for the number of indexes? What is stored in the last index as a character, that is, in an index number 5? What is stored in the last index numerically? All strings should have this null character stored in its last index to signal the end of the string. 0 1 2 3 4 5 6 7 8 9 10 B o r i n g \0

Exercise 2
1.

Draw and name a one-dimensional array that would hold 10 temperatures. Number the elements.

Exercise 3
Given an array called myArray declared with 10 items, or boxes from [0] to [9], which of the following designations would be legal calls? Assume X = 5.
1. 2. 3. 4. 5.

myArray[X] myArray[2*X] myArray[0] myArray[X-6] myArray[4*X]

Anda mungkin juga menyukai