Chapter 6 - Arrays: Outline

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 52

1

Chapter 6 - Arrays
Outline
6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays Case Study: Computing Mean, Median and Mode Using Arrays Searching Arrays Multiple-Subscripted Arrays

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Objectives

In this chapter, you will learn:


To introduce the array data structure. To understand the use of arrays to store, sort and search lists and tables of values. To understand how to define an array, initialize an array and refer to individual elements of an array. To be able to pass arrays to functions. To understand basic sorting techniques. To be able to define and manipulate multiple subscript arrays.

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.1
Arrays

Introduction

Structures of related data items Static entity same size throughout program Dynamic data structures discussed in Chapter 12

Basically an array is a structured collection of items (elements) having the same type. Each element of the array can be used as a variable.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.2
Array

Arrays

Name that this same

of array (Note all elements of array have the name, c)

Group of consecutive memory locations Same name and type

c[0] c[1] c[2] c[3]

-45 6 0 72

To refer to an element, specify


Array name Position number

c[4]
c[5] c[6] c[7] c[8] c[9] c[10] c[11]

1543
-89 0 62 -3 1 6453 78

Format:
arrayname[ position number ]

First element at position 0 n element array named c:


c[ 0 ], c[ 1 ]...c[ n 1 ]

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Position number of the element within array c

6.2

Arrays

Array elements are like normal variables


c[ 0 ] = 3; printf( "%d", c[ 0 ] );

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.2

Arrays
Type highest unary multiplicative additive relational equality logical and logical or conditional assignment comma

Operators Associativity [] () left to right ++ -- ! (type) right to left * / % left to right + left to right < <= > >= left to right == != left to right && left to right || left to right ?: right to left = += -= *= /= %= right to left , left to right Fig. 6.2 Operator precedence.

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

6.3

Defining Arrays

When defining arrays, specify


Name Type of array Number of elements
arrayType arrayName[ numberOfElements ];

Examples:
int c[ 10 ]; float myArray[ 3284 ];

Defining multiple arrays of same type


Format similar to regular variables Example:
int b[ 100 ], x[ 27 ];
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

/* Fig. 6.3: fig06_03.c initializing an array */ #include <stdio.h> /* function main begins program execution */ int main() { int n[ 10 ]; /* n is an array of 10 integers */ int i; /* counter */

Outline

fig06_03.c

/* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] = 0; /* set element at location i to 0 */ } /* end for */ printf( "%s%13s\n", "Element", "Value" ); /* output contents of array n in tabular format */ for ( i = 0; i < 10; i++ ) { printf( "%7d%13d\n", i, n[ i ] ); } /* end for */ return 0; /* indicates successful termination */

Copyright 1992 2004*/ by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 } /* end main

Element 0 1 2 3 4 5 6 7 8 9

Value 0 0 0 0 0 0 0 0 0 0

Outline
Program Output

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

10

6.4
Initializers

Examples Using Arrays

int n[ 5 ] = { 1, 2, 3, 4, 5 };

If not enough initializers, rightmost elements become 0


int n[ 5 ] = { 0 } All elements 0 If too many a syntax error is produced syntax error

C arrays have no bounds checking If size omitted, initializers determine it


int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

/* Fig. 6.4: fig06_04.c Initializing an array with an initializer list */ #include <stdio.h>

11

Outline
fig06_04.c

/* function main begins program execution */ int main() { /* use initializer list to initialize array n */ int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; int i; /* counter */ printf( "%s%13s\n", "Element", "Value" ); /* output contents of array in tabular format */ for ( i = 0; i < 10; i++ ) { printf( "%7d%13d\n", i, n[ i ] ); } /* end for */ return 0; /* indicates successful termination */

21 } /* end main */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Element 0 1 2 3 4 5 6 7 8 9

Value 32 27 64 18 95 14 90 70 60 37

12

Outline
Program Output

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 /* Fig. 6.5: fig06_05.c 2 Initialize the elements of array s to the even integers from 2 to 20 */

