Lecture Wk 9 Arrays 1 Uni MAP Sem

  • Slides: 31
Download presentation
Lecture Wk 9 – Arrays (1) Uni. MAP Sem I - 19/20 DKT 121

Lecture Wk 9 – Arrays (1) Uni. MAP Sem I - 19/20 DKT 121 : Computer Programming 1

Outline 1. 2. 3. 4. 5. 6. 7. Introduction Arrays of Data Array Declaration

Outline 1. 2. 3. 4. 5. 6. 7. Introduction Arrays of Data Array Declaration Array Initialization Operations on Array Multidimensional Arrays Index out of bound 2

What is an Array? n n n The variables that we have used so

What is an Array? n n n The variables that we have used so far have all common characteristics: n Each variable could only store a single value at a time. Example: int Count, Length, Num; double Average, Total; char Selection, Choice; An array is a collection of a fixed number of components wherein all of the components are of the same type 3

What is an Array? (Example) n Example: Let us work with a list of

What is an Array? (Example) n Example: Let us work with a list of five integers: n n 5, 10, 15, 20, and 25. Previously we would declare five variables: int Num 1, Num 2, Num 3, Num 4, Num 5; n By using array, since they are all of the same data type, we could just write: int Num[5]; 4

What is an Array? (Example) Num 5 Num[0] 10 Num[1] 15 Num[2] 20 Num[3]

What is an Array? (Example) Num 5 Num[0] 10 Num[1] 15 Num[2] 20 Num[3] 25 Num[4] n 5 components or elements in this array. n Elements are referred to index. n Element Num[2] has index 2 and value 15. 5

