CIS 22B - Notes for Mon Oct 09

Announcements and Reminders

Topics

2 Dimensional Arrays

  • What is a 2 dimensional array?
You can think of it as an array of one-dimensional arrays
How is it stored?  How is it represented in memory?
  • How to ...
Declare a 2 dimensional array
Initialize a 2 dimensional array

Example

Pass the array to a function
Pass one element to a function
Pass one row to a function
Pass a column to a function

Example

Read data from a file into a 2 dimensional array

Example

Sort a 2 dimensional array by a column

Example

3 Dimensional Arrays

"You can think of a 3 dimensional array as an array of two dimensional arrays, or an array of an array of one dimensional arrays."
Declaration, initialization, usage

Examples
  1. Easy Example
  2. 3-D Example with Simple Calculations

Videos

Buckys C++ Programming Tutorials - 35 - Passing Arrays to Functions  (8 minutes)  This is a good one, if you're running behind

Buckys C++ Programming Tutorials - 36 - Multidimensional Arrays (6 minutes)  This is a good one.

C++ Tutorial 17 - Multidimensional Arrays as Parameters (21 minutes)

A 2D Array Example

Wandering Star

Read me

Lab Exercise #3

Put your name, the compiler used, and Lab Exercise #3 in a comment at the top of your exercise

Complete the program using the following main() and producing the indicated output.

const int Cols = 6;   // global const

// Put your function prototypes here

int main()
{
    const int Rows = 5;
    int a[Rows][Cols];
    initialize(a,Rows,Cols);
    print(a,Rows,Cols);
}

*****  OUTPUT  *****

 96 93 90 87 84 81
 78 75 72 69 66 63
 60 57 54 51 48 45
 42 39 36 33 30 27
 24 21 18 15 12  9