13

Outline
fig06_05.c

3 #include <stdio.h> 4 #define SIZE 10 5 6 /* function main begins program execution */ 7 int main() 8 { 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 return 0; /* indicates successful termination */ /* output contents of array s in tabular format */ for ( j = 0; j < SIZE; j++ ) { printf( "%7d%13d\n", j, s[ j ] ); } /* end for */ printf( "%s%13s\n", "Element", "Value" ); for ( j = 0; j < SIZE; j++ ) { /* set the values */ s[ j ] = 2 + 2 * j; } /* end for */ /* symbolic constant SIZE can be used to specify array size */ int s[ SIZE ]; /* array s has 10 elements */ int j; /* counter */

25 Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 } /* end main */

Element 0 1 2 3 4 5 6 7 8 9

Value 2 4 6 8 10 12 14 16 18 20

14

Outline
Program Output

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

/* Fig. 6.6: fig06_06.c Compute the sum of the elements of the array */ #include <stdio.h> #define SIZE 12 /* function main begins program execution */ int main() { /* use initializer list to initialize array */ int a[ SIZE ] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45 }; int i; /* counter */ int total = 0; /* sum of array */ /* sum contents of array a */ for ( i = 0; i < SIZE; i++ ) { total += a[ i ]; } /* end for */

15

Outline
fig06_06.c

Total of array element values is 383


Program Output

printf( "Total of array element values is %d\n", total ); return 0; /* indicates successful termination */

23 } /* end main */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19
20

/* Fig. 6.7: fig06_07.c Student poll program */ #include <stdio.h> #define RESPONSE_SIZE 40 /* define array sizes */ #define FREQUENCY_SIZE 11 /* function main begins program execution */ int main() { int answer; /* counter */ int rating; /* counter */ /* initialize frequency counters to 0 */ int frequency[ FREQUENCY_SIZE ] = { 0 }; /* place survey responses in array responses */
int responses[ RESPONSE_SIZE ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };

16

Outline
fig06_07.c (Part 1 of 2)

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

/* for each answer, select value of an element of array responses and use that value as subscript in array frequency to determine element to increment */ for ( answer = 0; answer < RESPONSE_SIZE; answer++ ) { ++frequency[ responses [ answer ] ]; } /* end for */ /* display results */ printf( "%s%17s\n", "Rating", "Frequency" ); /* output frequencies in tabular format */ for ( rating = 1; rating < FREQUENCY_SIZE; rating++ ) { printf( "%6d%17d\n", rating, frequency[ rating ] );
Rating Frequency 1 2 2 2 return 0; /* indicates successful termination */ 3 2 4 2 5 5 } /* end main */ 6 11 7 5 8 7 9 1 10 3

17

Outline
fig06_07.c (Part 2 of 2)

} /* end for */

Program Output

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3

/* Fig. 6.7: fig06_07.c Student poll program */ #include <stdio.h> array sizes */

18

Outline
fig06_07.c (Part 1 of 2)

#define RESPONSE_SIZE 40 4 #define RESPONSE_SIZE 40 /* define


5 6 7 #define FREQUENCY_SIZE 11