Arrays of Data n n Engineering applications usually involve large chunk of data (of

Arrays of Data n n Engineering applications usually involve large chunk of data (of common type) Arrays provide easy and efficient concept for data storage or management Arrays are usually processed through loops (processing is very common) Arrays are accessed by indicating an address or index 6

Arrays in C n n Arrays can assume any type (including the primitive data

Arrays in C n n Arrays can assume any type (including the primitive data types) int, char, string, double, float, etc. Like any other instances, arrays must be declared before use. 7

Array Declaration n n Format: n data_type arrayname[int value]; Example: n int List[5]; n

Array Declaration n n Format: n data_type arrayname[int value]; Example: n int List[5]; n const int Max_List_Size = 10; int Hours[Max_List_Size]; n const int SIZE = 100; double Amount[SIZE]; n const int Max_List_Size = 6; char Alphas[Max_List_Size]; n #define N 10 double B[N]; 8

Multiple Instances vs. Array // multiple instance // array int Value 1, Value 2,

Multiple Instances vs. Array // multiple instance // array int Value 1, Value 2, Value 3; int Value[3]; printf (“Enter first value: “); scanf (“%d”, &Value 1); for(int Count=0; Count<3; Count++) { printf (“Enter value : ”); printf (“%d : ”, Count+1); scanf (“%d”, &Value[Count]); } printf(“Enter second value: “); scanf(“%d”, &Value 2); printf (“Enter third value: “); scanf(“%d”, &Value 3); // process or display 9

Arrays - Memory Allocation n Arrays are allocated bulk memory Single reference used for

Arrays - Memory Allocation n Arrays are allocated bulk memory Single reference used for multiple locations Items are accessed based on index (address) with reference to first item int Value[8]; index Value[0]=23; 0 23 Value[1]=56; 1 56 Value[2]=100; 2 100 Value[3]=0; 3 0 Value[4]=12; 4 12 Value[5]=234; 5 234 Value[6]=666; 6 666 Value[7]=4; 7 4 10

Arrays Arithmetic n n n Operations on arrays are similar to that on basic

Arrays Arithmetic n n n Operations on arrays are similar to that on basic variables. Sum = Num[0] + Num[1] + Num[2] + Num[3]; Multiplication = 3 * Num[1]; Remainder = Num[3] % 3; Total = Num[1] * Num[2]; 11

Array Initialization n n Arrays can be initialized directly, but assignments are done using

Array Initialization n n Arrays can be initialized directly, but assignments are done using loops Like any other simple variable, arrays can also be initialized while they are being declared. double Sales[5] = {12. 25, 32. 50, 16. 90, 23, 45. 68}; Sales[0]=12. 25; Sales[1]=32. 50; Sales[2]=16. 90; Sales[3]=23. 00; Sales[4]=45. 68 12

Array Initialization (cont…) n Initializers: n n ▪ If not enough initializers, rightmost element

Array Initialization (cont…) n Initializers: n n ▪ If not enough initializers, rightmost element becomes 0 int N[ 7 ] = { 1, 2, 3, 4, 5 }; N[5] = N[6] = 0 All elements = 0 int N[ 5 ] = { 0 } ; If size is omitted, initializers determine the size int N[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array 13

Sample Program #include <stdio. h> int main() { Initializes the first 2 elements of

Sample Program #include <stdio. h> int main() { Initializes the first 2 elements of the A[]array. All the other elements are then automatically set to zero int A[3]= {11, 22}, B[]={44, 55, 66}, i. Loop; double X[2], Y[10]; Because no array size is given (the brackets are empty) and three values are given in braces, the array is automatically declared to have a size of 3 with the value shown being the initial element values. printf(“A[0]=%2 d, A[1]=%2 d, A[2]=%2 d n" “B[0]=%2 d, B[1]=%2 d, B[2]=%2 d nn", A[0], A[1], A[2], B[0], B[1], B[2]); printf("Please enter two real numbersn"); scanf("%lf %lf", &X[0], &X[1]); printf(“X[0] = %. 1 lf X[1] = %. 1 lfnn", X[0], X[1]); for (i. Loop=0; i. Loop<10; i. Loop++) { Y[i. Loop]= i. Loop*100. 0; printf(“Y[%1 d]=%. 2 lfn", i. Loop, Y[i]); } return 0; } Using a loop to fill all the elements of the Y[] array. 14

Sample Program n Output: ai. A[0]=11, ai. A[1]=22, ai. A[2]= 0 ai. B[0]=44, ai.

Sample Program n Output: ai. A[0]=11, ai. A[1]=22, ai. A[2]= 0 ai. B[0]=44, ai. B[1]=55, ai. B[2]=66 Please enter two real numbers 77. 0 88. 0 ad. X[0] = 77. 0 ad. X[1] = 88. 0 ad. Y[0]=0. 00 ad. Y[1]=100. 00 ad. Y[2]=200. 00 ad. Y[3]=300. 00 ad. Y[4]=400. 00 ad. Y[5]=500. 00 ad. Y[6]=600. 00 ad. Y[7]=700. 00 ad. Y[8]=800. 00 ad. Y[9]=900. 00 15

Array Initialization During Declaration n n When declaring and initializing arrays, it is not

Array Initialization During Declaration n n When declaring and initializing arrays, it is not necessary to specify the size of the array. The size of the array is determined by the number of initial values in the braces. double ad. Sales[] = {12. 25, 32. 50, 16. 90, 23, 45. 68}; 16

A simple example n The program declares and initializes the array Y. It uses

A simple example n The program declares and initializes the array Y. It uses a ‘for’ loop with index i to access the successive elements of Y. For each loop iteration, the value accessed id is added to the variable Total which is finally displayed. Note that the loop index i starts from 0 to 4 (not from 1 to 5). Also, note that the array size n is declared in the define statement. 17

A simple example (cont. . ) #include<stdio. h> #define n 5 void main() {

A simple example (cont. . ) #include<stdio. h> #define n 5 void main() { int i, Total = 0; int Y[n]={9, 6, 20, 5, 12}; { } } for (i=0; i<n; i++) // define number of n in the array // variable declaration // array declaration and // initialization printf("%d", Y[i]); Total = Total + Y[i]; printf ("n. Total = %dn”, Total); 18

Output; Uni. MAP Sem II - 09/10 DKT 120 : Computer Programming 19

Output; Uni. MAP Sem II - 09/10 DKT 120 : Computer Programming 19

Notes n The defined constants, #define is used to ease any future amendments of

Notes n The defined constants, #define is used to ease any future amendments of the codes, for instance, if the array is to be widen to an n of 10 instead of 5, it would be adequate by modifying the line: #define n 5 #define n 10 there is no need to make any other changes to the program, thus making the life of programmer easier. 20

Operations on Array Reading data in an array for (Index = 0; Index <

Operations on Array Reading data in an array for (Index = 0; Index < 10; Index++) scanf (“%d”, &Sale[Index]); n Printing/display an array for (Index = 0; Index < 10; Index++) printf (“%d ”, Sale[Index]); n 21

Parallel Arrays n Two (or more) arrays are called parallel if their corresponding components

Parallel Arrays n Two (or more) arrays are called parallel if their corresponding components hold related information. int Student. Id[50]; char Student. Grade[50]; 22

Multi-Dimensional Arrays n n Arrays can have multiple dimensions Most used is the 2

Multi-Dimensional Arrays n n Arrays can have multiple dimensions Most used is the 2 -dimensional array (for matrix implementation) Actual implementation is a single array (segmented) Nested loop structure usually used to access items 23

2 -Dimensional Array (Example) int ai. Value[4][2]; ai. Value[2][1]=5; index ai. Value Row 0

2 -Dimensional Array (Example) int ai. Value[4][2]; ai. Value[2][1]=5; index ai. Value Row 0 Column 0 1 0 Row 3 1 2 3 4 1 2 Row 1 0 5 5 5 6 7 Address Resolution = Row*(Max. Col) + Col 24

Multi-Dimensional Arrays (cont. . ) n n A collection of the same type of

Multi-Dimensional Arrays (cont. . ) n n A collection of the same type of data stored in contiguous and increasing memory locations. Declaration of multi-dimensional array: array_type n array_name Array dimension = 2 int B[2][3] = {51, 52, 53, 54, 55, 56}; two rows three columns first row initial values second row initial values 25

Multi-Dimensional Arrays (cont. . ) n n Multi-dimensional array can be initialized directly in

Multi-Dimensional Arrays (cont. . ) n n Multi-dimensional array can be initialized directly in the declaration statement. For example: int B[2][3] = {51, 52, 53, 54, 55, 56}; which initializes the elements to be B[0][0] = 51 B[0][1] = 52 B[0][2] = 53 B[1][0] = 54 B[1][1] = 55 B[1][2] = 56 n * note that C begins its subscripts at 0. The rightmost subscript is incremented first. 26

Multi-Dimensional Arrays (cont. . ) n n can use braces ({ }) to separate

Multi-Dimensional Arrays (cont. . ) n n can use braces ({ }) to separate rows in 2 -dimensional arrays. 3 columns For example: n n n int C [4][3] = {{1, 2, 3}, {4, 5, 6}, rows {7, 8, 9}, columns {10, 11, 12}}; 4 rows int C [4][3] = {{1, 2}, {4, 5, 6}, {7}, {10, 11, 12}}; initializes C[0][2], C[2][1] and C[2][2] to be zero int C [ ][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; implicitly declares the number of rows to be 4 27

Notes on Arrays n n Arrays enable better and easier data management system Closely

Notes on Arrays n n Arrays enable better and easier data management system Closely related to loops Indexing is zero-based (0 to n-1 for an array with n locations) Multi-dimensional arrays require nested loop structure (e. g. 2 -dimensional array) 28

Index out of bounds n n ‘Out of bounds’ is when (index < 0)

Index out of bounds n n ‘Out of bounds’ is when (index < 0) or (index > array. Size - 1) It is a run-time error, happens when an index is outside the valid boundaries of the array. Example: int A[10]; int X = 10 A[9] = 3 ; //ok A[X] = 4 ; //10 is not within the range 0… 9 29

Index out of bound n In C, no guard against this problem n n

Index out of bound n In C, no guard against this problem n n Does not check whether index value is within range or not Can result in accessing data of wrong memory location 30

How to overcome? n Use defined loops for (i. Loop = 0; i. Loop

How to overcome? n Use defined loops for (i. Loop = 0; i. Loop < 10; i. Loop ++) List [ i. Loop ] = 0; 31