You are on page 1of 24

# BITP 1113 Programming Technique

## Lecture 9 Array (Part 2)

Learning Outcomes

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

2011/2012

## 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

function
By

Same

By

When

array

## 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

1

## Passing an individual element #include <iostream> Example 2

using namespace std;
void print(int a); // function prototype
void main()
{
int i,j,ary1={{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

## 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

## 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

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

## 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

2011/2012

13

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

14

## Pass the whole row by indexing the array name with

only the row number

## 2D Array: Passing an individual element Example

#include <iostream>
using namespace std;
void print(int a);
void main()
{
int i,j,ary1={{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

16

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

## 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

## 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)

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

2011/2012