Arrays as Function Parameters
Outline
Passing an array argument (section 9.3)
Reading part of an array (section 9.4)
Searching and sorting arrays (section
9.5)
Finding the smallest value in an array
Array search
Sorting an array in ascending order
CSCE 106
2
Bubble Sort
#include <iostream>
using namespace std;
void exchange(float& a1, float& a2)
{
float temp = a1;
void exchange(float& a1, float& a2);
void main()
{
float x[] = {74.1, 45.2, 83.5, 16.6, 7.8};
a1 = a2;
a2 = temp;
}
for (int i=0; i<4; i++)
for (int j = i+1; j<5; j++)
if (x[i] < x[j])
exchange(x[i], x[j]);
for (j = 0; j<5; j++)
cout << “ ” << x[j]);
}
CSCE 106
3
Passing an Array Argument
Arrays are always passed by reference
Pass entire array to a function by writing
just its name (no subscripts or brackets) in
the argument list of the function call (actual
parameters)
In function definition and prototype, use
empty square brackets ([ ]) to identify array
Use keyword const to indicate that array
argument cannot be changed by function
CSCE 106
4
Example 1 – Comparing 2 Arrays
const int MAX_SIZE = 5;
float x[MAX_SIZE ];
float y[MAX_SIZE ];
...
if (sameArray(x, y, MAX_SIZE))
cout << “Arrays are identical.” << endl;
else
cout << “Arrays are different.” << endl;
CSCE 106
5
Listing 9.4
CSCE 106
Function sameArray
6
Example 2 – Adding 2 Arrays
const int MAX_SIZE = 5;
float x[MAX_SIZE ] = {1.8, 2.2, 3.4, 5.1, 6.7};
float y[MAX_SIZE ] = {2.0, 4.5, 1.3, 4.0, 5.5};
float z[MAX_SIZE];
...
addArray(MAX_SIZE, x, y, z);
CSCE 106
7
Listing 9.5
Function addArray
// File: addArray.cpp
// Stores the sum of a[i] and b[i] in c[i]
// Sums pairs of array elements with subscripts ranging from 0
//
to size – 1
// Pre: a[i] and b[i] are defined (0 <= i <= size-1)
// Post: c[i] = a[i] + b[i] (0 <= i <= size-1)
void addArray
(int size,
// IN: the size of the arrays
const float a[],
// IN: the first array
const float b[],
// IN: the second array
float c[])
// OUT: result array
{
// Add corresponding elements of a and b and store in c
for (int i = 0; i < size; i++)
c[i] = a[i] + b[i];
}
CSCE 106
8
Reading Part of an Array
Sometimes it is difficult to know how many
elements will be in an array
34 students in one section
28 students in another section
Always allocate enough space for largest
possible amount needed (e.g. 40 for
students in a section)
Remember to start reading with index [0]
Must keep track of how many elements
used
CSCE 106
9
Searching and Sorting Arrays
Two common array processing
problems
Searching
Sorting
E.g.
look for a particular score, highest score,
etc.
rearrange an array of scores in
increasing order
CSCE 106
10
Array Search – Function
Algorithm
1. For each array element
1.1 If the current element contains the target
1.2 Return the subscript of the current element
2. Return -1.
CSCE 106
11
Array Search Function Analysis
(Interface)
Input arguments
int items[ ]
int size
int target
// array to search
// number of items in array
// item to find
Output arguments
none
Returns
if found, subscript of first location in array
if not found, -1
CSCE 106
12
Listing 9.9
CSCE 106
The function linSearch
13
Finding the Smallest Value
1. Assume the first element is smallest so far
and save its subscript
2. For each array element after the first one
2.1 If the current element < the smallest so far
2.1.1 Save the subscript of current element
CSCE 106
14
Function findIndexOfMin
// Finds the subscript of the smallest value in a subarray.
int findIndexOfMin(float z[], int start, int end)
{
// local data
int minIndex,
// index of the smallest element
i;
// Assume the first element of subarray is the smallest
minIndex = start;
for (i = start + 1; i<=end; i++)
if (z[i] < z[minIndex])
minIndex = i;
// Returns the subscript of the smallest value in the subarray.
return minIndex;
}
CSCE 106
15
Sorting an Array in Ascending
Order
Many programs execute more efficiently
if data is in order before processing
starts
Possible to order in either ascending or
descending arrangement
Selection sort just one of many ways to
do this
CSCE 106
reuses previous components/functions of
search and swap
16
Selection Sort - Algorithm
1. Starting with the first item in the array
(subscript 0) and ending with the next-to-lastitem:
1.1 Set i equal to the subscript of the first item in
the subarray to be processed in the next steps
1.2 Find the subscript (minSub) of the smallest item
in the subarray with subscripts ranging from i
through n-1
1.3 Exchange the smallest item found in step 1.2
with item i
CSCE 106
17
Selection Sort Function Analysis
Input arguments
float items[ ]
int n
// array to sort
// number of items to sort
Output arguments
float items [ ]
// original array sorted
Local variables
int i
// subscript of first element
int minSub // subscript of smallest item
CSCE 106
18
Function selSort
void selSort(float y[], int n)
{
int minSub;
for (int i=0; i<n-1; i++)
{
// Find index of smallest item in unsorted section.
minSub = findIndexOfMin(y, i, n-1);
// Exchange items at position minSub and i.
exchange(y[minSub], y[i]);
}
}
CSCE 106
19
#include <iostream>
using namespace std;
void selSort(float y[], int n)
{
int minSub;
void exchange(float& a1, float& a2);
void selSort(float y[], int n);
int findIndexOfMin(float[], int, int);
void main()
{
const int size = 4;
float x[] = {74.1, 45.2, 83.5, 16.6};
selSort(x, size);
}
for (int i=0; i<n-1; i++)
{
minSub = findIndexOfMin(y, i, n-1);
exchange(y[minSub], y[i]);
}
}
int findIndexOfMin(float z[], int start, int end)
{
int minIndex,
int i;
void exchange(float& a1, float& a2)
{
float temp;
minIndex = start;
for (i = start + 1; i<=end; i++)
if (z[i] < z[minIndex])
minIndex = i;
temp = a1;
a1 = a2;
a2 = temp;
}
CSCE 106
return minIndex;
}
20
Next lecture we will
revise
CSCE 106
21
© Copyright 2026 Paperzz