Two dimensional arrays.

Slides:



Advertisements
Similar presentations
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.
Advertisements

§ 3.4 Matrix Solutions to Linear Systems.
An introduction to arrays
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall.
F UNDAMENTALS OF E NGINEERING A NALYSIS Eng. Hassan S. Migdadi Inverse of Matrix. Gauss-Jordan Elimination Part 1.
Matrices Jordi Cortadella Department of Computer Science.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Co. Chapter 3 Determinants Linear Algebra. Ch03_2 Let A be an n  n matrix and c be a nonzero scalar. (a)If then |B| = …….. (b)If then |B| = …..... (c)If.
4.4 Identify and Inverse Matrices Algebra 2. Learning Target I can find and use inverse matrix.
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
The goal is to give an introduction to the mathematical operations with matrices. A matrix is a 2-dimensional arrangement of (real valued) data. The data.
4.5 Matrices, Determinants, Inverseres -Identity matrices -Inverse matrix (intro) -An application -Finding inverse matrices (by hand) -Finding inverse.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Unit 3 Matrix Arithmetic IT Disicipline ITD 1111 Discrete Mathematics & Statistics STDTLP 1 Unit 3 Matrix Arithmetic.
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Copyright © Cengage Learning. All rights reserved. 7.7 The Determinant of a Square Matrix.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Section 2.1 Determinants by Cofactor Expansion. THE DETERMINANT Recall from algebra, that the function f (x) = x 2 is a function from the real numbers.
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.
Chapter 1 Section 1.6 Algebraic Properties of Matrix Operations.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Matrices and systems of Equations. Definition of a Matrix * Rectangular array of real numbers m rows by n columns * Named using capital letters * First.
A very brief introduction to Matrix (Section 2.7) Definitions Some properties Basic matrix operations Zero-One (Boolean) matrices.
MATRICES A rectangular arrangement of elements is called matrix. Types of matrices: Null matrix: A matrix whose all elements are zero is called a null.
2.1 Matrix Operations 2. Matrix Algebra. j -th column i -th row Diagonal entries Diagonal matrix : a square matrix whose nondiagonal entries are zero.
Fall 2011GNG1106-Mod7-1 Chapter 7 Arrays Objectives: One dimensional array Two dimensional arrays Passing arrays to subprograms.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Arrays Chapter 7.
Lecture 8: 2D Arrays and Nested Loops
Chapter 7 Matrix Mathematics
1.5 Matricies.
Two Dimensional Arrays
Matrix Operations.
Chapter 7: Array.
An introduction to arrays
C Passing arrays to a Function
Arrays An Array is an ordered collection of variables
2. Matrix Algebra 2.1 Matrix Operations.
4.5 Determinants of Matrices
JavaScript Arrays.
Multidimensional Arrays
Arrays Chapter 7.
Multidimensional array
Arrays Week 2.
Multidimensional Arrays
Presented By Farheen Sultana Ist Year I SEM
2D Arrays Just another data structure Typically use a nested loop
INC 161 , CPE 100 Computer Programming
Lets Play with arrays Singh Tripty
Basics of Linear Algebra
College Algebra Chapter 6 Matrices and Determinants and Applications
Lecture 14 2D Arrays Richard Gesick.
Multi-Dimensional Arrays
Combining relations via relational composition
Two dimensional arrays.
The Determinant of a 2  2 Matrix
Matrices.
Multidimensional Arrays Section 6.4
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
Arrays in MatLab Arrays can have any number dimensions
Two dimensional arrays.
Presentation transcript:

Two dimensional arrays. More arrays Two dimensional arrays.

But our hotel reservation system has a limitation. What if our hotel has more than one floor and each floor can have hundreds of rooms?

Maybe matrices from mathematics can help us. Arrays (so far) are one dimensional. Matrices are two dimensional. (from wikipedia)

Maybe matrices from mathematics can help us. So the row can be the floor of the hotel and the column can be the particular room # on that floor!

