Chapter 6 - Arrays: Outline
Chapter 6 - Arrays: Outline
Outline
6.1 Introduction
6.2 Arrays
6.3 Declaring Arrays
6.4 Examples Using Arrays
6.5 Passing Arrays to Functions
6.6 Sorting Arrays
6.7 Case Study: Computing Mean, Median and Mode Using
Arrays
6.8 Searching Arrays
6.9 Multiple-Subscripted Arrays
• Function prototype
void modifyArray( int b[], int arraySize );
– Parameter names optional in prototype
• int b[] could be simply int []
• int arraySize could be simply int
• Mean - average
• Median - number in middle of sorted list
– 1, 2, 3, 4, 5
3 is the median
• Mode - number that occurs most often
– 1, 1, 1, 2, 3, 3, 4, 5
1 is the mode
• Linear search
– Simple
– Compare each element of array with key value
– Useful for small and unsorted arrays
Column subscript
Array name
Row subscript
• Initialization
1 2
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
3 4
– Initializers grouped by row in braces
– If not enough, unspecified elements set to zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 1 0
3 4
• Referencing elements
– Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );
Lowest grade: 68
Highest grade: 96
The average grade for student 0 is 76.00
The average grade for student 1 is 87.50
The average grade for student 2 is 81.75