/* function main begins program execution */ {

8 int for ( main() answer=0; answer < RESPONSE_SIZE; answer++) { 9 10 11 } 12 13 14 15 16


17 18 19
20

int answer; /* counter */ int rating; /* counter */

++frequency[ responces[answer] ];

/* initialize frequency counters to 0 */ int frequency[ FREQUENCY_SIZE ] = { 0 }; /* place survey responses in array responses */
int responses[ RESPONSE_SIZE ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

19

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

/* Fig. 6.8: fig06_08.c Histogram printing program */ #include <stdio.h> #define SIZE 10

20

Outline
fig06_08.c (Part 1 of 2)

/* function main begins program execution */ int main() { /* use initializer list to initialize array n */ int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; int i; /* outer counter */ int j; /* inner counter */

printf( "%s%13s%17s\n", "Element", "Value", "Histogram" );

/* for each element of array n, output a bar in histogram */ for ( i = 0; i < SIZE; i++ ) { printf( "%7d%13d ", i, n[ i ]) ;

for ( j = 1; j <= n[ i ]; j++ ) { /* print one bar */ printf( "%c", '*' ); } /* end inner for */

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

21

Outline
24 25 26 27 28 29 } /* end main */ return 0; /* indicates successful termination */ printf( "\n" ); /* start next line of output */ } /* end outer for */

fig06_08.c (Part 2 of 2) Program Output

Element 0 1 2 3 4 5 6 7 8 9

Value 19 3 15 7 11 9 13 5 17 1

Histogram ******************* *** *************** ******* *********** ********* ************* ***** ***************** *

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9

/* Fig. 6.9: fig06_09.c Roll a six-sided die 6000 times */ #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 7 /* function main begins program execution */ int main() int face; int roll; /* random number with value 1 - 6 */ /* roll counter */

22

Outline
fig06_09.c (Part 1 of 2)

10 { 11 12 13 14 15 16 17 18 19 20 21 22 23 printf( "%s%17s\n", "Face", "Frequency" );


Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24

int frequency[ SIZE ] = { 0 }; /* initialize array to 0 */ srand( time( NULL ) ); /* seed random-number generator */ /* roll die 6000 times */ for ( roll = 1; roll <= 6000; roll++ ) { face = rand() % 6 + 1; ++frequency[ face ]; /* replaces 26-line switch of Fig. 5.8 */ } /* end for */

25 26 27 28 29 30 31

/* output frequency elements 1-6 in tabular format */ for ( face = 1; face < SIZE; face++ ) { printf( "%4d%17d\n", face, frequency[ face ] ); } /* end for */ return 0; /* indicates successful termination */

23

Outline
fig06_09.c (Part 2 of 2)

Program Output

32 } /* end main */

Face 1 2 3 4 5 6

Frequency 1029 951 987 1033 1010 990

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

24

6.4

Examples Using Arrays

Character arrays
String first is really a static array of characters Character arrays can be initialized using string literals
char string1[] = "first";

Null character '\0' terminates strings string1 actually has 6 elements It is equivalent to
char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };

Can access individual characters


string1[ 3 ] is character s

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

25

Array name is address of array, so & not needed for scanf


scanf( "%s", string2 );

Reads characters until white-space encountered Can write beyond end of array, be careful

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

/* Fig. 6.10: fig06_10.c Treating character arrays as strings */ #include <stdio.h> /* function main begins program execution */ int main() { char string1[ 20 ]; int i; /* reserves 20 characters */ /* counter */ char string2[] = "string literal"; /* reserves 15 characters */

26

Outline
fig06_10.c (Part 1 of 2)

/* read string from user into array string2 */ printf("Enter a string: "); scanf( "%s", string1 ); /* output strings */ printf( "string1 is: %s\nstring2 is: %s\n" "string1 with spaces between characters is:\n", string1, string2 ); /* output characters until null character is reached */ for ( i = 0; string1[ i ] != '\0'; i++ ) { printf( "%c ", string1[ i ] );

24 } /* end for */ & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Copyright 1992 2004 by Deitel 25

26 27 28 29

printf( "\n" );

27

Outline
return 0; /* indicates successful termination */

30 } /* end main */

fig06_10.c (Part 2 of 2)

Typed by user

Enter a string1 string2 string1 H e l l

string: Hello there is: Hello is: string literal with spaces between characters is: o

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9

/* Fig. 6.11: fig06_11.c Static arrays are initialized to zero */ #include <stdio.h> void staticArrayInit( void ); /* function prototype */

28

Outline
fig06_11.c (Part 1 of 3)

