6
CS111 Lab Arrays, continued Instructor: Michael Gordon

Arrays, continued

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Arrays, continued

CS111 Lab Arrays, continued

Instructor: Michael Gordon

Page 2: Arrays, continued

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.

Page 3: Arrays, continued

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)

Page 4: Arrays, continued

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)

Page 5: Arrays, continued

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]

Page 6: Arrays, continued

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