Download presentation
Presentation is loading. Please wait.
1
Working With Arrays
2
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } [0] [1] [2] [3] [4]
3
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=0 [0] [1] [2] [3] [4]
4
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=0 [0] [1] [2] [3] [4] 12
5
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=1 [0] [1] [2] [3] [4] 12
6
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=1 [0] [1] [2] [3] [4] 12 15
7
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=2 [0] [1] [2] [3] [4] 12 15
8
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=2 [0] [1] [2] [3] [4] 12 15 31
9
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=3 [0] [1] [2] [3] [4] 12 15 31
10
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=3 [0] [1] [2] [3] [4] 12 15 31 18
11
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=4 [0] [1] [2] [3] [4] 12 15 31 18
12
Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=4 [0] [1] [2] [3] [4] 12 15 31 18 14
13
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=0, sum=0 [0] [1] [2] [3] [4] 12 15 31 18 14
14
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=0, sum=12 [0] [1] [2] [3] [4] 12 15 31 18 14
15
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=1, sum=12 [0] [1] [2] [3] [4] 12 15 31 18 14
16
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=1, sum=27 [0] [1] [2] [3] [4] 12 15 31 18 14
17
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=2, sum=27 [0] [1] [2] [3] [4] 12 15 31 18 14
18
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=2, sum=58 [0] [1] [2] [3] [4] 12 15 31 18 14
19
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=3, sum=58 [0] [1] [2] [3] [4] 12 15 31 18 14
20
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=3, sum=76 [0] [1] [2] [3] [4] 12 15 31 18 14
21
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=4, sum=76 [0] [1] [2] [3] [4] 12 15 31 18 14
22
Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=4, sum=90 [0] [1] [2] [3] [4] 12 15 31 18 14
23
Traversals i=4, sum=90 average=18
... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=4, sum=90 average=18 [0] [1] [2] [3] [4] 12 15 31 18 14
24
Traversals max=12 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; [0] [1] [2] [3] [4] 12 15 31 18 14
25
Traversals max=12 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=1 [0] [1] [2] [3] [4] 12 15 31 18 14
26
Traversals max=12 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=1 [0] [1] [2] [3] [4] 12 15 31 18 14
27
Traversals max=15 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=1 [0] [1] [2] [3] [4] 12 15 31 18 14
28
Traversals max=15 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=2 [0] [1] [2] [3] [4] 12 15 31 18 14
29
Traversals max=15 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=2 [0] [1] [2] [3] [4] 12 15 31 18 14
30
Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=2 [0] [1] [2] [3] [4] 12 15 31 18 14
31
Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=3 [0] [1] [2] [3] [4] 12 15 31 18 14
32
Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=3 [0] [1] [2] [3] [4] 12 15 31 18 14
33
Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=4 [0] [1] [2] [3] [4] 12 15 31 18 14
34
Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=4 [0] [1] [2] [3] [4] 12 15 31 18 14
35
Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; [0] [1] [2] [3] [4] 12 15 31 18 14
36
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 15 31 18 14
37
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 31 18 14
38
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 31 18 14 j j+1
39
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 31 18 14
40
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 31 18 14 j j+1
41
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 15 31 18 14
42
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 15 31 18 14 j j+1
43
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=31 [0] [1] [2] [3] [4] 12 15 31 18 14 j j+1
44
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=31 [0] [1] [2] [3] [4] 12 15 18 18 14 j j+1
45
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=31 [0] [1] [2] [3] [4] 12 15 18 31 14 j j+1
46
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 [0] [1] [2] [3] [4] 12 15 18 31 14
47
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 [0] [1] [2] [3] [4] 12 15 18 31 14 j j+1
48
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 swap=31 [0] [1] [2] [3] [4] 12 15 18 31 14 j j+1
49
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 swap=31 [0] [1] [2] [3] [4] 12 15 18 14 14 j j+1
50
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 swap=31 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1
51
Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=4 [0] [1] [2] [3] [4] 12 15 18 14 31
52
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 15 18 14 31
53
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 18 14 31
54
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1
55
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 18 14 31
56
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1
57
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 15 18 14 31
58
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1
59
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=18 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1
60
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=18 [0] [1] [2] [3] [4] 12 15 14 14 31 j j+1
61
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=18 [0] [1] [2] [3] [4] 12 15 14 18 31 j j+1
62
Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 [0] [1] [2] [3] [4] 12 15 14 18 31
63
Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 15 14 18 31
64
Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 14 18 31
65
Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 14 18 31 j j+1
66
Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 14 18 31
67
Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 14 18 31 j j+1
68
Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 swap=15 [0] [1] [2] [3] [4] 12 15 14 18 31 j j+1
69
Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 swap=15 [0] [1] [2] [3] [4] 12 14 14 18 31 j j+1
70
Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 swap=15 [0] [1] [2] [3] [4] 12 14 15 18 31 j j+1
71
Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 14 15 18 31
72
Traversals i=4 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 14 15 18 31
73
Traversals i=4 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 14 15 18 31 j j+1
74
Traversals i=4 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 14 15 18 31
75
Traversals i=5 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 14 15 18 31
76
Traversals i=5 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 14 15 18 31
77
Arrays as Parameters void print_array(const float the_array[], const int size) { for(int i = 0; i < size; i++) cout << the_array[i] << " "; return; } int main() const int SIZE=100; float my_array[SIZE]; ... print_array(my_array, SIZE);
78
Incorrect Argument Syntax
void print_array(const float the_array[], const int size) { for(int i = 0; i < size; i++) cout << the_array[i] << " "; return; } int main() const int SIZE=100; float my_array[SIZE]; ... print_array(my_array[], SIZE);
79
Preconditions for Arrays as Parameters
// Pre: parameter size is a positive integer // and the_array[0] -> the_array[size-1] is data. void print_array(const float the_array[], const int size) { for(int i = 0; i < size; i++) cout << the_array[i] << " "; return; }
80
Pass by Reference void bubble_sort(float the_array[], const int size) { for(short i=1; i<size; i++) for(short j=0; j<size-i; j++) if(the_array[j] > the_array[j+1]) swap(the_array[j],the_array[j+1]); int main() const int SIZE=100; float my_array[SIZE]; ... bubble_sort(my_array, SIZE);
81
Pass by Reference BEFORE
void shift_right(int the_array[], const int size) { for(int i=size – 1; i>0; i++) the_array[i]=the_array[i-1]; return; } int main() const int SIZE=100; float my_array[SIZE]; ... shift_right(my_array, SIZE); BEFORE [0] [1] [2] [3] [4] 12 14 15 18 31
82
Pass by Reference BEFORE AFTER
void shift_right(int the_array[], const int size) { for(int i=size – 1; i>0; i++) the_array[i]=the_array[i-1]; return; } int main() const int SIZE=100; float my_array[SIZE]; ... shift_right(my_array, SIZE); BEFORE [0] [1] [2] [3] [4] 12 14 15 18 31 AFTER [0] [1] [2] [3] [4] 12 12 14 15 18
83
Pass by Reference // The is_found() function will search the array passed for an instance of // the target and will return true if the target is found in the array; // false otherwise. // Pre: the_array[0] -> the_array[size] is valid data. // Post: true returned if target found in array; false otherwise. bool is_found (const char the_array[], const int size, const char target) { bool found = false; int i = 0; while(i < size && found == false) { if (target == the_array[i]) found = true; i++; } return found; }
84
Pass by Reference // The is_found() function will search the array passed for an instance of // the target and will return the first index position containing the target // value if the target is found in the array; -1 is returned otherwise. // Pre: the_array[0] -> the_array[size] is valid data. // Post: the first index position containing the target value is returned; if // the target is not found in the array, a -1 is returned. int is_found (const char the_array[], const int size, const char target) { int position_found = -1; int i = 0; while(i < size && position_found == -1) if (target == the_array[i]) position_found = i; i++; } return position_found;
85
End of Session
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.