void automaticArrayInit( void ); /* function prototype */ /* function main begins program execution */ int main() printf( "First call to each function:\n" ); staticArrayInit(); automaticArrayInit(); printf( "\n\nSecond call to each function:\n" ); staticArrayInit(); automaticArrayInit(); return 0; /* indicates successful termination */

10 { 11 12 13 14 15 16 17 18 19 20 21 } /* end main */ 22
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

23 /* function to demonstrate a static local array */ 24 void staticArrayInit( void ) 25 { 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 } /* end function staticArrayInit */ 45 /* modify and output contents of array1 */ for ( i = 0; i <= 2; i++ ) { printf( "array1[ %d ] = %d } /* end for */ ", i, array1[ i ] += 5 ); printf( "\nValues on exiting staticArrayInit:\n" ); /* output contents of array1 */ for ( i = 0; i <= 2; i++ ) { printf( "array1[ %d ] = %d } /* end for */ ", i, array1[ i ] ); printf( "\nValues on entering staticArrayInit:\n" ); /* initializes elements to 0 first time function is called */ static int array1[ 3 ]; int i; /* counter */

29

Outline
fig06_11.c (Part 2 of 3)

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

46 /* function to demonstrate an automatic local array */ 47 void automaticArrayInit( void ) 48 { 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 } /* end function automaticArrayInit */ /* modify and output contents of array2 */ for ( i = 0; i <= 2; i++ ) { printf( "array2[ %d ] = %d } /* end for */ ", i, array2[ i ] += 5 ); printf( "\nValues on exiting automaticArrayInit:\n" ); /* output contents of array2 */ for ( i = 0; i <= 2; i++ ) { printf("array2[ %d ] = %d } /* end for */ ", i, array2[ i ] ); printf( "\n\nValues on entering automaticArrayInit:\n" ); /* initializes elements each time function is called */ int array2[ 3 ] = { 1, 2, 3 }; int i; /* counter */

30

Outline
fig06_11.c (Part 3 of 3)

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

First call to each function: Values on entering staticArrayInit: array1[ 0 ] = 0 array1[ 1 ] = 0 array1[ 2 ] = 0 Values on exiting staticArrayInit: array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5 Values on array2[ 0 Values on array2[ 0 entering automaticArrayInit: ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3 exiting automaticArrayInit: ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8

31

Outline
Program Output

Second call to each function: Values on entering staticArrayInit: array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5 Values on exiting staticArrayInit: array1[ 0 ] = 10 array1[ 1 ] = 10 array1[ 2 ] = 10

Values on array2[ 0 Values on array2[ 0

entering automaticArrayInit: ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3 exiting automaticArrayInit: ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

First call to each function: Values on entering staticArrayInit: array1[ 0 ] = 0 array1[ 1 ] = 0 array1[ 2 ] = 0 Values on exiting staticArrayInit: array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5 Values on array2[ 0 Values on array2[ 0 entering automaticArrayInit: ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3 exiting automaticArrayInit: ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8

32

Outline
Program Output

Second call to each function: Values on entering staticArrayInit: array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5 Values on exiting staticArrayInit: array1[ 0 ] = 10 array1[ 1 ] = 10 array1[ 2 ] = 10

Values on array2[ 0 Values on array2[ 0

entering automaticArrayInit: ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3 exiting automaticArrayInit: ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

First call to each function: Values on entering staticArrayInit: array1[ 0 ] = 0 array1[ 1 ] = 0 array1[ 2 ] = 0 Values on exiting staticArrayInit: array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5 Values on array2[ 0 Values on array2[ 0 entering automaticArrayInit: ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3 exiting automaticArrayInit: ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8

33

Outline
Program Output

Second call to each function: Values on entering staticArrayInit: array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5 Values on exiting staticArrayInit: array1[ 0 ] = 10 array1[ 1 ] = 10 array1[ 2 ] = 10

Values on array2[ 0 Values on array2[ 0

entering automaticArrayInit: ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3 exiting automaticArrayInit: ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

34

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

35

6.5

Passing Arrays to Functions

Passing arrays
To pass an array argument to a function, specify the name of the array without any brackets
int myArray[ 24 ]; myFunction( myArray, 24 );

Array size usually passed to function

Arrays passed call-by-reference Name of array is address of first element Function knows where the array is stored
Modifies original memory locations

Passing array elements


Passed by call-by-value Pass subscripted name (i.e., myArray[ 3 ]) to function
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

36

6.5

Passing Arrays to Functions

Function prototype
void modifyArray( int b[], int arraySize );

Parameter names optional in prototype only


int b[] could be written int [] int arraySize could be simply int

The following are equivalent prototypes:


void modifyArray( int b[], int arraySize ); void modifyArray( int [], int );

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

/* Fig. 6.12: fig06_12.c The name of an array is the same as &array[ 0 ] */ #include <stdio.h>

37

Outline
fig06_12.c

/* function main begins program execution */ int main() { char array[ 5 ]; /* define an array of size 5 */ printf( " " array = %p\n&array[0] = %p\n"

&array = %p\n",

array, &array[ 0 ], &array ); return 0; /* indicates successful termination */

Program Output

16 } /* end main */

array = 0012FF78 &array[0] = 0012FF78 &array = 0012FF78

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9

/* Fig. 6.13: fig06_13.c Passing arrays and individual array elements to functions */ #include <stdio.h> #define SIZE 5 /* function prototypes */ void modifyArray( int b[], int size ); void modifyElement( int e );

38

Outline
fig06_13.c (Part 1 of 3)

10 /* function main begins program execution */ 11 int main() 12 { 13 14 15 16 17 18 19 20 21 22 23 24 printf( "\n" ); Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 /* output original array */ for ( i = 0; i < SIZE; i++ ) { printf( "%3d", a[ i ] ); } /* end for */ printf( "Effects of passing entire array by reference:\n\nThe " "values of the original array are:\n" ); int a[ SIZE ] = { 0, 1, 2, 3, 4 }; /* initialize a */ int i; /* counter */

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/* pass array a to modifyArray by reference */ modifyArray( a, SIZE ); printf( "The values of the modified array are:\n" ); /* output modified array */ for ( i = 0; i < SIZE; i++ ) { printf( "%3d", a[ i ] ); } /* end for */ /* output value of a[ 3 ] */ printf( "\n\n\nEffects of passing array element " "by value:\n\nThe value of a[3] is %d\n", a[ 3 ] ); modifyElement( a[ 3 ] ); /* pass array element a[ 3 ] by value */ /* output value of a[ 3 ] */ printf( "The value of a[ 3 ] is %d\n", a[ 3 ] ); return 0; /* indicates successful termination */

39

Outline
fig06_13.c (Part 2 of 3)

47 } /* end main */ 48
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

49 /* in function modifyArray, "b" points to the original array "a" 50 52 { 53 54 55 56 57 58 59 60 } /* end function modifyArray */ 61 62 /* in function modifyElement, "e" is a local copy of array element 63 65 { 66 67 /* multiply parameter by 2 */ printf( "Value in modifyElement is %d\n", e *= 2 ); a[ 3 ] passed from main */ 64 void modifyElement( int e ) /* multiply each array element by 2 */ for ( j = 0; j < size; j++ ) { b[ j ] *= 2; } /* end for */ int j; /* counter */ in memory */

40

Outline
fig06_13.c (Part 3 of 3)

51 void modifyArray( int b[], int size )

68 } /* end function modifyElement */

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Effects of passing entire array by reference: The values of 0 1 2 3 The values of 0 2 4 6 the original array are: 4 the modified array are: 8

41

Outline
Program Output

Effects of passing array element by value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[ 3 ] is 6

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

42

Caution: Passing arrays to functions


As illustrated, the ONLY way to pass arrays to functions is by-reference =>
The function can modify arrays, defined in the main program.

Now, is it possible to prevent a function from modifying the array?

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

43

Prevent a function to modify an array


Yes.

By using the const keyword in function prototype.

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

/* Fig. 6.14: fig06_14.c Demonstrating the const type qualifier with arrays */ #include <stdio.h> void tryToModifyArray( const int b[] ); /* function prototype */ /* function main begins program execution */ int main() { int a[] = { 10, 20, 30 }; /* initialize a */ tryToModifyArray( a ); printf("%d %d %d\n", a[ 0 ], a[ 1 ], a[ 2 ] ); return 0; /* indicates successful termination */

44

Outline
fig06_14.c (Part 1 of 2)

18 } /* end main */ 19
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

20 /* in function tryToModifyArray, array b is const, so it cannot be 21 23 { 24 25 26 b[ 0 ] /= 2; b[ 1 ] /= 2; b[ 2 ] /= 2; /* error */ /* error */ /* error */ used to modify the original array a in main. */

45

Outline
fig06_14.c (Part 2 of 2)

22 void tryToModifyArray( const int b[] )

Program Output

27 } /* end function tryToModifyArray */

Compiling... FIG06_14.C fig06_14.c(24) : error C2166: l-value specifies const object fig06_14.c(25) : error C2166: l-value specifies const object fig06_14.c(26) : error C2166: l-value specifies const object

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

46

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

47

6.9

Multiple-Subscripted Arrays

Multiple subscripted arrays


Tables with rows and columns (m by n array) Like matrices: specify row, then column
Column Column 1 Column Column 3 0 0 ][ 0 ] a[ 0 ][ 1 ] a[ 2 0 ][ 2 ] a[ 0 ][ 3 ] a[
a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ] a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ] Column subscript Array name Row subscript

Row 0 Row 1 Row 2

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

48

6.9

Multiple-Subscripted Arrays

int a[3][4];

Row 0 Row 1 Row 2

Column Column 1 Column Column 3 0 0 ][ 0 ] a[ 0 ][ 1 ] a[ 2 0 ][ 2 ] a[ 0 ][ 3 ] a[


a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ] a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ] Column subscript Array name Row subscript

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

49

6.9

Multiple-Subscripted Arrays

Initialization
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

Initializers grouped by row in braces


b[0][0]
1 2

b[0][1]
3 4

b[1][0]

b[1][1]

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

50

6.9

Multiple-Subscripted Arrays
1 3 2 4

Initialization
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

Initializers grouped by row in braces


If not enough, unspecified elements set to zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
1 3

0 4

Referencing elements
Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

/* Fig. 6.21: fig06_21.c Initializing multidimensional arrays */ #include <stdio.h> void printArray( const int a[][ 3 ] ); /* function prototype */ /* function main begins program execution */ int main() { /* initialize array1, array2, array3 */ int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 }; int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } }; printf( "Values in array1 by row are:\n" ); printArray( array1 ); printf( "Values in array2 by row are:\n" ); printArray( array2 ); printf( "Values in array3 by row are:\n" ); printArray( array3 ); return 0; /* indicates successful termination */

51

Outline
fig06_21.c (Part 1 of 2)

26 } /* end main */ Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27

28 /* function to output array with two rows and three columns */ 29 void printArray( const int a[][ 3 ] ) 30 { 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 } /* end function printArray */ printf( "\n" ); /* start new line of output */ } /* end outer for */ /* output column values */ for ( j = 0; j <= 2; j++ ) { printf( "%d ", a[ i ][ j ] ); } /* end inner for */ /* loop through rows */ for ( i = 0; i <= 1; i++ ) { int i; /* counter */ int j; /* counter */

52

Outline
fig06_21.c (Part 2 of 2)

Program Output

Values in array1 by row are: 1 2 3 4 5 6 Values in array2 by row are: 1 2 3 4 5 0 Values in array3 by row are: 1 2 0 4by 0Deitel 0 & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Copyright 19922004

You might also like