Department of Information Technology Computer Programming Problem Solving

Department of Information Technology Computer Programming & Problem Solving www. husamo. wordpress. com Ch 2 Arrays KIC/Computer Programming & Problem Solving 1

Outline Ø Introduction Ø Arrays Ø Declaring Arrays Ø Examples Using Arrays Ø Multiple-Subscripted Arrays 08/01/2022 2 KIC/Computer Programming & Problem Solving

Objectives In this chapter, you will learn: Ø To introduce the array data structure. Ø To understand the use of arrays to store, sort and search lists and tables of values. Ø To understand how to define an array, initialize an array and refer to individual elements of an array. Ø To be able to define and manipulate multiple subscript arrays. 3

4 Introduction Arrays Ø Structures of related data items Ø Static entity – same size throughout program Ø Dynamic data structures 0 1 2 3 4

• Arrays 5 Name of array (Note that all elements of this array have the same name, c) – Group of consecutive memory locations – Same name and type • To refer to an element, specify – Array name – Position number • Format: arrayname[ position number ] – First element at position 0 – n element array named c: c[ 0 ], c[ 1 ]. . . c[ n – 1 ] c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Position number of the element within array c
![Arrays Array elements are like normal variables: c[ 0 ] = 3; printf( "%d", Arrays Array elements are like normal variables: c[ 0 ] = 3; printf( "%d",](http://slidetodoc.com/presentation_image_h2/5f69b47a72a1c29cf081239d3361572d/image-6.jpg)
Arrays Array elements are like normal variables: c[ 0 ] = 3; printf( "%d", c[ 0 ] ); Perform operations in subscript. If x equals 3 c[ 5 - 2 ] == c[ 3 ] == c[x] 6

Defining Arrays • When defining arrays, specify Ø Name Ø Type of array Ø Number of elements array. Type array. Name[ number. Of. Elements ]; Examples: int c[ 10 ]; float my. Array[ 3284 ]; • Defining multiple arrays of same type – Format similar to regular variables Example: int b[ 100 ], x[ 27 ]; 7
![Examples Using Arrays 8 • Initializers int n[ 5 ] = { 1, 2, Examples Using Arrays 8 • Initializers int n[ 5 ] = { 1, 2,](http://slidetodoc.com/presentation_image_h2/5f69b47a72a1c29cf081239d3361572d/image-8.jpg)
Examples Using Arrays 8 • Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; – If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 } • All elements 0 – If too many a syntax error is produced syntax error – C arrays have no bounds checking • If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; – 5 initializers, therefore 5 element array

Examples Using Arrays KIC/Computer Programming & Problem Solving 9

Examples Using Arrays KIC/Computer Programming & Problem Solving 10

Examples Using Arrays 11

Examples Using Arrays 12

Character Arrays – String “first” is really a static array of characters – Character arrays can be initialized using string literals char string 1[] = "first"; • Null character '