Chapters 5 and 6 Slides

Chapters 5 and 6 Slides - Arrays and Matrices Chapter 5 1...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style Chapter 5 11 Arrays and Matrices
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 5 22 One-Dimensional Arrays Array (Vector) 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] Name of array (Note that all elements of this array have the same name, c) Position number of the element c[6] - 45 6 0 7 2 154 3 - 89 0 6 2 - 3 1 645 3 7 8 c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4]
Background image of page 2
Chapter 5 33 One-Dimensional Arrays cont’d An array is an indexed data structure All variables stored in an array are of the same data type An element of an array is accessed using the array name and an index or subscript The name of the array is the address of the first element and the subscript is the offset In C, the subscripts always start with 0 and increment by 1
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 5 44 Definition and Initialization An array is defined using a declaration statement arrayType  arrayName[size];
Background image of page 4
Chapter 5 55 Definition and Initialization cont’d When defining arrays, specify name type of array number of elements arrayType arrayName[ numberOfElements ]; Examples : int c[ 10 ];   float myArray[ 3284 ]; Defining multiple arrays of same type format similar to regular variables Example : int b[ 100 ], x[ 27 ];
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 5 66 Example int list[10]; allocates memory for 10 integer variables subscript of first element is 0 subscript of last element is 9 C does not perform any bounds checking on arrays list[0] list[1 ] list[9 ]
Background image of page 6
Chapter 5 77 Initializing Arrays Arrays can be initialized at the time they are declared. Examples: double taxrate[3] = {0.15, 0.25, 0.3};     char list[5] = {‘h’ ,’e’ ,’l’ ,’o’};      double vector[100] = {0.0};                            /* assigns zero to all 100 elements */     int s[] = {5,0,-5}  /* the size of array s is 3 */ Not yet implemented in Miracle C
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 5 88 Initializing Arrays cont’d If there are too many initializers a syntax error is produced If size is omitted, the number of initializers determines the size Example: int n[ ] = { 1, 2, 3, 4, 5 }; // 5 initializers, therefore 5 element array This form of initialization does not work in Miracle C
Background image of page 8
Chapter 5 99 Assigning values to an array for loops are often used to assign values to an array Example: int list[10], i; for(i=0; i<10; i++) { list[i] = i; } declares list[0], list[1], . .., list[9]
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon