A First Book of ANSI C Fourth Edition Chapter 8 Arrays
Objectives One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional Arrays Common Programming and Compiler Errors A First Book of ANSI C, Fourth Edition 2
Chapter 8 Arrays The variables used so far, scalar variables, each variable can only be used to store a single value at a time or atomic type. This means that the value cannot be further subdivided or separated into a legitimate data type Frequently we may have a set of values, all of the same data type, or called a singledimensional array, that form a logical group. A First Book of ANSI C, Fourth Edition 3
8.1 One Dimensional Arrays A one-dimensional array is a list of values of the same data type. int grades[5] ; More Examples char code[4] ; // an array of four character codes double prices[6] ; // an array of six double precision prices float amount [100] ; //an array of 100 floating point amounts A First Book of ANSI C, Fourth Edition 4
Examples using the elements of the grades array are grades [0] = 98 ; grades [1] = grades [0] 11 grades [2] = 2 * ( grades [0] 6 ); grades [3] = 79 ; grades [4] = (grades [2] + grades [3] 3 ) / 2 ; total = grades [0] + grades [1] + grades [2] + grades [3] + grades [4] ; How to use for loop for adding five grade values? One extremely important advantage of using integer expressions as subscripts is that it allows sequencing through an array using a for loop. A First Book of ANSI C, Fourth Edition 5
Find maximum value When an element with a higher value is located, that element becomes the new maximum. maximum = price[0]; for (i = 1; i <= 999; i++) if (price[i] > maximum) maximum = price[i]; A First Book of ANSI C, Fourth Edition 6
Input and Output of Array Values Individual array elements can be assigned values using individual assignment statements or, interactively, using the scanf( ) function. Examples of individual data entry statements are price[5] = 10.69 ; scanf ( %d %lf, &grades[0], &price[2] ); scanf ( %c, &code[0] ); scanf ( %d %d %d, &grades[0], &grades[1], &grades[2]); A First Book of ANSI C, Fourth Edition 7
Alternatively, a for statement can be used to cycle through the array for interactive data input. For example, the code for (i = 0 ; i <= 4 ; ++i ) { printf ( Enter a grade : ); scanf ( %d, &grade[i] ); } prompts the user for five grades. The first grade entered is stored in grades[0], the second in grades[1], and so on until all five grades are entered. A First Book of ANSI C, Fourth Edition 8
One caution should be mentioned about storing data in an array. C does not check the value of the index being used. If an array has been declared as consisting of 10 elements, for example, and you use an index of 12, which is outside the bounds of the array, C will not notify you of the error when the program is compiled. A First Book of ANSI C, Fourth Edition 9
During output, individual array elements can be displayed using the printf( ) function or complete sections of the array can be displayed by including a printf( ) function call within a for loop. Examples of this are printf ( %lf, price[6] ); printf ( The value of element %d is %d, i, grades[i] ); for ( n = 5 ; n <= 20 ; ++n ) printf ( %d %lf, n, price[n] ); A First Book of ANSI C, Fourth Edition 10
Example of Input and Output of Array Values Sample output: Enter a grade: 85 Enter a grade: 90 Enter a grade: 78 Enter a grade: 75 Enter a grade: 92 grades 0 is 85 grades 1 is 90 grades 2 is 78 grades 3 is 75 grades 4 is 92 A First Book of ANSI C, Fourth Edition 11
Example of Input and Output of Array Values (continued) Statement is outside of the second for loop; total is displayed only once, after all values have been added A First Book of ANSI C, Fourth Edition 12
8.2 Array Initialization Arrays, like scalar variables, can be declared either inside or outside a function. Arrays declared inside a function are local arrays, and arrays declared outside a function are global arrays. A First Book of ANSI C, Fourth Edition 13
Array Initialization (continued) #define SIZE1 20 #define SIZE2 25 #define SIZE3 15 int gallons[size1]; /* a global array */ static int dist[size2]; /* a static global array */ int main() { int miles[size3]; /* an auto local array */ static int course[size3]; /* static local array */ } return 0; A First Book of ANSI C, Fourth Edition 14
Array elements can be initialized within their declaration statements in the same manner as scalar variables, except that the initializing elements must be included in braces {}. Examples of such initializations for automatic, static, and global arrays are int grades[5] = { 98, 87, 92, 79, 85 }; char codes[6] = { s, a, m, p, l, e }; double width[7] = { 10.96, 6.43, 2.58,.86, 5.89, 7.56, 8.22} ; static int temp[4] = {10, 20, 30, 40 } ; static float temp[4] = { 98.6, 97.2, 99.0, 101.5} ; A First Book of ANSI C, Fourth Edition 15
If the number of initializers is less than the declared number of elements listed in square brackets the initializers are applied starting with array element 0. Thus, in the declaration float length[7] = { 7.8, 6.4, 4.9, 11.2 }; only length[0], length[1], length[2] and length[3] are initialized with the listed values. The other array elements will be initialized to zero. A First Book of ANSI C, Fourth Edition 16
A unique feature of initializers is that the size of an array may be omitted when initializing values are included in the declaration statement. For example, the declaration int gallons[ ] = { 16, 12, 10, 14, 11 } ; reserves enough storage room for five elements. Similarly, the following two declarations are equivalent : char codes[6] = { s, a, m, p, l, e }; char codes[ ] = { s, a, m, p, l, e }; A First Book of ANSI C, Fourth Edition 17
An interesting and useful simplification can also be used when initializing character arrays. For example, the declaration char codes[ ] = sample ; // no braces or commas uses the string sample to initialize the codes array. \0, is called the null character The null character is automatically appended to all strings by the C compiler. A First Book of ANSI C, Fourth Edition 18
Array Initialization (continued) A First Book of ANSI C, Fourth Edition 19
8.3 Arrays as Function Arguments Individual array elements are passed to a function by simply including them as subscripted variables in the function call argument list. For example, the function call findmin(grades[2], grades[6] ) ; passes the values of the elements grades[2] and grades[6] to the function findmin ( ). Pass by Value / Pass by Reference? A First Book of ANSI C, Fourth Edition 20
Arrays as Function Arguments (continued) Size can be omitted A First Book of ANSI C, Fourth Edition 21
Arrays as Function Arguments (continued) A First Book of ANSI C, Fourth Edition 22
Case Study: Computing Averages and Standard Deviations Two statistical functions are created to determine the average and standard deviation, respectively, of an array of numbers A First Book of ANSI C, Fourth Edition 23
Write the Functions A First Book of ANSI C, Fourth Edition 24
Write the Functions (continued) A First Book of ANSI C, Fourth Edition 25
8.5 Two-Dimensional Arrays A two-dimensional array, which is sometimes referred to as a table, consists of both rows and columns of elements. For example, the array of numbers 8 16 9 52 3 15 27 6 int val[3][4]; 14 25 2 10 is a two-dimensional array of integers. A First Book of ANSI C, Fourth Edition 26
Two-Dimensional Arrays (continued) A First Book of ANSI C, Fourth Edition 27
The declaration of 2D Array int val[3][4] = { { 8, 16, 9, 52 }, { 3, 15, 27, 6 }, { 14, 25, 2, 10 } } ; declares val to be an array of integers with three rows and four columns, with the initial values given in the declaration. The first set of internal braces contains the values for row 0 of the array, the second set of internal braces contains the values for row 1, and the third set of braces the values for row 2. A First Book of ANSI C, Fourth Edition 28
Two-Dimensional Arrays (continued) is similar int val[3][4] = { { 8, 16, 9, 52 }, { 3, 15, 27, 6 }, { 14, 25, 2, 10 } } ; A First Book of ANSI C, Fourth Edition 29
Two-Dimensional Arrays (continued) A First Book of ANSI C, Fourth Edition 30
Two-Dimensional Arrays (continued) Row size can be omitted How it is different? void display (int nums[][cols] A First Book of ANSI C, Fourth Edition 31
Internal Array Element Location Algorithm (continued) A First Book of ANSI C, Fourth Edition 32
Larger Dimensional Arrays A three-dimensional array can be viewed as a book of data tables. 3-D array of size [2][3][4] Leftmost subscript i = 0 k =0 k =1 Rightmost subscript j = 0 j = 1 j = 2 j = 3 i = 1 i = 2 Middle subscript by Reddy & Ziegler 33
Memory Location a[0][0][0] a[0][0][1] a[0][0][2] a[0][0][3] a[0][1][0] a[0][1][1] a[0][1][2] a[0][1][3] a[0][2][0] a[0][2][1] a[0][2][2] a[0][2][3] a[1][0][0] a[1][0][1] a[1][0][2] a[1][0][3] a[1][1][0] a[1][1][1] a[1][1][2] a[1][1][3] a[1][2][0] a[1][2][1] a[1][2][2] a[1][2][3] by Reddy & Ziegler 34
Declaration and Storage Allocation Declaration of three-dimensional arrays requires three indices: plane, row, and column. On each plane, the row and column indices are the same and the plane index varies from one plane to the next. The plane index varies from 0 to one less than the number of planes, the row index varies from 0 to one less than the number of rows, and the column index varies from 0 to one less than the number of columns. by Reddy & Ziegler 35
by Reddy & Ziegler 36
Input of Three-Dimensional Arrays by Reddy & Ziegler 37
Output of Three-Dimensional Arrays by Reddy & Ziegler 38
Manipulation of Three-Dimensional Arrays by Reddy & Ziegler 39
by Reddy & Ziegler 40
by Reddy & Ziegler 41
Common Programming Errors Forgetting to declare the array Using a subscript that references a nonexistent array element Not using a large enough conditional value in a for loop counter to cycle through all the array elements Forgetting to initialize the array A First Book of ANSI C, Fourth Edition 42
Common Compiler Errors A First Book of ANSI C, Fourth Edition 43
Summary A single-dimensional array is a data structure that can store a list of values of the same data type Elements are stored in contiguous locations Referenced using the array name and a subscript Single-dimensional arrays may be initialized when they are declared Single-dimensional arrays are passed to a function by passing the name of the array as an argument A First Book of ANSI C, Fourth Edition 44
Summary (continued) A two-dimensional array is declared by listing both a row and a column size with the data type and name of the array Two-dimensional arrays may be initialized when they are declared Two-dimensional arrays are passed to a function by passing the name of the array as an argument Three-dimensional arrays (by Reddy & Ziegler) A First Book of ANSI C, Fourth Edition 45