SlideServe
Menu
Préférences
© SlideServe · v2026

Multi-Dimensional Arrays

fordon avatar
18 diapositives · 367 Views · Aug 07, 2014

Multi-dimensional arrays, such as 1D, 2D, and 3D arrays, are crucial in programming for organizing data efficiently. A 1D array can track grades for a class, where each element corresponds to a student; for example, grades[i]. For more complex data, like tracking multiple assignments across different students, 2D arrays are appropriate. This guide explains when to use different dimensions and how to declare and access multi-dimensional arrays. Understand their structure, benefits, and practical examples in various programming contexts.

Multi-Dimensional Arrays - slide 1
01 / 18Multi-Dimensional Arrays
1 / 17

Télécharger la présentation

Multi-Dimensional Arrays
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Multi-Dimensional Arrays CSCE 110

  2. One dimension (which student) 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

  3. Student Lab grade When To Use Arrays Of Different Dimensions • (2D array continued)

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

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

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

  7. Declaring Multi-Dimensional Arrays • 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; _____ _______

  8. 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. 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. 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. 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. 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. 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. A 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;

  15. 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; • end; • end;

  16. 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; • end;

  17. 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. 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.