Containers - Arrays: CSIS1117 Computer Programming
Containers - Arrays: CSIS1117 Computer Programming
Contents
Arrays
Initialization
Out of bound problem
Pass index variables
Call-by-value
Call-by-reference
Multi-dimensional array
Command-line arguments
c1117 lecture 8
c1117 lecture 8
int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
cin >> x1 >> x2 >> x3 >> x4 >> x5
>> x6 >> x7 >> x8 >> x9 >> x10;
double mean = (x1 + x2 + x3 + x4 + x5 +
x6 + x7 + x8 + x9 + x10) / 10.0;
cout << "Mean: " << mean << endl;
...
// calculate the standard deviation
Containers
c1117 lecture 8
// storing 10 int
// storing 31 char
c1117 lecture 8
x[0]
c1117 lecture 8
4
x2
1
x3
9
9
x4
0 -3 -8 6
0
x5
3 -2
x[9]
Independent
variables
An array of
elements
Array initialization
c1117 lecture 8
Array Initialization
char choice[5] = {'A', 'B', 'C', 'D', 'E'};
cout << choice << endl;
c1117 lecture 8
c1117 lecture 8
10
Example
string names[] = {"Peter", "Grace"};
int N = sizeof(names) / sizeof(names[0]);
for(int i = 0; i < N; ++i){ ... }
c1117 lecture 8
11
Example
c1117 lecture 8
12
3, 5, 7, 11};
i < 6; ++i)
<< endl;
c1117 lecture 8
13
c1117 lecture 8
14
Example
c1117 lecture 8
15
Example
Take the 10 numbers from input and store them in an array
8
a[0]
3
a[9]
3
a[9]
3
a[9]
16
Example
Similarly, find the 2nd smallest element by
scanning the array once
0
a[0]
3
a[9]
a[0]
3
a[9]
c1117 lecture 8
17
Example
Until the largest element is put at the end of the array
0 1 2 3 4 5 6 7 8 9
a[0]
a[9]
The elements in the array are sorted, and we only
need to print them out one by one from the array.
c1117 lecture 8
18
Example
index uses to store the index of the i-th smallest
element in the i-th iteration.
for(int i = 0; i < 10; ++i){
int index = i;
for(int j = i + 1; j < 10; ++j)
if(a[index] > a[j])
Scan the array from the
index = j;
int temp = a[index]; (i+1)-th elements to the last
one in the i-th iteration and
a[index] = a[i];
a[i] = temp;
update the variable index if
}
a[index] is the smallest
value among them.
Swap the i-th smallest element
with the element in i-th entry
c1117 lecture 8
19
Example
8 4
a[0]
3
a[9]
3
a[9]
swap(a[0], a[6])
0 4
a[0]
c1117 lecture 8
Exchange the
element in
position 0 with
the element in
position 6.
20
Example
10 20
10 20
21
Call-by-value
c1117 lecture 8
address
1004
1008
...
10
20
1300
1304
1308
Main memory
22
Memory
address
1004
1008
...
1300
1304
10
20
10
20
1308
Main memory
23
Memory
address
1004
1008
...
1300
1304
10
20
20
10
10
1308
temp
Main memory
24
Call-by-reference
c1117 lecture 8
25
Memory
address
1004
10
x, p
1008
20
y, q
1012
1016
swap(x, y);
1020
Main memory
26
Example
10 20
20 10
c1117 lecture 8
27
c1117 lecture 8
28
29
Memory address
10
1004
3
1008
int main(){
4
1012
int a[10];
8
1016
for(int i = 0; i < 10; ++i)
21
cin >> a[i];
-1
1020
sorting(a, 10);
0
return 0;
9
}
-32
2
10 3 4 8 21 1 0 9 32 2
c1117 lecture 8
Main memory
30
Memory address
10
1004
void sorting(int x[],int size){
3
1008
for(int i=0; i<size; ++i){
4
int index = i;
1012
8
for(int j=i+1; j<size; ++j)
1016
21
if(x[index] > x[j])
-1
index = j;
1020
swap(x[index], x[i]);
0
}
9
print_array(a);
-32
}
2
10
Main memory
a, x
size
31
Memory address
10 -32
1004
void sorting(int x[],int size){
3
1008
for(int i=0; i<size; ++i){
4
int index = i;
1012
8
for(int j=i+1; j<size; ++j)
1016
21
if(x[index] > x[j])
-1
index = j;
1020
swap(x[index], x[i]);
0
}
9
print_array(a);
-32 10
}
2
10
Main memory
a, x
size
32
Multi-dimensional arrays
c1117 lecture 8
33
mytable
Column
Row
Mytable[1][4]
Mytable[2][7]
c1117 lecture 8
34
c1117 lecture 8
35
Command-line arguments
c1117 lecture 8
36
c1117 lecture 8
37
c1117 lecture 8
38