Presentation is loading. Please wait.

Presentation is loading. Please wait.

J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.

Similar presentations


Presentation on theme: "J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110."— Presentation transcript:

1 J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110

2 J. Michael Moore When To Use Arrays Of Different _______________ Determined by the data – the number ____________________ ___________ determines the number ______________ to use. Examples: (1D array) Tracking grades for a class Each cell contains the grade for a student i.e., grades[i] There is _________________ that specifies which student’s grades are being accessed (2D array) Expanded grades program Again there is ____________ that specifies which student’s grades are being accessed The __________________ can be used to specify which lab assignment One dimension (which student)

3 J. Michael Moore When To Use Arrays Of Different Dimensions (2D array continued) Student Lab grade First student Second student Third student … Lab1 Lab2 Lab3 Lab4 Lab5 : LabN

4 J. Michael Moore When To Use Arrays Of Different Dimensions (2D array continued) Notice that each row is merely a _________ A _________ is an array containing rows of _________ [1][2][3][4] [1] [2] [3] [4] [5] Columns Rows

5 J. Michael Moore When To Use Arrays Of Different Dimensions (3D array – take the ________ but allow for multiple ______). The _______________________ specifies which section grades are being tracked. Student (X = column) Lab Assignment (Y = row) Section (Z) Note: 1.The standard approach for specifying the dimensions is to specify the row coordinate (Y) and then the column coordinate (X). 2.The size of a _________ must be the same for all elements along that _______ e.g., all ____ must be of the same ____

6 J. Michael Moore When To Use Arrays Of Different Dimensions Lab6 Lab1 Lab2 Lab3 Lab7 Lab5 Lab4 Student 3…Student 2Student 1 Section 501 Section 502 Section 503

7 J. Michael Moore Format: (Two dimensional arrays) Name : array [min..max, min..max] of type; (Three dimensional arrays) Name : array [min..max, min..max, min..max] of type; Examples: var johnFinances : array [1..3, 1..7] of real; var cube : array [1..6, 1..6, 1..6] of char; Declaring Multi-Dimensional Arrays ____________

8 J. Michael Moore Declaring Multi-Dimensional Arrays As A Type Format: Type declaration Type name = array [min..max, min..max] of element type; name = array [min..max, min..max, min..max] of element type; Variable declaration array name : Type name;

9 J. Michael Moore Declaring Multi-Dimensional Arrays As A Type Example: Type declaration Finances = array [1..3, 1..7] of real; Cube = array [1..6, 1..6, 1..6] of char; Variable declaration var johnFinances : Finances; var aCube : Cube;

10 J. Michael Moore Accessing / Assigning Values To Elements Format: name [row][column] := value; Example: finances [1][1] := 4500; writeln (finances[1][1]); OR finances [1, 1] := 4500; writeln (finances[1, 1]);

11 J. Michael Moore Multi-Dimensional Arrays And Input/Output Arrays of ___________________ (including multi-dimensional _________________ arrays) cannot be passed as parameters to: read, readln, write, writeln. Only ______________________ can be passed as parameters to these procedures.

12 J. Michael Moore A Character-Based Grid program gridExample (input, output); const MAX_ROWS = 4; MAX_COLUMNS = 4; NUM_COMBINATIONS = 10; type Grid = array[1..MAX_ROWS, 1..MAX_COLUMNS] of char;

13 J. Michael Moore A Character-Based Grid (2) function generateElement (temp : integer) : char; var anElement : char; begin case (temp) of 1, 2, 3, 4, 5, 6 : anElement := ' '; 7, 8, 9: anElement := '*'; 10: anElement := '.';

14 J. Michael Moore A Character-Based Grid (3) else {still in case statement} begin writeln(' >'); writeln('<< Value should be 1-',NUM_COMBINATIONS, ' but random value is ', temp); anElement := '!'; end; generateElement := anElement; end;

15 J. Michael Moore A Character-Based Grid procedure initialize (var aGrid : Grid); var r : integer; c : integer; temp : integer; begin for r := 1 to MAX_ROWS do begin for c := 1 to MAX_COLUMNS do begin temp := random(NUM_COMBINATIONS) + 1; aGrid[r][c] := generateElement(temp); end;

16 J. Michael Moore A Character-Based Grid (5) procedure display (aGrid : Grid); var row: integer; column: integer; begin for row := 1 to MAX_ROWS do begin for column := 1 to MAX_COLUMNS do begin write(aGrid[row][column]); end; writeln; end;

17 J. Michael Moore A Character-Based Grid (6) procedure displayLines (aGrid : Grid); var row : integer; column : integer; begin for row := 1 to MAX_ROWS do begin writeln(' - - - -'); for column := 1 to MAX_COLUMNS do begin write('|', aGrid[row][column]); end; writeln('|'); end; writeln(' - - - -'); end;

18 J. Michael Moore A Character-Based Grid (7) begin var aGrid : Grid; initialize(aGrid); writeln('Displaying grid'); writeln('==============='); display(aGrid); writeln; writeln('Displaying grid with bounding lines'); writeln('=========================='); displayLines(aGrid); end.


Download ppt "J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110."

Similar presentations


Ads by Google