Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

# Module1_arrays.pptx

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Chargement dans…3
×

## Consultez-les par la suite

1 sur 9 Publicité

# Module1_arrays.pptx

Duqjw

Publicité
Publicité

## Plus De Contenu Connexe

Publicité

### Module1_arrays.pptx

1. 1. MODULE 1 ARRAYS
2. 2. Arrays • An array is used to store a collection of data of the same type. • All arrays consist of contiguous memory locations. • The lowest address corresponds to the first element and the highest address to the last element. • For example, an array containing 5 integer values of type int called foo could be represented as:
3. 3. Array Declaration • A typical declaration for an array in C++ is: type name [elements]; • type is a valid type (int, float...) • name is a valid identifier • elements field (which is always enclosed in square brackets[]), specifies the length of the array in terms of the number of elements. • Eg:int foo [5];// the foo array, with five elements of type int.
4. 4. Initializing arrays • The elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. • For example: int foo [5] = { 16, 2, 77, 40, 12071 }; This statement declares an array that can be represented like this: •
5. 5. • The initializer can even have no values, just the braces: int baz [5] = { }; • This creates an array of five int values, each initialized with a value of zero: • • C++ allows the possibility of leaving the square brackets empty []. In this case, the compiler will assume automatically a size for the array that matches the number of values included between the braces {}: eg: int foo [] = { 16, 2, 77, 40, 12071 };
6. 6. Accessing the values of an array • In C++, each element in an array is associated with a number. The number is known as an array index. We can access elements of an array by using those indices. • // syntax to access array elements array[index]; • Consider the array x:
7. 7. • Multidimensional arrays are also known as array of arrays. The data in multidimensional array is stored in a tabular form as shown in the diagram below:
8. 8. • A two dimensional array: • int arr[2][3]; • This array has total 2*3 = 6 elements. • A three dimensional array: • int arr[2][2][2]; • This array has total 2*2*2 = 8 elements. • How to declare a two dimensional array? • int myarray[2][3];
9. 9. • Initialization: We can initialize the array in many ways: Method 1: • int arr[2][3] = {10, 11 ,12 ,20 ,21 , 22}; • Accessing array elements: arr[0][0] – first element arr[0][1] – second element arr[0][2] – third element arr[1][0] – fourth element arr[1][1] – fifth element arr[1][2] – sixth element