Function as
Array Parameter
Liang, Introduction to C++
Programming, (c) 2007 Pearson
1
Passing Size along with Array
Normally when you pass an array to a function, you
should also pass its size in another argument. So the
function knows how many elements are in the array.
Otherwise, you will have to hard code this into the
function or declare it in a global variable. Neither is flexible
or robust.
2
Pass-by-Reference
Passing an array variable means that the starting
address of the array is passed to the formal
parameter. The parameter inside the function
references to the same array that is passed to the
function. No new arrays are created. This is pass-byreference.
PassByReferenceDemo
Run
3
const Parameters
Passing arrays by reference makes sense for performance
reasons.
If an array is passed by value, all its elements must be
copied into a new array. For large arrays, this could take
more time and space.
But passing arrays by reference could lead to errors if
your function changes the array accidentally.
To prevent it from happening, you can put the const
keyword before the array parameter to tell the compiler that
the array cannot be changed. The compiler will report
errors if the code in the function attempts to modify the
array.
Compile error
ConstArrayDemo 4
Modifying Arrays in Functions
Can you return an array from a function using a similar
syntax? For example, you may attempt to declare a
function that returns a new array that is a reversal of an
array as follows:
// Return the reversal of list
int[] reverse(const int list[], int size)
This is not allowed in C++.
5
Modifying Arrays in Functions,
cont.
However, you can circumvent this restriction by passing
two array arguments in the function, as follows:
// newList is the reversal of list
void reverse(const int list[], list newList[], int size)
list
newList
ReverseArray
Run
6
animation
Trace the reverse Function
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
}
list
newList
1
2
3
4
5
6
0
0
0
0
0
0
7
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
i = 0 and j = 5
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
}
list
newList
1
2
3
4
5
6
0
0
0
0
0
0
8
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
i (= 0) is less than 6
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
}
list
newList
1
2
3
4
5
6
0
0
0
0
0
0
9
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
i = 0 and j = 5
}
Assign list[0] to result[5]
}
list
newList
1
2
3
4
5
6
0
0
0
0
0
1
10
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
After this, i becomes 1 and j
}
becomes 4
}
list
newList
1
2
3
4
5
6
0
0
0
0
0
1
11
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
i (=1) is less than 6
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
}
list
newList
1
2
3
4
5
6
0
0
0
0
0
1
12
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
}
list
newList
1
2
3
4
5
6
0
0
0
0
2
1
i = 1 and j = 4
Assign list[1] to result[4]
13
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
After this, i becomes 2 and j
}
becomes 3
}
list
newList
1
2
3
4
5
6
0
0
0
0
2
1
14
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
i (=2) is still less than 6
newList[j] = list[i];
}
}
list
newList
1
2
3
4
5
6
0
0
0
0
2
1
15
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
i = 2 and j = 3
Assign list[i] to result[j]
}
list
newList
1
2
3
4
5
6
0
0
0
3
2
1
16
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
After this, i becomes 3 and j
becomes 2
}
}
list
newList
1
2
3
4
5
6
0
0
0
3
2
1
17
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
i (=3) is still less than 6
}
list
newList
1
2
3
4
5
6
0
0
0
3
2
1
18
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
i = 3 and j = 2
Assign list[i] to result[j]
}
list
newList
1
2
3
4
5
6
0
0
4
3
2
1
19
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
After this, i becomes 4 and j
becomes 1
}
list
1
2
3
4
5
6
newList
0
0
4
3
2
1
20
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
i (=4) is still less than 6
}
list
1
2
3
4
5
6
newList
0
0
4
3
2
1
21
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
i = 4 and j = 1
newList[j] = list[i];
Assign list[i] to result[j]
}
}
list
newList
1
2
3
4
5
6
0
5
4
3
2
1
22
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
After this, i becomes 5 and j
becomes 0
}
list
newList
1
2
3
4
5
6
0
5
4
3
2
1
23
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
i (=5) is still less than 6
}
list
newList
1
2
3
4
5
6
0
5
4
3
2
1
24
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
i = 5 and j = 0
}
Assign list[i] to result[j]
}
list
newList
1
2
3
4
5
6
6
5
4
3
2
1
25
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
After this, i becomes 6 and j
}
becomes -1
}
list
newList
1
2
3
4
5
6
6
5
4
3
2
1
26
animation
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
i (=6) < 6 is false. So exit
the loop.
}
}
list
1
2
3
4
5
6
newList
6
5
4
3
2
1
27
Searching Arrays
Searching is the process of looking for a specific
element in an array
E.g., discovering whether a certain score is
included in a list of scores.
There are many algorithms and data structures
devoted to searching.
i
n
tl
i
n
e
a
r
S
e
a
r
c
h
(
i
n
tl
i
s
t
[
]
,i
n
tk
e
y
,i
n
ta
r
r
a
y
S
i
z
e
)
{
f
o
r(
i
n
ti=0
;i<a
r
r
a
y
S
i
z
e
;i
+
+
)
{
i
f(
k
e
y=
=l
i
s
t
[
i
]
)
r
e
t
u
r
ni
;
[
0
][
1
][
2
]…
}
l
is
t
r
e
t
u
r
n1
;
}
k
e
yC
o
m
p
a
r
e
k
e
y
w
ith
l
is
t
[
i]f
o
ri=
0
,1
,…
28
Linear Search
The linear search approach compares the key
element, key, sequentially with each element in the
array list.
The method continues to do so until the key matches
an element in the list, or the list is exhausted without a
match being found.
If a match is made, the linear search returns the
index of the element in the array that matches the key.
If no match is found, the search returns -1.
29
animation
Linear Search Animation
Key
List
3
6
4
1
9
7
3
2
8
3
6
4
1
9
7
3
2
8
3
6
4
1
9
7
3
2
8
3
6
4
1
9
7
3
2
8
3
6
4
1
9
7
3
2
8
3
6
4
1
9
7
3
2
8
30
From Idea to Solution
i
n
tl
i
n
e
a
r
S
e
a
r
c
h
(
i
n
tl
i
s
t
[
]
,i
n
tk
e
y
,i
n
ta
r
r
a
y
S
i
z
e
)
{
f
o
r(
i
n
ti=0
;i<a
r
r
a
y
S
i
z
e
;i
+
+
)
{
i
f(
k
e
y=
=l
i
s
t
[
i
]
)
r
e
t
u
r
ni
;
[
0
][
1
][
2
]…
}
l
is
t
r
e
t
u
r
n1
;
}
k
e
yC
o
m
p
a
r
e
k
e
y
w
ith
l
is
t
[
i]f
o
ri=
0
,1
,…
Trace the function
int[]
int i
int j
int k
list = {1, 4, 4, 2, 5, -3, 6, 2};
= linearSearch(list, 4); // returns 1
= linearSearch(list, -4); // returns -1
= linearSearch(list, -3); // returns 5
31
Binary Search
For binary search to work, the elements in the
array must already be ordered. Without loss of
generality, assume that the array is in
ascending order.
e.g., 2 4 7 10 11 45 50 59 60 66 69 70 79
The binary search first compares the key with
the element in the middle of the array.
32
Binary Search (cont.)
Consider the following three cases:
If the key is less than the middle element,
you only need to search the key in the first
half of the array.
If the key is equal to the middle element,
the search ends with a match.
If the key is greater than the middle
element, you only need to search the key in
the second half of the array.
33
animation
Binary Search
Key
List
8
1
2
3
4
6
7
8
9
8
1
2
3
4
6
7
8
9
8
1
2
3
4
6
7
8
9
34
Binary Search (cont.)
k
eyis1
1
lo
w
m
id
k
ey<5
0
[0
] [1
] [2
] [3
] [4
] [5
] [6
] [7
] [8
] [9
][1
0
][1
1
][1
2
]
h
ig
h
list 2 4 7 1
01
14
5 5
05
96
06
66
97
07
9
lo
w
k
ey>7
m
id
h
ig
h
[0
] [1
] [2
] [3
] [4
] [5
]
list 2 4 7 1
01
14
5
id h
ig
h
lo
w m
[3
] [4
] [5
]
k
ey=
=1
1
list
1
01
14
5
35
key is 54
low
mid cont.
Binary
Search,
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
key >50
list
2 4 7 10 11 45 50 59 60 66 69 70 79
low
key <66
high
mid
high
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list
59 60 66 69 70 79
lowmid high
[7] [8]
key < 59
list
59 60
low high
[6] [7] [8]
59 60
36
Binary Search (cont.)
The binarySearch method returns the index of the
search key if it is contained in the list. Otherwise,
it returns –insertion point - 1. The insertion point is
the point at which the key would be inserted into
the list.
37
From Idea to Soluton
int binarySearch(int list[], int key, int arraySize)
{
int low = 0;
int high = arraySize - 1;
while (high >= low)
{
int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}
return –low - 1;
}
38
Sorting Arrays
Sorting, like searching, is also a common task in
computer programming. It would be used, for
instance, if you wanted to display the grades from
Listing 6.2, AssignGrade.cpp, in alphabetical order.
Many different algorithms have been developed for
sorting. This section introduces two simple, intuitive
sorting algorithms: selection sort and insertion sort.
39
swap
Selection Sort
2
9
5
4
8
1
6
8
1
9
The number 9 now is in the
correct position and thus no
longer need to be considered.
1
8
9
The number 8 now is in the
correct position and thus no
longer need to be considered.
6
8
9
The number 6 now is in the
correct position and thus no
longer need to be considered.
Select 9 (the largest) and swap it
with 6 (the last) in the list
Selection sort finds
the largest number
in the list and places
it last. It then finds
the largest number
remaining and
places it next to last,
and so on until the
list contains only a
single number.
Figure 6.4 shows
how to sort the list
{2, 9, 5, 4, 8, 1, 6}
using selection sort.
swap
2
6
5
Select 8 (the largest) and swap it
with 1 (the last) in the rem aining
list
4
swap
2
6
5
Select 6 (the largest) and swap it
with 1 (the last) in the rem aining
list
4
swap
2
1
5
4
Select 5 (the largest) and swap it
with 4 (the last) in the rem aining
list
2
4 is the largest and last in the list.
N o swap is necessary
1
4
5
6
8
9
The number 5 now is in the
correct position and thus no
longer need to be considered.
swap
2
1
4
5
6
8
9
The number 4 now is in the
correct position and thus no
longer need to be considered.
1
2
4
5
6
8
9
The number 2 now is in the
correct position and thus no
longer need to be considered.
Select 2 (the largest) and swap it
with 1 (the last) in the rem aining
list
Since there is only one number in
the rem aining list, sort is
com pleted
40
animation
Selection Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
2
2
2
1
9
6
1
2
5
5
4
4
4
4
5
5
8
1
6
6
1
8
8
8
6
2
6
5
4
8
1
9
2
1
5
4
6
8
9
2
1
4
5
6
8
9
9
9
9
41
From Idea to Solution
for (int i = listSize - 1; i >= 1; i--)
{
select the largest element in list[0..i];
swap the largest with list[i], if necessary;
// list[i] is in its correct position.
// The next iteration apply on list[0..i-1]
}
list[0] list[1] list[2] list[3] ...
list[0] list[1] list[2] list[3] ...
list[0] list[1] list[2] list[3] ...
list[0] list[1] list[2] list[3] ...
list[10]
list[9]
list[8]
list[7]
...
list[0]
42
for (int i = listSize - 1; i >= 1; i--)
{
select the largest element in list[0..i];
swap the largest with list[i], if necessary;
// list[i] is in its correct position.
// The next iteration apply on list[0..i-1]
}
Expand
// Find the maximum in the list[0..i]
double currentMax = list[0];
int currentMaxIndex = 0;
for (int j = 1; j <= i; j++)
{
if (currentMax < list[j])
{
currentMax = list[j];
currentMaxIndex = j;
}
}
43
for (int i = list.length - 1; i >= 1; i--)
{
select the largest element in list[0..i];
swap the largest with list[i], if necessary;
// list[i] is in place. The next iteration applies on list[0..i-1]
}
Expand
// Find the maximum in the list[0..i]
double currentMax = list[0];
int currentMaxIndex = 0;
for (int j = 1; j <= i; j++)
{
if (currentMax < list[j])
{
currentMax = list[j];
currentMaxIndex = j;
}
}
44
for (int i = list.length - 1; i >= 1; i--)
{
select the largest element in list[0..i];
swap the largest with list[i], if necessary;
// list[i] is in place. The next iteration applies on list[0..i-1]
}
Expand
// Swap list[i] with list[currentMaxIndex] if necessary;
if (currentMaxIndex != i)
{
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
45
Optional
Insertion Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
The insertion sort
algorithm sorts a list
of values by
repeatedly inserting
an unsorted element
into a sorted sublist
until the whole list
is sorted.
Step 1: Initially, the sorted sublist contains the
first element in the list. Insert 9 to the sublist.
2
9
5
4
8
1
6
Step2: The sorted sublist is {2, 9}. Insert 5 to the
sublist.
2
9
5
4
8
1
6
Step 3: The sorted sublist is {2, 5, 9}. Insert 4 to
the sublist.
2
5
9
4
8
1
6
Step 4: The sorted sublist is {2, 4, 5, 9}. Insert 8
to the sublist.
2
4
5
9
8
1
6
Step 5: The sorted sublist is {2, 4, 5, 8, 9}. Insert
1 to the sublist.
2
4
5
8
9
1
6
Step 6: The sorted sublist is {1, 2, 4, 5, 8, 9}.
Insert 6 to the sublist.
1
2
4
5
8
9
6
Step 7: The entire list is nowsorted
1
2
4
5
6
8
9
46
animation
Insertion Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
2
2
2
1
9
5
4
2
5
9
5
4
4
4
8
5
8
8
9
6
1
1
1
8
6
2
9
5
4
8
1
6
2
4
5
9
8
1
6
1
2
4
5
8
9
6
6
6
9
47
Optional
The insertion sort
algorithm sorts a list
of values by
repeatedly inserting
an unsorted element
into a sorted sublist
until the whole list
is sorted.
How to Insert?
[0
] [1
] [2
] [3
] [4
] [5
] [6
]
lis
t 2 59 4
S
te
p1
:S
a
v
e4toate
m
p
o
ra
ryv
a
ria
b
lec
u
rre
n
tE
le
m
e
n
t
[0
] [1
] [2
] [3
] [4
] [5
] [6
]
lis
t 2 5
9
S
te
p2
:M
o
v
elis
t[2
]tolis
t[3
]
[0
] [1
] [2
] [3
] [4
] [5
] [6
]
lis
t 2
59
S
te
p3
:M
o
v
elis
t[1
]tolis
t[2
]
[0
] [1
] [2
] [3
] [4
] [5
] [6
]
lis
t 2 4 59
S
te
p4
:A
s
s
ig
nc
u
rre
n
tE
le
m
e
n
ttolis
t[1
]
48
© Copyright 2026 Paperzz