James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.

Slides:



Advertisements
Similar presentations
James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Advertisements

Arrays.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop.
1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
1 Arrays in JavaScript Name of array (Note that all elements of this array have the same name, c ) Position number of the element within array c c[6] c[0]
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
James Tam Repetition In Pascal In this section of notes you will learn how to have a section of code repeated without duplicating the code.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type arrays.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays.
James Tam Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in your Pascal programs.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
J. Michael Moore Arrays CSCE 110 From James Tam’s material.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate your code.
James Tam Arrays / Lists In this section of notes you will be introduced to new type of variable that consists of other types.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
Getting Started With Pascal Programming
J. Michael Moore From James Tam's material Records CSCE 110.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
Link Lists In this section of notes you will learn how to create and manage a dynamic list. Link Lists in Pascal.
James Tam Records You will learn in this section of notes what is a record and how to use them in Pascal.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
James Tam Lists In this section of notes you will be introduced to new type of variable that consists of other types.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
VB .NET Programming Fundamentals
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 10.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Repetition in Pascal:
Paskal Dil Karşılaştırması
Arrays In this section of notes you will be introduced to a homogeneous composite type, one-dimensional arrays One-dimensional arrays in Pascal.
Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays Homogeneous.
Presentation transcript:

James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.

James Tam When To Use Arrays Of Different Dimensions Determined by the data – the number of categories of information determines the number of dimensions to use. Examples: (1D array) Tracking grades for a class Each cell contains the grade for a student i.e., grades[i] There is one dimension that specifies the student (2D array) Personal finances program One dimension of information specifies the financial category (cash in or cash out). The other dimension is used to specify the time One dimension (which student)

James Tam When To Use Arrays Of Different Dimensions (2) (2D array continued) Time Financial category JanuaryFebruaryMarch… Income -Rent -Food -Fun -Car -Misc Net income

James Tam When To Use Arrays Of Different Dimensions (3) (2D array continued) Notice that each row is merely a 1D array (A 2D array is an array containing rows of 1D arrays) -Rent Net income Income -Food -Fun [1][2][3][4] [1] [2] [3] [4] [5] [6] [7] Columns Rows -Misc -Car

James Tam When To Use Arrays Of Different Dimensions (4) (3D array – take the 2D array but allow for multiple people) The third dimension specifies whose finances are being tracked. Time (X) Financial category (Y) Person (Z)

James Tam When To Use Arrays Of Different Dimensions (5) -Misc Income -Rent -Food Net income -Car -Fun March…FebruaryJanuary John’s finances Mary’s finances Bob’s finances

James Tam 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; Example: var johnFinances : array [1..7, 1..7] of real; var cube : array[1..3, 1..4, 1..6] of char; RowsColumns

James Tam Declaring Multi-Dimensional Arrays As A Type Format: Type declaration Type name = array [min..max, min..max] of element type; Type name = array [min..max, min..max, min..max] of element type; Variable declaration Array name : Type name;

James Tam Declaring Multi-Dimensional Arrays As A Type (2) Example Type declaration Finances = array [1..7, 1..7] of real; Cube = array[1..3, 1..4, 1..6] of char; Variable declaration var johnFinances : Finances; var aCube : Cube;

James Tam Accessing / Assigning Values To Elements Format: name [row][column] := name [row][column]; Example: finances [1][1] := 4500; writeln (finances[1][1]);

James Tam Example Program: Map Generator And Editor You can find the full program in Unix under: /home/231/examples/arrays/map.p

James Tam Example Program: Map Generator And Editor: Breaking The Problem Down map.p makeBorderpopulatedisplayWorldeditWorld inBoundscharacterValid

James Tam Example Program: Map Generator And Editor program map (input, output); const MAX_ROWS = 10; MAX_COLUMNS = 10; type Level = array[1..MAX_ROWS, 1..MAX_COLUMNS] of char;

