210 likes | 226 Views
INC 161 , CPE 100 Computer Programming. Lecture 6 Array. Array = Group of data Array Characteristics Easily be processed with loops Can be of any data type Has an index as integer. Declaration of Arrays. One dimensional array Using following format to declare a one dimensional array
E N D
INC 161 , CPE 100Computer Programming Lecture 6 Array
Array = Group of data Array Characteristics • Easily be processed with loops • Can be of any data type • Has an index as integer
Declaration of Arrays • One dimensional array Using following format to declare a one dimensional array type name[expr]; where type is a data type, name is an identifier, expr is an integral expression. The value of expr is the number of elements of the array. For example, int a[6]; declares a one-dimensional array. The name of array is a. The data type of array elements is int. Array a has 6 elements from a[0] to a[5].
How Arrays are Stored in Memory An array is a group of consecutive memory location. All the elements of an array have the same name and data type. Example1: (One-dimensional array) int a[6]; One dimensional array a has six elements. The data type of array elements is int. Array a occupies 24 consecutive bytes in the memory as shown on the right. The elements of an array can be referred by the format name[position_number] First element at position 0. In our example, the elements of array a can be represented as a[0], a[1], a[2], a[3], a[4], and a[5].
2000 • How array elements are referenced int i, a[6]; a[0] = 10; a[5] = 2*a[0]; i = a[0]+a[5] 2001 a[0] 2002 2003 2004 2005 a[1] 2006 2007 2008 2009 a[2] 200A 200B
Two-dimensional array Using following format to declare a two-dimensional array type name[expr1][expr2]; expr1 and expr2 are integral expressions. The value of expr1 specifies the number of rows of the array and the value of expr2 specifies the number of columns of the array. For example, int a[2][3]; declares a two-dimensional array. Array a has 2 rows and 3 columns with 6 elements.
Example2: (Two-dimensional arrays) int b[2][3]; Two dimensional array b has six elements (2x3) with 2 horizontal rows and 3 vertical columns. From a programming point of view, it is a block of memory with each row of the array lying in a contiguous block of memory as show below. The elements of a two-dimensional array can be referred by the format arrayname[row_subscript][column_subscript]
Size of Array The sizeof operator can be used to determine the size of an array. > int a[6] > sizeof(a) 24 > int c[3][4] > sizeof(c) 48 > double d[3][4] > sizeof(d) 96 > char c[6] > sizeof(c) 6
Initialization of Arrays • One-dimensional array • int a[5] = {1, 2, 3, 4, 5}; • If not enough initializers, rightmost elements become 0 • If too many, syntax error • int a[5] = {0}; • All elements 0 • int a[] = {1, 2, 3, 4, 5}; • The size is omitted, initializers determine it • 5 initializers, therefore, five-element array • The size of array a is 20 bytes. • char str1[] = “Hello"; • null character '\0' terminates strings • str1 actually has 6 elements • char str1[] = { ‘H', ‘e', ‘l', ‘l', ‘o', '\0' };
Two dimensional array 1. int a[2][2] = {{1, 2 }, { 3, 4 } }; • Initializers grouped by row in braces • a[0][0] = 1, a[0][1] = 2, a[1][0] = 3, a[1][1] = 4 2. int b[2][2] = {{1}, {3, 4 } }; • If not enough, unspecified elements set to zero • b[0][0] = 1, b[0][1] = 0, b[1][0] = 3, b[1][1] = 4 3. int c[2][2] = {1, 2, 3, 4}; c[0][0] = 1, c[0][1] = 2, c[1][0] = 3, c[1][1] = 4
When do we need arrays? Arrays are useful in loop processing of large data. Data that needs repetitive processing can be defined and indexed in a loop. e.g. We want to multiply three data with 2. int a1; int a2; int a3; a1 = a1 * 2; a2 = a2 * 2; a3 = a3 * 2; int a[3]; int i; for(i=0;i<3;i++) { a[i] = a[i] * 2; }
mean (average) Example: Process Data in Array The mean or average value is defined as the sum of all elements divided by the number of elements.
Calculate the mean of data /* File: mean.c */ #include <stdio.h> #define N 8 main() { double a[N] = {3, 21, 0, -3, 34, -14, 45, 18}; double sum, meanval; int i; sum = 0.0; for(i=0; i<N; i++) { sum += a[i]; } meanval = sum / N; printf("The mean value of the array is %f\n", meanval); }
Segmentation fault Segmentation fault happens when the index to an array is invalid. The code can be successfully compiled but the program will be error in run time. int a[5]; a[6]++; a[5]++; a[-2]++; int i = 10; a[i]++;
Find the minimum of data /* File: minimum.c */ #include <stdio.h> #define N 8 main() { double a[N] = {3, 21, 0, -3, 34, -14, 45, 18}; double minval; int i; minval = a[0]; for(i= 1; i < N; i++) { if(a[i] < minval) { minval = a[i]; } } printf("The minimum value of the array is %f\n", minval); }
i = 0 a[i] 3 21 0 -3 34 -14 45 18 minval = 3
i = 1 a[i] 3 21 0 -3 34 -14 45 18 a[i] > minval 21 > 3 minval = 3
i = 2 a[i] 3 21 0 -3 34 -14 45 18 a[i] < minval 0 < 3 minval = 3 minval = 0
i = 3 a[i] 3 21 0 -3 34 -14 45 18 a[i] < minval -3 < 0 minval = 0 minval = -3
i = 4 a[i] 3 21 0 -3 34 -14 45 18 a[i] > minval 34 > -3 minval = -3