Multi-Dimensional ArraysWhen To Use Arrays Of Different _______________When To Use Arrays Of Different DimensionsSlide 4Slide 5Slide 6Declaring Multi-Dimensional ArraysDeclaring Multi-Dimensional Arrays As A TypeSlide 9Accessing / Assigning Values To ElementsMulti-Dimensional Arrays And Input/OutputA Character-Based GridA Character-Based Grid (2)A Character-Based Grid (3)Slide 15A Character-Based Grid (5)A Character-Based Grid (6)A Character-Based Grid (7)J. Michael MooreFrom James Tam’s materialMulti-Dimensional ArraysCSCE 110J. Michael MooreWhen 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 assignmentOne dimension (which student)J. Michael MooreWhen To Use Arrays Of Different Dimensions•(2D array continued)StudentLab grade First student Second student Third student …Lab1Lab2 Lab3 Lab4 Lab5 : LabNJ. Michael MooreWhen 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]ColumnsRowsJ. Michael MooreWhen 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 ____J. Michael MooreWhen To Use Arrays Of Different Dimensions Lab6 Lab1 Lab2 Lab3 Lab7 Lab5 Lab4Student 3 …Student 2Student 1Section 501Section 502Section 503J. Michael MooreFormat: (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_____ _______J. Michael MooreDeclaring Multi-Dimensional Arrays As A TypeFormat: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;J. Michael MooreDeclaring Multi-Dimensional Arrays As A TypeExample: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;J. Michael MooreAccessing / 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]);J. Michael MooreMulti-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.J. Michael MooreA Character-Based Gridprogram gridExample (input, output);const MAX_ROWS = 4; MAX_COLUMNS = 4; NUM_COMBINATIONS = 10;type Grid = array[1..MAX_ROWS, 1..MAX_COLUMNS] of char;J. Michael MooreA 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 := '.';J. Michael MooreA Character-Based Grid (3) else {still in case statement} begin writeln('<< Error with the random # generator.>>'); writeln('<< Value should be 1-',NUM_COMBINATIONS, ' but random value is ', temp); anElement := '!'; end; end; generateElement := anElement;end;J. Michael MooreA Character-Based Gridprocedure 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; end; end;J. Michael MooreA 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;end;J. Michael MooreA 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;J. Michael MooreA Character-Based Grid (7)begin var aGrid : Grid; initialize(aGrid); writeln('Displaying grid'); writeln('==============='); display(aGrid); writeln; writeln('Displaying grid with bounding lines'); writeln('==========================');
View Full Document