MODULE 9 ARRAYS Overview An array is a

  • Slides: 23
Download presentation
MODULE 9 : ARRAYS Overview An array is a variable that can store multiple

MODULE 9 : ARRAYS Overview An array is a variable that can store multiple items of the same data type. Arrays can be declared as one-dimension or multidimension. Array as derived data type and dynamic data type helps achieve an efficient memory usage. An array is a homogeneous sequence of objects allocated in contiguous memory; that is, all elements of an array have the same type and there are no gaps between the objects of the sequence. Mr. Joseph Ecklu (DCS) Slide 1

Session Objective At the end of the session, the student will – Appreciate the

Session Objective At the end of the session, the student will – Appreciate the use of arrays data type in programming – Be able to declare arrays and manipulate them – Know the advantages of arrays over regular variables – Know the limitation of arrays

Reading List • Read Chapter 2 of C++ Programming in easy steps by Mike

Reading List • Read Chapter 2 of C++ Programming in easy steps by Mike Mc. Grath • Chapter 9 of C++ Programming by Malik, D. S. Mr. Joseph Ecklu (DCS) Slide 3

C. ARRAYS - dimensioned Variables • An ARRAY is a variable ( or subscripted

C. ARRAYS - dimensioned Variables • An ARRAY is a variable ( or subscripted variable ) that allows the programmer to design a set of similar data types. • Thus an ARRAY is a collection of similar elements. These elements could be all ints, or all floats, or all chars, etc. Mr. Joseph Ecklu (DCS) Slide 4

ARRAY declaration • Like other variables an array needs to be declared so that

ARRAY declaration • Like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. • Eg int c[10]; • Here, int specifies the type of the variable, just as it does with ordinary variables and c specifies the name of the variable. • The [10] : - the number 10 tells how many elements of the type int will be in our array. • This number (in this case 10) is often called the ‘dimension’ of the array. • The [ ] tells the compiler that we are dealing with an array. Mr. Joseph Ecklu (DCS) Slide 5

Example score[10] To refer to a particular location or element in the array, we

Example score[10] To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array You refer to any one of these elements by giving the array name followed by the particular element’s position number in square brackets ([]). The position number is more formally called a subscript or index (this number specifies the number of elements from the beginning of the array). The first element has subscript 0 (zero) and is sometimes called the zeroth element. Mr. Joseph Ecklu (DCS) Slide 6

ACCESSING ELEMENTS OF AN ARRAY • c[0] = -45 ; • c[1] = 6

ACCESSING ELEMENTS OF AN ARRAY • c[0] = -45 ; • c[1] = 6 ; • c[2] = 0 ; • c[3] = 72 ; • c[4] = 1543 ; • c[5] = -89 ; • c[6] = 0 ; • c[7] = 62 ; • c[8] = -3; • c[9] = 1 ; Mr. Joseph Ecklu (DCS) Slide 7

ACCESSING ELEMENTS OF AN ARRAY • Mr. Joseph Ecklu (DCS) The score array Slide

ACCESSING ELEMENTS OF AN ARRAY • Mr. Joseph Ecklu (DCS) The score array Slide 8

ELEMENT c[index] value 1 c[0] -45 2 c[1] 6 3 c[2] 0 4 c[3]

ELEMENT c[index] value 1 c[0] -45 2 c[1] 6 3 c[2] 0 4 c[3] 72 …… …. c[9] 1 10 Thus, c[2] is the third element of the array and the value is 0 Mr. Joseph Ecklu (DCS) Slide 9

OPTIONAL ARRAY INITIALIZATION Optionally an array can be initialized when it is declared by

OPTIONAL ARRAY INITIALIZATION Optionally an array can be initialized when it is declared by assigning values to each element as a commaseparated list enclosed by curly brackets (braces). For example: int nums[6] = { 0, 1, 2, 3, 4, 5 } ; Mr. Joseph Ecklu (DCS) Slide 10

SOME COMMON ARRAY OPERATIOS 1. To initialize all the elements of an array to

SOME COMMON ARRAY OPERATIOS 1. To initialize all the elements of an array to zero int score[10] ; for(int i = 0 ; i < 10 ; i++ ) { score[i] = 0 ; } Mr. Joseph Ecklu (DCS) Slide 11

SOME COMMON ARRAY OPERATIONS To read elements from the keyboard into an array ;

SOME COMMON ARRAY OPERATIONS To read elements from the keyboard into an array ; int score[10] ; for(int i = 0 ; i < 10 ; i++) { cout <<”Please enter element number ” << i << “ ” } cin >> score[i] ; Mr. Joseph Ecklu (DCS) Slide 12

SOME COMMON ARRAY OPERATIONS • Print elements of an array int scores[5] = {45

SOME COMMON ARRAY OPERATIONS • Print elements of an array int scores[5] = {45 , 90 , 44 , 2 , 54} ; for(int i = 0 ; i < 5 ; i++) { cout << scores[i] << “ “ ; } Mr. Joseph Ecklu (DCS) Slide 13

SOME COMMON ARRAY OPERATIONS To find the sum of elements in an array int

SOME COMMON ARRAY OPERATIONS To find the sum of elements in an array int scores[5] = {90 , 78 , 33 , 56} ; int sum = 0 ; // store sum for(int i = 0 ; i < 5 ; i++) { sum = sum + scores[i] // or sum+=scores[i] } cout << “sum = ” << sum << endl ; // print sum Mr. Joseph Ecklu (DCS) Slide 14

SOME COMMON ARRAY OPERATIONS To find the largest element in an array int scores[5]

SOME COMMON ARRAY OPERATIONS To find the largest element in an array int scores[5] = {90 , 78 , 33 , 56} ; int largest = scores[0] ; // assume largest is the first for(int i = 0 ; i < 5 ; i++) { if(scores[i] > largest) { largest = scores[i] ; } } cout << “largest = ” << largest << endl ; // print largest Mr. Joseph Ecklu (DCS) Slide 15

Multidimensional Arrays MULTIDIMENSIONAL ARRAYS The C++ built-in array can be used as a multidimensional

Multidimensional Arrays MULTIDIMENSIONAL ARRAYS The C++ built-in array can be used as a multidimensional array. We simply treat a multidimensional array as an array of arrays, that is, an array with arrays as elements. Example int s[5][6] ; // 2 – dimensional array int m[4][6][7] ; // 3 - dimensional array int ac[4][4][4][5] // 4 – dimensional array In theory there is no limit on dimension that an array can have. Mr. Joseph Ecklu (DCS) Slide 16

Multidimensional Arrays In Practice, multi-dimensional arrays of three indices and more are uncommon, but

Multidimensional Arrays In Practice, multi-dimensional arrays of three indices and more are uncommon, but two-dimensional arrays are useful to store grid-based information, such as coordinates. For example: int elements[2] [3] = { { 1, 2, 3 } , { 4, 5, 6 } }; The two dimensional array works like a table Where the first dimension represent the number of rows (In the case of elements as show above it 2) and the second dimension represent the number of columns ( 3 for elements). Mr. Joseph Ecklu (DCS) Slide 17

Multidimensional Arrays The name of the array is elements [1][2] Mr. Joseph Ecklu (DCS)

Multidimensional Arrays The name of the array is elements [1][2] Mr. Joseph Ecklu (DCS) Slide 18

Multidimensional Arrays Some common Operations on two dimensional Arrays To initialize every element to

Multidimensional Arrays Some common Operations on two dimensional Arrays To initialize every element to zero float z[3][4] ; for(int row = 0 ; row < 3 ; row++) { for(int col = 0 ; col < 4 ; col++) { z[row][col] = 0. 0 ; } } Mr. Joseph Ecklu (DCS) Slide 19

Multidimensional Arrays To read element from the keyboard into a 2 dimensional array float

Multidimensional Arrays To read element from the keyboard into a 2 dimensional array float z[3][4] ; for(int row = 0 ; row < 3 ; row++) { for(int col = 0 ; col < 4 ; col++) { cout << “Enter element for row ” << row << “ column ” << col << “ “ ; cin >> z[row][col] ; } } Mr. Joseph Ecklu (DCS) Slide 20

Multidimensional Arrays To Print the elements of a 2 dimensional array int z[2] [3]

Multidimensional Arrays To Print the elements of a 2 dimensional array int z[2] [3] = { { 1, 2, 3 } , { 4, 5, 6 } }; for(int row = 0 ; row < 3 ; row++) { for(int col = 0 ; col < 4 ; col++) { cout << z[row][col] << “ “ ; } cout << endl ; } Mr. Joseph Ecklu (DCS) Slide 21

Multidimensional Arrays Sum the elements in a 2 dimensional array int z[2] [3] =

Multidimensional Arrays Sum the elements in a 2 dimensional array int z[2] [3] = { { 1, 2, 3 } , { 4, 5, 6 }} ; int sum = 0 ; for(int row = 0 ; row < 3 ; row++) { for(int col = 0 ; col < 4 ; col++) { sum += z[row][col] ; } } Mr. Joseph Ecklu (DCS) Slide 22

Session Summary • An array is a variable that can store multiple items of

Session Summary • An array is a variable that can store multiple items of the same data type. • This session has covered the two types of array which is one-dimensional and multidimensional array and how to declare them.