You are on page 1of 24

BITP 1113 Programming Technique

Lecture 9 Array (Part 2)

Learning Outcomes

At the end of this lecture, you should be able


to

Differentiate passing array by value and passing


array by reference
Pass individual elements from array to a function
Pass an entire array to a function

FTMK - UTeM - Semester 1


2011/2012

Passing array to function

We can use function to process arrays in large program


Pass by value

Pass by reference

The variable s value is passed to a formal parameter


Changing the value of the local parameter inside the function
does not effect the value of the variable outside the function
Refer to passing individual elements

Passing an array 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
Refer to passing the whole array

Passing the one-dimensional array to


function
By

passing individual elements

Same

By

passing the whole array

When

array

as passing any ordinary variable to a function

we need the function to operate on the whole

1-D array : Passing individual elements

Passing individual element


Array element must matches the function
parameter type

When as a value parameter, function cannot


change the value of the element in the calling
function

E.g : a function, print_square receives an


integer and prints its square, using an array,
we can loop through the array and pass each
element in turn to print_square

Passing individual element - Example


1

Passing an individual element #include <iostream> Example 2


using namespace std;
void print(int a); // function prototype
void main()
{
int i,j,ary1[4][3]={{1,2,3},{2,4,6},{3,6,9},{3,2,1}};
for(i = 0; i < 4; i++)
{
for(j = 0; j < 3; j++)
print( ary1[i][j] );
cout << endl;
} // end for
} // end main
void print(int a)
{
cout << a << \t;
} 7// end print

1-D array : Passing the whole array

Passing the whole array


When we use large arrays in functions, by
passing each value we need an extra memory
to do so

E.g : if an array containing 20k elements were


passed by value to a function, another 20k
elements would have to be allocated in the
function and each element would have to be
copied from one array to another

Instead of passing the whole array, C++


passes the address of the array

1-D array : Passing the whole array

An array name is the address of the first


element in the array
Because of the name of array is in fact its
address, passing an array name allows the
called function to refer to the array back in the
calling function
Two rules associated with passing the whole
array

The function must be called by passing only the


name of the array
In the function definition, the formal must be an
array type, the size of the array doesnt need to be
specified. If provided, it is ignored by the compiler.

A function can change the elements in array by


passing the array name without the constant
modifier

Passing the whole arrays for updating Example 1

10

Arrays are passed by


reference only
The symbol & is not used
when declaring an array as a
formal parameter
C++ does not allow
functions to return a value of

Passing the whole arrays for updating Example 2


#include <iostream>
using namespace std;
#define SIZE 7
void reverseOrder(int [], int); // function prototype
void main()
{
int numbers[SIZE]={1,2,3,4,5,6,7};
reverseOrder(numbers, SIZE);
} // end main
void reverseOrder(int list[], int arraySize)
{
for (int i = arraySize-1; i >= 0; i--)
cout << list[i] << "\t";
}11// end reverseOrder

Use of const array argument

Passing array by reference could lead to errors


if your function changed accidentally
Put const keyword before the array parameter
to tell the compiler that the array cant be
changed

12

FTMK - UTeM - Semester 1


2011/2012

Passing the whole array as constants : Examp

13

Passing the two-dimensional array to


function

Passing an individual element


- Pass the individual element by indexing the array
name with the row number and the column number

Passing a row

Passing the whole array

14

Pass the whole row by indexing the array name with


only the row number

Use the array name as the actual parameter

2D Array: Passing an individual element Example


#include <iostream>
using namespace std;
void print(int a);
void main()
{
int i,j,ary1[4][3]={{1,2,3},{2,4,6},{3,6,9},{3,2,1};
for(int i=0; i<4; i++){
for(int j=0; j<3; j++)
print(ary1[i][j]);
cout << endl;
}
}
void print(int a)
{
cout << a << \t;
}
15

2D Array : Passing a row - Example

16

2D Array : Passing the whole array


Example 1

17

2D Array : Passing the whole array #define


NUMBER_OF_STUDENTS
5 // const int NUMBER_OF_STUDENTS=5;
Example
2

#define NUMBER_OF_QUESTIONS 10 // const int NUMBER_OF_QUESTIONS=10;


void checkAnswer(char[], char[][NUMBER_OF_QUESTIONS], int, int, int[]); //
function prototype
void main()
{
char key[NUMBER_OF_QUESTIONS] =
{'C','C','B','A','D','B','A','B','C','D' };
char answer[NUMBER_OF_STUDENTS][NUMBER_OF_QUESTIONS]=
{
{'C','D','C','C','B','B','A','B','C','B'},
{'C','C','B','C','D','B','A','B','A','B'},
{'D','D','C','A','D','B','A','B','A','D'},
{'B','C','C','C','B','B','B','B','C','C'},
{'C','C','C','A','D','B','C','B','C','D'}
};
int checked[NUMBER_OF_STUDENTS];
checkAnswer(key, answer, NUMBER_OF_STUDENTS, NUMBER_OF_QUESTIONS,
checked); // passing array numbers
18

} // end main

cont. eg. 2D Array : Passing the whole array


void checkAnswer(char key[], char answer[][NUMBER_OF_QUESTIONS],
int numStud, int numQues, int checked[])
{
int i, j; // declaring index
for (i = 0; i < numStud; i++) // initializing checked (zeros)
checked [i] = 0;
for (i = 0; i < numStud; i++)
{
for (j = 0; j < numQues; j++)
{
if (answer[i][j] == key[j])
checked[i] += 1;
} // inner for
cout << "Student " << i << "'s marks : " << checked[i] << "/" <<
NUMBER_OF_QUESTIONS << "." << endl;
} // end for
} 19
// end checkAnswer

cont. eg. Passing the whole array

To pass an array to a function, just use the


array name:
showScores(tests);

To define a function that takes an array


parameter, use empty [] for array
argument:
void showScores(int
// function
void showScores(int
// function

[]);
prototype
tests[])
header

cont. eg. Passing the whole array

When passing an array to a function, it is


common to pass array size so that function
knows how many elements to process:
showScores(tests, ARRAY_SIZE);

Array size must also be reflected in prototype,


header:
void showScores(int [], int);
// function prototype
void showScores(int tests[], int size)
// function header
7-21

(Program Continues)

Program 7-14 (Continued)

Ask yourself

Can you describe the difference between


passing array by value and reference?
Do you know how to pass single values from
an array to a function and pass a whole array
to a function?
Do you know the purpose of using constant in
argument list of a function when passing an
array?

24

FTMK - UTeM - Semester 1


2011/2012