CIS 22B - Notes for Tuesday, 1/23

  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

 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 = 8;   // global const

// Put your function prototypes here

int main()
{
    const int Rows = 8;

    int a[Rows][Cols];
    initialize(a,Rows,Cols);
    print(a,Rows,Cols);
    printInHex(a,Rows,Cols);
}

 *****  OUTPUT  *****

     4     8    12    16    20    24    28    32
    36    40    44    48    52    56    60    64
    68    72    76    80    84    88    92    96
   100   104   108   112   116   120   124   128
   132   136   140   144   148   152   156   160
   164   168   172   176   180   184   188   192
   196   200   204   208   212   216   220   224
   228   232   236   240   244   248   252   256

     4     8     c    10    14    18    1c    20
    24    28    2c    30    34    38    3c    40
    44    48    4c    50    54    58    5c    60
    64    68    6c    70    74    78    7c    80
    84    88    8c    90    94    98    9c    a0
    a4    a8    ac    b0    b4    b8    bc    c0
    c4    c8    cc    d0    d4    d8    dc    e0
    e4    e8    ec    f0    f4    f8    fc   100