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

E.g.9 For to do loop for i:=1 to 10 do writeln(i); While do loop i:=1;
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
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]
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
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 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 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 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 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 Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
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.
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.
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 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.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
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.
J. Michael Moore From James Tam's material Records CSCE 110.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
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 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 Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
James Tam Lists In this section of notes you will be introduced to new type of variable that consists of other types.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Chapter 8 Arrays and Strings
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions.
Pascal Programming Today Chapter 11 1 Chapter 11.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
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 Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
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)
Arithmetic operations, decisions and looping
Multidimensional Arrays
Multidimensional Arrays
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.
Visit for more Learning Resources
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 no. 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 -Transport -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 -Transport -Misc [1][2][3][4] [1] [2] [3] [4] [5] [6] [7]

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 -Transport -Fun March…FebruaryJanuary John’s finances Mary’s finances Bob’s finances

James Tam Declaring Multi-Dimensional Arrays Syntax: (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: johnFinances : array [1..7, 1..7] of real; cube : array[1..3, 1..4, 1..6] of char; RowsColumns

James Tam Accessing / Assigning Values To Elements Syntax: 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/array/map.p program map (input, output); const MAXROWS = 10; MAXCOLUMNS = 10; var world : array[1..MAXROWS, 1..MAXCOLUMNS] of char; r, c : integer; randomValue : real; quitChoice : char; editChoice : char; rowToEdit : integer; columnToEdit : integer; charToChange : char;

James Tam Example Program: Map Generator And Editor (2) begin for c := 1 to MAXCOLUMNS do world[1][c] := '-'; for c := 1 to MAXCOLUMNS do world[MAXROWS][c] := '-'; for r := 1 to MAXROWS do world[r][1] := '|'; for r := 1 to MAXROWS do world[r][MAXCOLUMNS] := '|' ;

James Tam Example Program: Map Generator And Editor (3) for r := 2 to (MAXROWS-1) do begin for c:= 2 to (MAXCOLUMNS-1) do begin randomValue := Random; if (randomValue <= 0.05) then world [r][c] := '~' else if (randomValue <= 0.25) then world [r][c] := '^' else if (randomValue <= 0.30) then world [r][c] := 'C' else if (randomValue <= 0.40) then world [r][c] := 'T' else world [r][c] := ' '; end; (* inner for *) end; (* outer for *)

James Tam Example Program: Map Generator And Editor (4) repeat begin for r := 1 to MAXROWS do begin for c := 1 to MAXCOLUMNS do begin write(world[r][c]); end; (* inner for loop *) writeln; end; (* for loop - displays world *) 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 (5) 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 (rowToEdit (MAXROWS-1)) OR (columnToEdit < 2) OR (columnToEdit > (MAXCOLUMNS-1)) then begin writeln('Value for row or column must be in the range of 2 - 9') end;

James Tam Example Program: Map Generator And Editor (6) else begin writeln('What do wish to change this square to? Choices include:'); writeln('"~" for water'); writeln('"^" for trees'); writeln('"C" for a city'); writeln('"T" for a town'); writeln('" " (A space) for an open field'); write('Enter choice and hit return: '); readln(charToChange); world[rowToEdit][columnToEdit] := charToChange; end; (* else *) end; (* edit mode *) writeln('Type "Q" or "q" to quit, or return to continue: '); readln(quitChoice); end; (* repeat loop *) until (quitChoice = 'Q') OR (quitChoice = 'q');

James Tam Example Program: Map Generator And Editor (7) end.

James Tam Summary You now know about multi-dimensional arrays: What is the syntax for declaring and using arrays of multiple dimensions? What determines the number of dimensions that an array should have? Operations on 2D arrays: Initialization of elements Scanning selected parts of the array using loops