James Tam Example Program: Map Generator And Editor (2) procedure makeBorder (var aLevel: Level); var r : integer; c : integer; begin for c := 1 to MAX_COLUMNS do aLevel[1][c] := '-'; for c := 1 to MAX_COLUMNS do aLevel[MAX_ROWS][c] := '-'; for r := 1 to MAX_ROWS do aLevel[r][1] := '|'; for r := 1 to MAX_ROWS do aLevel[r][MAX_COLUMNS] := '|'; end; (* makeBorder *)

James Tam Example Program: Map Generator And Editor (3) procedure populate (var aLevel : Level); var r: integer; c: integer; randomValue: real;

James Tam Example Program: Map Generator And Editor (4) begin for r := 2 to (MAX_ROWS-1) do begin for c:= 2 to (MAX_COLUMNS-1) do begin randomValue := random; if (randomValue <= 0.05) then aLevel [r][c] := '~' else if (randomValue <= 0.25) then aLevel [r][c] := '^' else if (randomValue <= 0.40) then aLevel [r][c] := 'T' else aLevel [r][c] := ' '; end; (* inner for: traverse columns *) end; (* outer for: traverse rows *) end; (* populate *)

James Tam Example Program: Map Generator And Editor (5) procedure displayWorld (aLevel : Level); var r : integer; c : integer; begin for r := 1 to MAX_ROWS do begin for c := 1 to MAX_COLUMNS do begin write(aLevel[r][c]); end; writeln; end; (* for loop - displays world *) end; (* displayWorld *)

James Tam Example Program: Map Generator And Editor (6) function inBounds (row : integer; column : integer):boolean; begin if (row < 2) OR (row > (MAX_ROWS-1)) OR (column < 2) OR (column > MAX_COLUMNS-1) then inBounds := false else inBounds := true; end; (* inBounds *)

James Tam Example Program: Map Generator And Editor (7) function characterValid (newCharacter : char) : boolean; begin if (newCharacter = '~') OR (newCharacter = '^') OR (newCharacter = 'T') OR (newCharacter = ' ') then characterValid := true else characterValid := false; end; (* characterValid *)

James Tam Example Program: Map Generator And Editor (8) procedure editWorld (var world : Level); var editChoice: char; charToChange: char; rowToEdit: integer; columnToEdit: integer; begin writeln; write('Enter ''Y'' or ''y'' if you wish to edit the world or the return '); write('key otherwise: '); readln(editChoice);

James Tam Example Program: Map Generator And Editor (9) if (editChoice = 'Y') OR (editChoice = 'y') then begin writeln; write('Enter row (2 - 9) to edit: '); readln(rowToEdit); write('Enter column (2 - 9) to edit: '); readln(columnToEdit); if (inBounds(rowToEdit,columnToEdit) = false) then begin writeln('Value for row and column must be in the range of 2 - 9'); end

James Tam Example Program: Map Generator And Editor (10) else begin writeln('What do wish to change this square to? Choices include:'); writeln('"~" for water'); writeln('"^" for trees'); writeln('"T" for a town'); writeln('" " (A space) for an open field'); write('Enter choice and hit return: '); readln(charToChange); if (characterValid(charToChange) = true) then begin writeln('Changed: row ', rowToEdit, ', column ', columnToEdit, ' to ', charToChange); world[rowToEdit][columnToEdit] := charToChange; end else writeln('You can only populate the world with water, a forest,' ' a town or an empty space.'); end; (* else *) end; (* if edit mode chosen. *) end; (* editWorld *)

James Tam Example Program: Map Generator And Editor (11) begin var outside : Level; var quitChoice : char; makeBorder(outside); populate(outside); repeat begin displayWorld(outside); editWorld(outside); write('Type ''Q'' or ''q'' to quit, or return to continue: '); readln(quitChoice); end; (* repeat loop *) until (quitChoice = 'Q') OR (quitChoice = 'q'); end. (* End of main program *)

James Tam You Should Now Know The number of dimensions that should be set for an array How to declare arrays of multiple dimensions How to access and assign values to different parts (elements, rows etc.) of multi-dimensional arrays How to scan selected parts of the array using loops