Upload
michael-gordon
View
496
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
CS111 Lab Arrays, continued
Instructor: Michael Gordon
Arrays in Functions
To write a function title with an array as a
parameter, use the array declaration
without the size.
Example: int func(int a[])
To call a function with an array as an
argument, just use the array name.
Example: int x = func(grades)
Where int grades[] was previously declared.
Functions, continued
Arrays are, by default, pass by reference.
If your function needs to perform actions
on the entire array, you will need to
specify the array size as a parameter.
Example: int sum(int arr[], int size)
Two Dimensional Arrays
A 2D array can be thought of in two ways:
A grid/table of rows and columns of data
An array of arrays
You can declare it as follows:
e.g: int arrayName[5][4];
Function call: func(arrayName)
Function title example:
func(int a[][4], int rows)
Visualization
The grid below shows the addresses/indexes of each cell of an array c[5][4];
c[0][0] c[0][1] c[0][2] c[0][3]
c[1][0] c[1][1] c[1][2] c[1][3]
c[2][0] c[2][1] c[2][2] c[2][3]
c[3][0] c[3][1] c[3][2] c[3][3]
c[4][0] c[4][1] c[4][2] c[4][3]
Visualization, pop quiz
What’s the value in c[0][0]?
What’s the value in c[2][2]?
What’s the value in c[3][4]?
90 100 85 99
40 50 30 45
77 68 81 65
85 89 91 93
70 65 66 71