Deitel_Chapter-06_1pp
Deitel_Chapter-06_1pp
Chapter 6
Arrays
Chapter 6 - Arrays
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
3
Objectives
6.1 Introduction
• Arrays
– Structures of related data items
– Static entity – same size throughout program
– Dynamic data structures discussed in Chapter 12
5
Name of array (Note
that all elements of
6.2 Arrays this array have the
same name, c)
• Array
– Group of consecutive memory locations
c[0] -45
– Same name and type c[1] 6
c[2] 0
• To refer to an element, specify c[3] 72
METHOD - 2:
METHOD - 3:
int c[12];
int c[12], i;
C[0] = -45;
for (i=0; i <= 11; i++)
C[1] = 6;
{
C[2] = 0;
printf(“Enter element - %d”, i);
C[3] = 72;
scanf(“%d”, &c[i]);
........
}
C[11] = 78;
7
6.2 Arrays
• Array elements are like normal variables
c[ 0 ] = 3;
printf( "%d", c[ 0 ] );
– Perform operations in subscript. If x equals 3
c[ 5 - 2 ] == c[ 3 ] == c[ x ]
8
6.2 Arrays
N N
∑X i ∑ (x i − x) 2
Avg ( x ) = i =1 Variance = i =1
N N −1
∑ x −x i
Absolute deviation = i =1
N
28
int main()
{
int score[MAXSTUDENTS];
int N = 0; // Number of students
float avg, variance, std_dev, abs_dev;
float total = 0.0, sqr_total = 0.0, abs_total = 0.0;
int i = 0;
system("pause");
return 0;
}
30
Average = 68.750000
Variance = 300.916656
Standard deviation = 17.346949
Absolute deviation = 12.250000
1 /* Fig. 6.11: fig06_11.c
31
2 Static arrays are initialized to zero */ Outline
3 #include <stdio.h>
4
5 void staticArrayInit( void ); /* function prototype */ fig06_11.c (Part 1
6 void automaticArrayInit( void ); /* function prototype */ of 3)
7
8 /* function main begins program execution */
9 int main()
10 {
11 printf( "First call to each function:\
function:\n" );
12 staticArrayInit();
13 automaticArrayInit();
14
15 printf( "\n\nSecond call to each function:\
function:\n" );
16 staticArrayInit();
staticArrayInit();
17 automaticArrayInit();
18
19 return 0; /* indicates successful termination */
20
21 } /* end main */
22
23 /* function to demonstrate a static local array */
32
24 void staticArrayInit( void )
Outline
25 {
26 /* initializes elements to 0 first time function is called */
27 static int array1[ 3 ]; fig06_11.c (Part 2
28 int i; /* counter */ of 3)
29
30 printf( "\nValues on entering staticArrayInit:\
staticArrayInit:\n" );
31
32 /* output contents of array1 */
33 for ( i = 0; i <= 2; i++ ) {
34 printf( "array1[ %d ] = %d ",
", i, array1[ i ] );
35 } /* end for */
36
37 printf( "\nValues on exiting
exiting staticArrayInit:\
staticArrayInit:\n" );
38
39 /* modify and output contents of array1 */
40 for ( i = 0; i <= 2; i++ ) {
41 printf( "array1[ %d ] = %d ",
", i, array1[ i ] += 5 );
42 } /* end for */
43
44 } /* end function staticArrayInit
staticArrayInit */
45
46 /* function to demonstrate an automatic local array */
33
47 void automaticArrayInit( void )
Outline
48 {
49 /* initializes elements each time function is called */
50 int array2[ 3 ] = { 1, 2, 3 }; fig06_11.c (Part 3
51 int i; /* counter */ of 3)
52
53 printf( "\n\nValues on entering automaticArrayInit:\
automaticArrayInit:\n" );
54
55 /* output contents of array2 */
56 for ( i = 0; i <= 2; i++ ) {
57 printf("array2[
printf("array2[ %d ] = %d ",
", i, array2[ i ] );
58 } /* end for */
59
60 printf( "\nValues on exiting automaticArrayInit:\
automaticArrayInit:\n" );
61
62 /* modify and output contents of array2 */
63 for ( i = 0; i <= 2; i++ ) {
64 printf( "array2[ %d ] = %d ",
", i, array2[ i ] += 5 );
65 } /* end for */
66
67 } /* end function
function automaticArrayInit */
34
First call to each function:
Outline
Values on entering staticArrayInit:
staticArrayInit:
array1[ 0 ] = 0 array1[ 1 ] = 0 array1[ 2 ] = 0
Program Output
Values on exiting staticArrayInit:
staticArrayInit:
array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5
********
Median
********
The unsorted array of responses is
6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8
6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9
6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3
5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8
7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7
Program Output
********
(continued)
Mode
********
Response Frequency Histogram
1 1 2 2
5 0 5 0 5
1 1 *
2 3 ***
3 4 ****
4 5 *****
5 8 ******** Histogram Scales
6 9 *********
7 23 ***********************
8 27 ***************************
5, 10, 15, 20, 25
9 19 *******************
The mode is the most frequent value.
For this run the mode is 8 which occurred 27 times.
59
6.8 Searching Arrays: Linear Search and
Binary Search
• Search an array for a key value
• Linear search
– Simple
– Compare each element of array with key value
– Useful for small and unsorted arrays
60
Linear search
61
6.8 Searching Arrays: Linear Search and
Binary Search
• Binary search
– For sorted arrays
– Compares middle element with key
• If equal, match found
• If key < middle, looks in first half of array
• If key > middle, looks in last half
• Repeat
– Very fast; at most n steps, where 2n > number of elements
• 30 element array takes at most 5 steps
– 25 > 30 so at most 5 steps
62
Binary search
1 /* Fig. 6.18: fig06_18.c
63
2 Linear search of an array */ Outline
3 #include <stdio.h>
4 #define SIZE 100
5 fig06_18.c (Part 1
6 /* function prototype */ of 3)
7 int linearSearch( const int array[ ], int key, int size );
8
9 /* function main begins program execution */
10 int main()
11 {
12 int a[ SIZE ]; /* create array a */
13 int x; /* counter */
14 int searchKey; /* value to locate in a */
15 int element; /* variable to hold location of searchKey or -1 */
*/
16
17 /* create data */
18 for ( x = 0; x < SIZE;
SIZE; x++ ) {
19 a[ x ] = 2 * x;
20 } /* end for */
21
22 printf( "Enter integer search key:\
key:\n" );
23 scanf( "%d",
"%d", &searchKey );
24
25 /* attempt to locate searchKey in array a */
64
element = linearSearch( a, searchKey, SIZE );
26
Outline
27
28 /* display results */
29 if ( element != -1 ) { fig06_18.c (Part 2
30 printf( "Found value in element %d\
%d\n",
n", element ); of 3)
31 } /* end
end if */
32 else {
33 printf( "Value not found\
found\n" );
34 } /* end else */
35
36 return 0; /* indicates successful termination */
37
38 } /* end main */
39
40 /* compare key to every element of array until the location
location is found
41 or until the end of array is reached; return subscript of element
42 if key or -1 if key is not found */
43 int linearSearch( const int array[ ], int key, int size )
44 {
45 int n; /* counter */
46
47 /* loop through array */
48 for ( n = 0; n < size; ++n ) {
49
50 if ( array[ n ] == key ) {
65
return n; /* return location of key */
51 Outline
52 } /* end if */
53
54 } /* end for */ fig06_18.c (Part 3
55 of 3)
56 return -1; /* key not found */
57
58 } /* end function linearSearch */
25 not found
Subscripts:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
------------------------------------------------------------
0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28
0 2 4 6* 8 10 12
8 10* 12
8*
Column subscript
Array
name Row subscript
74
int main() {
int a[2][2] = {{10, 15}, {20, 5}}; /* Matrix a */
int b[2][2] = {{25, 5}, { 6, 0}}; /* Matrix b */
int c[2][2]; /* Matrix c */
int i, j;
printf ("RESULTING ADDITION MATRIX \n\n");
system("PAUSE");
return 0;
}
78
35 20
26 5
int main()
{
int a[N][M] = {{8, 5, -6, 7},
{0, 2, 1, 4}
}; /* Matrix a */
int i,j,k;
82
system("PAUSE");
return 0;
}
84
53 -22 90
6 42 40
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