# 10 - Lecture 10| Arrays One-Dimensional Array An Example...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 10| Arrays One-Dimensional Array An Example Array Bounds Two-Dimensional Array An example Multi-Dimensional Array Array Initialization | One Dimensional Array Initialization | Two Dimensional Array Initialization | Multi Dimensional Passing Arrays to Functions | One Dimensional Passing Arrays to Functions | Two Dimensional Passing Arrays to Functions | Multi Dimensional Readings Exercises CSC 1500 { Lecture 10 1 Overview A sequence of data items that are of the same type. Indexible. Number of items xed at compile time. Stored contiguously. CSC 1500 { Lecture 10 2 CSC 1500 Class Grades Suppose there are only three students. #include <stdio.h> #include <stdlib.h> int main(void) { int grade1, grade2, grade3 printf("Student 1? ") scanf("%d", &grade1) printf("Student 2? ") scanf("%d", &grade2) printf("Student 3? ") scanf("%d", &grade3) printf("Average = %.2f\n", (grade1+grade2+grade3)/3.0) return 0 } What if the class size is 100? Declare 100 variables? CSC 1500 { Lecture 10 3 One-Dimensional Array Name of array (Note that all elements of this array have the same name, grade.) grade[0] grade[1] grade[2] grade[3] grade[4] grade[5] 89 76 54 32 91 64 ... ... grade[97] grade[98] grade[99] 83 54 82 Subscript, indicating the position of an element. The array grade can hold 100 distinct integer values simultaneously. The array elements are referred to as grade 2], ..., grade 99]. The integral value inside grade 0] , grade 1] , is called a subscript or index . Note carefully array subscript starts with 0 in the C language. CSC 1500 { Lecture 10 4 An Example #include <stdio.h> #define SIZE 4 int main(void) { int list SIZE] int i, sum=0 for (i=0 i<SIZE i++) { printf("number = ? ") scanf("%d", &list i]) } for (i=0 i<SIZE i++) { sum += list i] } printf("\nSum = %d\n", sum) printf("Average = %.2f\n", (float) sum/SIZE) return 0 } /* variables list 0] ... list SIZE-1] */ number number number number = = = = ? ? ? ? 1 2 3 4 Sum = 10 Average = 2.50 CSC 1500 { Lecture 10 5 Array Bounds #include <stdio.h> #define SIZE 4 int main(void) { int list SIZE] int i, sum=0 for (i=0 i<SIZE i++) { printf("number = ? ") scanf("%d", &list i]) } For (i=0 i<=SIZE i++) sum += list i] printf("sum = %d\n", sum) return 0 } /* list 0] ... list SIZE-1] */ /* up to i < SIZE */ /* wrong array bound: i <= SIZE */ number = ? 1 number = ? 2 number = ? 3 number = ? 4 sum = -9842 <-- 1 + 2 + 3 + 4 = -9842 ?! The value of an array subscript must be in the range 0 to SIZE-1 in the above example. An array subscript value outside this range will cause a runtime error | array out of bound. The e ect of the error is unpredictable, e.g. HANG!. It is the C programmer's responsibility (not the C compiler's) to ensure correct value of array subscript. CSC 1500 { Lecture 10 6 Two-Dimensional Array int a 3] 5] row 0 row 1 row 2 col 0 a 0] 0] a 1] 0] a 2] 0] col 1 a 0] 1] a 1] 1] a 2] 1] col 2 a 0] 2] a 1] 2] a 2] 2] col 3 a 0] 3] a 1] 3] a 2] 3] col 4 a 0] 4] a 1] 4] a 2] 4] In processing array elements of a multi-dimensional array, each dimension requires a single loop to sweep through its indexes, e.g. for-loop. Thus, nested-loops are usually used with multi-dimensional array. CSC 1500 { Lecture 10 7 An example #include <stdio.h> #define #define M N 3 5 /* number of rows */ /* number of columns */ int main(void) { int a M] N], i, j, sum = 0 /* NOTE: M and N are symbolic constants */ /* they are NOT variables */ /*** Array bounds must be determined at compile time ***/ putchar('\n') for (i = 0 i < M ++i) for (j = 0 j < N ++j) a i] j] = i + j for (i = 0 i < M ++i) { for (j = 0 j < N ++j) printf("a %d] %d] = %d printf("\n") } for (i = 0 i < M ++i) for (j = 0 j < N ++j) sum += a i] j] printf("\nsum = %d\n\n", sum) return 0 } /* fill the array */ /* print array values */ ", i, j, a i] j]) /* sum the array */ a 0] 0] = 0 a 1] 0] = 1 a 2] 0] = 2 sum = 45 a 0] 1] = 1 a 1] 1] = 2 a 2] 1] = 3 a 0] 2] = 2 a 1] 2] = 3 a 2] 2] = 4 a 0] 3] = 3 a 1] 3] = 4 a 2] 3] = 5 a 0] 4] = 4 a 1] 4] = 5 a 2] 4] = 6 CSC 1500 { Lecture 10 8 Multi-Dimensional Array Array dimensions Declarations of arrays Remarks int int int a 100] b 2] 7] c 5] 3] 2] a one-dimensional array a two-dimensional array a three-dimensional array Example, #include <stdio.h> int main() { int int a 7] 9] 2] i, j, k, sum=0 ... ... for (i=0 i<7 i++) for (j=0 j<9 j++) for (k=0 k<2 k++) sum += a i] j] k] ... ... } CSC 1500 { Lecture 10 9 Array Initialization | One Dimensional Example, int a 4] = { 3, 4, 5, 6 } results in a 0]<--3 a 1]<--4 a 2]<--5 a 3]<--6 Example, int a ] = { 3, 4, 5, 6 } If an array is declared without a size, and is initialized to a series of values, it is implicitly given the size of the number of initializers . Example, int a 4] = { 3, 4 } results in a 0]<--3 a 1]<--4 a 2]<--0 a 3]<--0 When a list of initializers is shorter than the number of array elements to be initialized, the remaining elements are initialized to zero. CSC 1500 { Lecture 10 10 Array Initialization | Two Dimensional Example, int a 2] 3] = { 1, 2, 3, 4, 5, 6 } results in a 0] 0]<--1 a 1] 0]<--4 a 0] 1]<--2 a 1] 1]<--5 a 0] 2]<--3 a 1] 2]<--6 Note that rst indexing is by rows. Example, int a 2] 3] = { {1, 2, 3}, {4, 5, 6} } Example, int a ] 3] = { {1, 2, 3}, --------{4, 5, 6} } --------- If the size in the rst square bracket pair is empty, then the compiler takes the size from the number of inner brace pairs. Only the rst size could be omitted. CSC 1500 { Lecture 10 11 Array Initialization | Multi Dimensional Example, int a 2] 2] 3] = { { {1, 1, 0}, {2, 0, 0} }, { {3, 0, 0}, {4, 4, 0} }, } Example, int a ] 2] 3] = { { {1, 1, 0}, {2, 0, 0} }, { {3, 0, 0}, {4, 4, 0} }, } CSC 1500 { Lecture 10 12 Passing Arrays to Functions | One Dimensional #include <stdio.h> #define SIZE 5 int sum(int ], int) int sum (int list ], int size) { int sum=0 while (size > 0) { --size sum += list size] } return sum } int main(void) { int list SIZE] int i for (i=0 i<SIZE i++) { printf("number = ? ") scanf("%d", &list i]) } printf("sum = %d\n", sum(list, SIZE)) return 0 } In the calling environment, main(): { The name of the array is used as the parameter. { The bracket pair ] and size is omitted. In the called function, sum(...): { The bracket pair ] is required to tell the compiler that the parameter is an array. { The array size can be omitted. In fact, even if the array size is given, it is ignored by the compiler! Why? { The array size is usually passed as another int parameter. CSC 1500 { Lecture 10 13 Passing Arrays to Functions | One Dimensional #include <stdio.h> #define SIZE 5 void times2(int ], int) (con't) int main(void) { int list SIZE] int i for (i=0 i<SIZE i++) { printf("number = ? ") scanf("%d", &list i]) } times2(list, SIZE) printf("\n") for (i=0 i<SIZE i++) { printf("list %d] x 2 = %d\n", i, list i]) } return 0 } void times2(int array ], int size) { while (size > 0) { --size array size] *= 2 } } CSC 1500 { Lecture 10 14 Passing Arrays to Functions | One Dimensional (con't) number number number number number list list list list list = = = = = ? ? ? ? ? 2 2 2 2 2 1 2 3 4 5 = = = = = 2 4 6 8 10 0] 1] 2] 3] 4] x x x x x The array list ] is not copied. That is, no new array is created in the function times2(). Instead, array ] in function list ] in main(). times2() refers to the array Changes made to array ] really a ect list ]. Note: not violating the variable scoping rule!!! CSC 1500 { Lecture 10 15 Passing Arrays to Functions | Two Dimensional /* similar to the program on page 8 */ #include <stdio.h> #define M 3 #define N 5 int sum(int ] N]) int sum(int a ] N]) { int i, j, sum=0 for (i = 0 i < M ++i) for (j = 0 j < N ++j) sum += a i] j] return sum } int main(void) { int a M] N], i, j putchar('\n') for (i = 0 i < M ++i) for (j = 0 j < N ++j) a i] j] = i + j /* fill the array */ for (i = 0 i < M ++i) { /* print array values */ for (j = 0 j < N ++j) printf("a %d] %d] = %d ", i, j, a i] j]) printf("\n") } printf ("\nsum = %d\n\n", sum(a)) return 0 } In the calling environment, main(): { The name of the array is used as the parameter. { The two bracket pairs ] ] are omitted. In the called function, sum(...): { The rst dimension size can be omitted, while the second dimension size must be speci ed, i.e., a ] N]. { Why? CSC 1500 { Lecture 10 16 Passing Arrays to Functions | Multi Dimensional In the calling environment: { The name of the array is used as the parameter. { The bracket pairs ] ]... are omitted. { Example, fcn(array_3d) In the called function: { Only the rst dimension size can be omitted. { All the other dimension sizes must be speci ed. { The compiler can then calculate the element positions in the main memory. { Example, int fcn(int array_3d ] 10] 20]) { ... } CSC 1500 { Lecture 10 17 Readings Chapter 9, Sections 9.1 { 9.2 Chapter 9, Sections 9.5 { 9.8 Exercises Chapter 9, Exercises 2, 5, 8, 9 2 End of Lecture 10 CSC 1500 { Lecture 10 18 ...
View Full Document

## This note was uploaded on 05/23/2010 for the course COMPUTER S CSC1500 taught by Professor Fung during the Spring '10 term at CUHK.

Ask a homework question - tutors are online