Chapter Eleven Arrays
description
Transcript of Chapter Eleven Arrays
![Page 1: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/1.jpg)
1
Chapter ElevenChapter Eleven
ArraysArrays
![Page 2: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/2.jpg)
2
A Motivating ExampleA Motivating Example
main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3); scanf(“%d”, &n4); printf(“n%d = %d\n”, n4); printf(“n%d = %d\n”, n3); printf(“n%d = %d\n”, n2); printf(“n%d = %d\n”, n1); printf(“n%d = %d\n”, n0);}
Input 5 integers and print them in reverse order
![Page 3: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/3.jpg)
3
ArraysArrays
• An array is a consecutive group of memory locations with two characteristics
• An array is homogeneous: all memory locations in the array store data of the same type
• An array is ordered: memory locations in the array are named in ordered integer index beginning at zero
![Page 4: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/4.jpg)
4
ExamplesExamples
7 9 6 2 3 4
0 1 2 3 4 5
1.7 3.9 7.6 2.5 3.2 6.4
0 1 2 3 4 5
![Page 5: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/5.jpg)
5
Array Declaration & AccessArray Declaration & Access
• Arrays are declared aselement-type array-name [ array-size ];int intArray[6];float floatArray[6];
• Array elements are accessed asintArray[0] = 0;floatArray[ 1] = floatArray[2] + 2.3;
![Page 6: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/6.jpg)
6
ExamplesExamples
7 9 6 2 3 4
0 1 2 3 4 5
1.7 3.9 7.6 2.5 3.2 6.4
0 1 2 3 4 5
intArray
floatArray
0 9 6 2 3 4
0 1 2 3 4 5
intArray
intArray[0] = 0;
1.7 9.9 7.6 2.5 3.2 6.4
0 1 2 3 4 5
floatArray
floatArray[1] = floatArray[2] + 2.3;
![Page 7: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/7.jpg)
7
An ExampleAn Example#define SIZE 5main( ) { int i, n[SIZE];
for ( i = 0; i < SIZE; i++ ) { scanf(“%d”, &n[i]); } for ( i = SIZE - 1; i >= 0; i-- ) { printf(“n[%d] = %d\n”, i, n[i]); }}
![Page 8: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/8.jpg)
8
An ExampleAn Example
#define SIZE 5main( ) { int i, sum, n[SIZE]; sum = 0; for ( i = 0; i < SIZE; i++ ) scanf(“%d”, &n[i]); for ( i = 0; i < SIZE; i++ ) sum += n[i]; printf(“sum = %d\n”, sum);}
![Page 9: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/9.jpg)
9
Address of VariablesAddress of Variables
• In memory, every byte is identified by an address
• Data values requiring multiple bytes are identified by the address of the first byte
int float
![Page 10: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/10.jpg)
10
Address of Array ElementsAddress of Array Elements
int iArray[5]; number of bytes = 4 * 5 = 20
iArray[0] iArray[4]iArray[1] iArray[2] iArray[3]
1000 1004 1008 1012 1016
address of iArray[i] = 1000 + 4 * ibase address offset
![Page 11: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/11.jpg)
11
Common PitfallsCommon Pitfalls
• Whenever you use arrays in your programs, make sure that the index values used to select elements from the array remain within the array bounds
• On most computers, referencing elements that are outside the array bounds is not detected as an error but will certainly lead to unpredictable results
![Page 12: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/12.jpg)
12
Passing Arrays as Passing Arrays as ParametersParameters
#define SIZE 5main( ) { int n[SIZE];
inputArray(n); /* use 0 as sentinel value */ reverseArray(n); printArray(n);}
![Page 13: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/13.jpg)
13
Two IssuesTwo Issues
• The required size of the array n is unknown
• The array n passed to the two functions inputArray and reverseArray should be changed by these two functions
![Page 14: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/14.jpg)
14
Generalizing the Size of Generalizing the Size of ArraysArrays
• The usual strategy is to declare an array that is larger than you need and use only part of it
• The number of elements declared is called the allocated size of the array
• The number of elements actually in use is called the effective size of the array
![Page 15: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/15.jpg)
15
Generalizing the Size of Generalizing the Size of ArraysArrays
int n[MAXSIZE];
void printArray(int n[MAXSIZE], int size);void printArray(int n[], int size);
void reverseArray(int n[], int size);
int inputArray(int n[], int maxsize); int inputArray(int n[], int maxsize, int sentinel);
![Page 16: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/16.jpg)
16
Generalizing the Size of Generalizing the Size of ArraysArrays
#define MAX 100main( ) { int n[MAX], size;
size = inputArray(n, MAX, 0); reverseArray(n, size); printArray(n, size);}
![Page 17: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/17.jpg)
17
Passing Array Passing Array ArgumentsArguments
• When an array is passed to a function, instead of copying the entire array to the function, only the base address of the array is passed to the function
• The array parameter is thus a synonym of the array argument. Changing the elements of the array parameter is the same as changing the elements of the array arguments
![Page 18: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/18.jpg)
18
printArrayprintArray
static void printArray(int array[], int size){ int i;
for (i = 0; i < size; i++) { printf(“%d\n”, array[i]); }}
![Page 19: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/19.jpg)
19
inputArrayinputArraystatic int inputArray(int array[], int max, int sentinel){ int n, value; n = 0; while (TRUE) { printf(“?”); scanf(“%d”, &value); if (value == sentinel) break; if (n == max) {printf(“Error: array full”); exit(1); } array[n++] = value; } return n;}
![Page 20: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/20.jpg)
20
reverseArrayreverseArraystatic void reverseArray(int array[], int size){ int i;
for (i = 0; i < size / 2; i++) { /* swap(array[i], array[size – i –1]); */ swap(array, i, size – i –1); }}
![Page 21: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/21.jpg)
21
swapswap
static void swap(int array[], int p1, int p2){ int tmp;
tmp = array[p1]; array[p1] = array[p2]; array[p2] = tmp;}
![Page 22: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/22.jpg)
22
An ExampleAn Example
<<This program counts letter frequencies>>Peter Piker picked a peckOf pickled peppers.
A 1C 3D 2E 8F 1I 3…
![Page 23: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/23.jpg)
23
An ExampleAn Example
int nA, nB, nC, …, nZ;
int letterCounts[26];
int letterIndex(char ch){ if (isalpha(ch)) { return toupper(ch) – ‘A’; } else { return –1; }}
![Page 24: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/24.jpg)
24
An ExampleAn Example
void recordLetter(char ch, int letterCounts[]){ int index;
index = letterIndex(ch); if (index != -1) letterCounts[index]++;}
![Page 25: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/25.jpg)
25
An ExampleAn Example
void clearIntArray(int array[], int n){ int i;
for (i = 0; i < n; i++) { array[i] = 0; }}
![Page 26: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/26.jpg)
26
An ExampleAn Example
void displayLetterCounts(int letterCounts[]){ char ch; int num;
for (ch = ‘A’; ch <= ‘Z’; ch++) { num = letterCounts[letterIndex(ch)]; if (num != 0) printf(“%c %4d\n”, ch, num); }}
![Page 27: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/27.jpg)
27
Static Initialization of Static Initialization of ArraysArrays
int digits[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};int digits[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
string bigCities[] = { “New York”, “Los Angeles”, “Chicago”, “Houston”, “Philadelphia”, “San Diego”, “Detroit”, “Dallas”, }
int nBigCities = sizeof bigCities / sizeof bigCities[0];
![Page 28: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/28.jpg)
28
Scalar-Type Array IndexScalar-Type Array Index
string booleanName[2] = {“FALSE”, “TRUE”};
typedef enum {FALSE, TRUE} bool;
printf(“flag = %s\n”, booleanName[flag]);
![Page 29: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/29.jpg)
29
Multidimensional Multidimensional ArraysArrays
• Arrays of arrays are called multidimensional arrays
char board[3][3];
board[0][0] board[0][1] board[0][2]
board[1][0] board[1][1] board[1][2]
board[2][0] board[2][1] board[2][2]
![Page 30: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/30.jpg)
30
Multidimensional Multidimensional ArraysArrays
board[0][0]board[0][1]board[0][2]board[1][0]board[1][1]board[1][2]board[2][0]board[2][1]board[2][2]
board[0]
board[1]
board[2]
![Page 31: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/31.jpg)
31
Passing Passing Multidimensional Multidimensional
ArraysArraysvoid displayBorad(char board[3][3]) { int row, column; for (row = 0; row < 3; row++) { if (row != 0) printf(“---+---+---\n”); for (column != 0; column < 3; column++) { if (column != 0) printf(“|”); printf(“ %c “, borad[row][column]); } printf(“\n”); }}
![Page 32: Chapter Eleven Arrays](https://reader036.fdocuments.in/reader036/viewer/2022062423/568143a6550346895db02b5a/html5/thumbnails/32.jpg)
32
Initializing Initializing Multidimensional Multidimensional
ArraysArrays
double identityMatrix[3][3] = { {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};