You are on page 1of 16

Lecture 9

Array (Part 2)

Learning Outcomes:
 To know how to use array as function
parameter

1
Arrays and Functions
 Using function to process arrays in large program

1) Passing a one-dimensional array to function

 By passing individual elements


 Same as passing any ordinary variable to a
function

 By passing the whole array


 When we need the function to operate on the whole
array

2
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

3
Passing individual element - Example 1

4
Passing an individual element - Example 2
#include <iostream>
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“;
} // end print
5
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

6
 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 doesn’t 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
7
Passing the whole arrays for updating - Example 1

•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
8
return a value of the type array
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";
} // end reverseOrder 9
Passing the whole array as constants : Example

10
2 ) Passing a 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
 Pass the whole row by indexing the array name with only the
row number

 Passing the whole array


 Use the array name as the actual parameter

11
2 – D 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“;
}
12
2 – D Array : Passing a row - Example

13
2 – D Array : Passing the whole array – Example 1

14
2 – D Array : Passing the whole array - Example 2
#define NUMBER_OF_STUDENTS 5 // const int NUMBER_OF_STUDENTS=5;
#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

} // end main
15
cont. eg. 2 – D 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
} // end checkAnswer

- END - 16

You might also like