CIS 22B - Notes for Tuesday, 10/6

  Announcements and Reminders

Recording

2 Dimensional Arrays

  • What is a 2-dimensional array?
You can think of it as an array of one-dimensional arrays
  • How is a 2D array declared?  initialized?
  • How is it stored?  How is it represented in memory?
Example 2-1 - How is a 2D array stored in memory?
  • How to ...
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 2-2 - 2D Arrays and Functions

Example 2-4 - Read data from a file into a 2 dimensional array

Input file

Date    Open    High    Low    Close*    Adj Close**    Volume
Jul 13, 2020    389.06    399.82    381.03    381.91    381.91    47,820,100
Jul 10, 2020    381.34    383.92    378.82    383.68    383.68    22,564,300
Jul 09, 2020    385.05    385.27    378.69    383.01    383.01    31,410,700
Jul 08, 2020    376.72    381.50    376.36    381.37    381.37    29,273,000
Jul 07, 2020    375.41    378.62    372.23    372.69    372.69    28,106,100
Jul 06, 2020    370.00    375.78    369.87    373.85    373.85    29,663,900
Jul 02, 2020    367.85    370.47    363.64    364.11    364.11    28,510,400
...

Input data stored in a 2D int array

  20200713     38906     39982     38103     38191     38191  47820100
  20200710     38134     38392     37882     38368     38368  22564300
  20200709     38505     38527     37869     38301     38301  31410700
  20200708     37672     38150     37636     38137     38137  29273000
  20200707     37541     37862     37223     37269     37269  28106100
  20200706     37000     37578     36987     37385     37385  29663900
  20200702     36785     37047     36364     36411     36411  28510400
    ...


Example 2-5 - Sort a 2-dimensional array by a column

 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
Example 2-6 - An Easy 3D Example

Example 2-7 - A 3-D Example with some 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)

 Lab Exercise #3

 Put your name, the compiler and operating system 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.  The initialize function should assign a random number between 0 and 255 to each element of the array.  Note:  your random numbers may differ from those shown below.  Hint:  you'll next the hex manipulator for the second half of the output.

const int Cols = 10;     // Global const

// Put your function prototypes here //

int main()
{
    const int Rows = 6;

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

// Put your function definitions here //

 *****  OUTPUT  *****

   41   35  190  132  225  108  214  174   82  144
   73  241  241  187  233  235  179  166  219   60
  135   12   62  153   36   94   13   28    6  183
   71  222  179   18   77  200   67  187  139  166
   31    3   90  125    9   56   37   31   93  212
  203  252  150  245   69   59   19   13  137   10

   29   23   be   84   e1   6c   d6   ae   52   90
   49   f1   f1   bb   e9   eb   b3   a6   db   3c
   87    c   3e   99   24   5e    d   1c    6   b7
   47   de   b3   12   4d   c8   43   bb   8b   a6
   1f    3   5a   7d    9   38   25   1f   5d   d4
   cb   fc   96   f5   45   3b   13    d   89    a