Introducing 2D arrays.

2D arrays Declare (and allocate) a 1D array int N = 100; //number of rooms boolean rooms[] = new boolean [ N+1 ]; Declare (and allocate) a 2D array int Floors = 10; //number of floors int Rooms = 100; //rooms on a floor boolean rooms2D[][] = new boolean[Floors+1][Rooms+1];

2D arrays Initialize a 1D array Initialize a 2D array for (int i=0; i<=N; i++) rooms[i] = false; //initially unoccupied Initialize a 2D array int Floors = 10; //number of floors int Rooms = 100; //rooms on a floor boolean rooms2D[][] = new boolean[Floors+1][Rooms+1]; Looks like we’ll need a for loop for the floor and another for loop for the rooms. But first, how would we initialize all of the rooms on floor 0?

2D arrays Initialize a 1D array Initialize a 2D array for (int i=0; i<=N; i++) rooms[i] = false; //initially unoccupied Initialize a 2D array int Floors = 10; //number of floors int Rooms = 100; //rooms on a floor boolean rooms2D[][] = new boolean[Floors+1][Rooms+1]; But first, how would we initialize all of the rooms on floor 0? for (int rm=0; rm<=Rooms; rm++) rooms2D[0][rm] = false;

2D arrays But first, how would we initialize all of the rooms on floor 0? for (int rm=0; rm<=Rooms; rm++) rooms2D[0][rm] = false; Or… int f = 0; rooms2D[f][rm] = false; Can we initialize the next floor?

2D arrays Can we initialize the next floor? int f = 0; for (int rm=0; rm<=Rooms; rm++) rooms2D[f][rm] = false; f = 1; How about the next?

2D arrays How about the next? int f = 0; for (int rm=0; rm<=Rooms; rm++) rooms2D[f][rm] = false; f = 1; f = 2; … So f starts at 0 and then increments to the next floor and so on.

2D arrays Initialization with nested for loops. for (int f=0; f<=Floors; f++) { for (int rm=0; rm<=Rooms; rm++) { rooms2D[f][rm] = false; }

Recall from our 1D hotel reservation system… //function that determines how many rooms // are occupied in our hotel static int occupiedCount ( boolean rooms[], int N ) { int count = 0; for (int i=1; i<=N; i++) { if (rooms[i]) ++count; } return count; Now, how can we make a similar function that works for our 2D hotel?

Recall from our 1D hotel reservation system… //function that determines how many rooms // are occupied in our 2D hotel static int occupiedCount ( boolean rooms2d[][], int Floors, int Rooms ) { int count = 0; for (int i=1; i<=N; i++) { if (rooms[i]) ++count; } return count; This still needs “adjustment.”

Recall from our 1D hotel reservation system… //function that determines how many rooms // are occupied in our 2D hotel static int occupiedCount ( boolean rooms2d[][], int Floors, int Rooms ) { int count = 0; int f = 1; for (int i=1; i<=Rooms; i++) { if (rooms[f][i]) ++count; } return count; This is better but it still needs “adjustment.”

Recall from our 1D hotel reservation system… //function that determines how many rooms // are occupied in our 2D hotel static int occupiedCount ( boolean rooms2d[][], int Floors, int Rooms ) { int count = 0; for (int f=1; f<=Floors; f++) { for (int i=1; i<=Rooms; i++) { if (rooms[f][i]) ++count; } return count; Done!

Back to matrices from mathematics. In linear algebra, the trace of an n-by-n square matrix A is defined to be the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right) of A, i.e., (from wikipedia)

Practice with 2D arrays Calculate the trace of a 2D array. Calculate the sum of all of the elements in a 2D array. Calculate the sum of a particular row in a 2D array. Calculate the sum of a particular column in a 2D array. Calculate means (averages) for 1-4 above. Calculate mean for 3x3.

Recap Pass by value Pass by reference 1D arrays and 2D arrays Arrays and functions