Two Dimensional Arrays and Complex Conditions

Slides:



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

Java Unit 9: Arrays Declaring and Processing Arrays.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Arrays, Type Casting and Constants Engineering 1D04, Teaching Session 5.
Introduction to Algorithms Engineering 1D04, Teaching Session 1.
Records Engineering 1D04, Teaching Session 11. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Records  So far we've seen a number of different.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
Computers, Variables and Types Engineering 1D04, Teaching Session 2.
Objects and Classes Continued Engineering 1D04, Teaching Session 10.
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:
Objects and Classes Engineering 1D04, Teaching Session 9.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
Multidimensional Arrays Computer and Programming.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Arrays.
Chapter 4 Stacks
Array in C# Array in C# RIHS Arshad Khan
Review 1.
UMBC CMSC 104 – Section 01, Fall 2016
10.4 The Algebra of Matrices
(Numerical Arrays of Multiple Dimensions)
Java for Beginners University Greenwich Computing At School DASCO
Two-Dimensional Arrays
Chapter 8 – Arrays and Array Lists
Repetition Structures Chapter 9
Lecture 5: Some more Java!
Today’s Material Arrays Definition Declaration Initialization
Data Structures and Algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
Matrix 2015/11/18 Hongfei Yan zip(*a) is matrix transposition
Numeric Arrays Numeric Arrays Chapter 4.
Other Kinds of Arrays Chapter 11
A simple way to organize data
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Multi-dimensional Array
Counted Loops.
Lecture 07 More Repetition Richard Gesick.
Stack Data Structure, Reverse Polish Notation, Homework 7
CS1100 Computational Engineering
Arrays An Array is an ordered collection of variables
Array Data Structure Chapter 6
Multidimensional Arrays Vectors of Vectors
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Can store many of the same kind of data together
Sequence Alignment 11/24/2018.
Use of Mathematics using Technology (Maltlab)
CS 240 – Lecture 9 Bit Shift Operations, Assignment Expressions, Modulo Operator, Converting Numeric Types to Strings.
EKT150 : Computer Programming
Coding Concepts (Sub- Programs)
Arrays 6-Dec-18.
Coding Concepts (Data Structures)
Topic 1: Problem Solving
Lial/Hungerford/Holcomb/Mullins: Mathematics with Applications 11e Finite Mathematics with Applications 11e Copyright ©2015 Pearson Education, Inc. All.
Topic 1: Problem Solving
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Can store many of the same kind of data together
Multidimensional Arrays
Lecture 13: Two-Dimensional Arrays
CISC124 Labs start this week in JEFF 155.
Multidimensional array
Can store many of the same kind of data together
The Java switch Statement
CIS16 Application Development and Programming using Visual Basic.net
Dr Tripty Singh Arrays.
Loops and Arrays in JavaScript
Array Data Structure Chapter 6
Multi-Dimensional Arrays
Dimensions matching Rows times Columns
Arrays and Matrices Prof. Abdul Hameed.
Presentation transcript:

Two Dimensional Arrays and Complex Conditions Engineering 1D04, Teaching Session 6

Recap Arrays © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

recap: Arrays const int numScores = 10; int [] score = new int[numScores]; string [] name = new string[numScores]; © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

recap: Arrays const int numScores = 10; int [] score = new int[numScores]; string [] name = new string[numScores]; always points to element indexed as 0 always points to element indexed as 0 numScores elements in both arrays © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

recap: Arrays How do we work with elements in the array? int i, total; for (i = 0; i < numScores; i++) { //do what we want to with element i total += score[i]; //for example } © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

recap: Arrays Alternative int i, total; total = 0; i = 0; while (i < numScores) { //do what we want to with element i total += score[i]; //for example i++; } © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

A new kind of loop Another (very safe) construct for loops. int total; foreach (int scoreValue in score) { //do what we want to with element total += scoreValue; //for example } © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

A new kind of loop Another (very safe) construct for loops. int total; type must match type of each element in the array - the variable is local to the loop and can have any name int total; total = 0; foreach (int scoreValue in score) { //do what we want to with element total += scoreValue; //for example } element cannot appear on the left of the = it is the element in the array - not the index of the element © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two Dimensional Arrays © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays A very common data structure is a 2-D array. A mathematical matrix is a good example. a1,1 a1,2 a1,3 a1,4 a2,1 a2,2 a2,3 a2,4 a3,1 a3,2 a3,3 a3,4 traditionally ai,j where i gives the row and j gives the column © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Let us assume that we have an array of this form, where each element is an integer. What algorithm could we construct to total each row and each column? a1,1 a1,2 a1,3 a1,4 a2,1 a2,2 a2,3 a2,4 a3,1 a3,2 a3,3 a3,4 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Let us assume that we have an array of this form, where each element is an integer. What algorithm could we construct to total each row and each column? a1,1 a1,2 a1,3 a1,4 r1 a2,1 a2,2 a2,3 a2,4 r2 a3,1 a3,2 a3,3 a3,4 r3 c1 c2 c3 c4 The first thing to realize is we are trying to calculate elements of two new 1-D arrays, r and c. Each element of r and each element of c is an integer. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Let us assume that we have an array of this form, where each element is an integer. What algorithm could we construct to total each row and each column? a1,1 a1,2 a1,3 a1,4 r1 = a1,1+a1,2+a1,3+a1,4 a2,1 a2,2 a2,3 a2,4 r2 = a2,1+a2,2+a2,3+a2,4 a3,1 a3,2 a3,3 a3,4 r3 = a3,1+a3,2+a3,3+a3,4 c1 c2 c3 c4 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Let us assume that we have an array of this form, where each element is an integer. What algorithm could we construct to total each row and each column? a1,1 a1,2 a1,3 a1,4 for i = 1,..,3 a2,1 a2,2 a2,3 a2,4 ri = ai,1+ai,2+ai,3+ai,4 a3,1 a3,2 a3,3 a3,4 c1 c2 c3 c4 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Let us assume that we have an array of this form, where each element is an integer. What algorithm could we construct to total each row and each column? a1,1 a1,2 a1,3 a1,4 for i = 1,..,3 a2,1 a2,2 a2,3 a2,4 ri = 0 a3,1 a3,2 a3,3 a3,4 for j = 1,2,..,4 ri = ri + ai,j c1 c2 c3 c4 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Let us assume that we have an array of this form, where each element is an integer. What algorithm could we construct to total each row and each column? a1,1 a1,2 a1,3 a1,4 for i = 1,..,3 a2,1 a2,2 a2,3 a2,4 ri = 0 a3,1 a3,2 a3,3 a3,4 for j = 1,2,..,4 ri = ri + ai,j c1 c2 c3 c4 So, what about c? © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Total each row and each column? a1,1 a1,2 a1,3 a1,4 for i = 1,..,3 a2,1 a2,2 a2,3 a2,4 ri = 0 a3,1 a3,2 a3,3 a3,4 for j = 1,2,..,4 ri = ri + ai,j for j = 1,2,..,4 cj = 0 for i = 1,..,3 cj = cj + ai,j © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Total each row and each column? a1,1 a1,2 a1,3 a1,4 for i = 1,..,3 a2,1 a2,2 a2,3 a2,4 ri = 0 a3,1 a3,2 a3,3 a3,4 for j = 1,2,..,4 ri = ri + ai,j for j = 1,2,..,4 cj = 0 for i = 1,..,3 cj = cj + ai,j These are called nested loops © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Before we see how we implement 2-D arrays in C# - a simple question. Are these two algorithms equal? for j = 1,2,..,4 for k = 1,2,..,4 cj = 0 ck = 0 for i = 1,..,3 for r = 1,..,3 cj = cj + ai,j ck = ck + ar,k © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Before we see how we implement 2-D arrays in C# - a simple question. Are these two algorithms equal? for j = 1,2,..,4 for k = 1,2,..,4 cj = 0 ck = 0 for i = 1,..,3 for r = 1,..,3 cj = cj + ai,j ck = ck + ar,k Yes they are! The names of the indices are irrelevant - they are often called “dummy indices”. Step through the algorithms to see … © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays We can also generalize the algorithms for n rows and m columns, for example Sum columns Sum rows for j = 1,2,..,m for i = 1,2,..,n cj = 0 ri = 0 for i = 1,2,..,n for j = 1,2,..,m cj = cj + ai,j ri = ri + ai,j © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays The only new concept really is how to declare multi-dimension arrays. We declare our 2-D array by: const int m = 4; const int n = 3; int [ , ] a = new int [n, m]; © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Two-Dimensional Arrays Then we implement the algorithm: for j = 1,2,..,m cj = 0 for i = 1,2,..,n cj = cj + ai,j const int m = 4; const int n = 3; int [ , ] a = new int [n, m]; int [] c = new int[m]; //assume values entered in a for (j = 0; j < m; j++) { c[j] = 0; for (i = 0; i < n; i++) c[j] += a[i,j] } Adding the rows is obviously very similar - try it yourself © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

recap: Conditions Write a program that determines if an integer year is a leap year. What conditions are there on being a leap year? © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

recap: Conditions Write a program that determines if an integer year is a leap year. For a year to be a leap year: It must be divisible by 4 It must not be divisible by 100 However, if it is divisible by 100 and 400 it is a leap year © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions Divisible by 4? No Not Leap Year Yes Not Leap Year © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions it’s a bit of a mess  private void button1_Click(object sender, EventArgs e) { int year = Convert.ToInt32(inputBox.Text); if ((year % 4) == 0){ if ((year % 100) == 0){ if ((year % 400) == 0){ outputBox.Text = "Leap Year"; }else { outputBox.Text = "Not Leap Year"; } alternative convention (saves space) it’s a bit of a mess  © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions One big problem is that there are multiple paths to get to the same result. Another is that we have unnecessarily nested if statements. This is a good example of why we need to work on analysis and design before coding. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions Step 1. Remove duplicate outputs Divisible by 4? No Not Leap Year Yes Not Leap Year No Divisible by 100? Divisible by 400? Yes Yes Leap Year No Leap Year © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions Step 1. Remove duplicate outputs Divisible by 4? No Not Leap Year Yes No Divisible by 100? Divisible by 400? Yes Yes No Leap Year © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions Try and make a single condition in here Step 2. Combine multiple conditions into one larger one. Try and make a single condition in here The Magic Condition Divisible by 4? No Not Leap Year No Yes No Divisible by 100? Divisible by 400? Yes Yes No Leap Year Yes © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions How do we determine what the Magic Condition is? Look for the routes that make the condition true. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions Route 1. Divisible by 4? No Not Leap Year Yes No © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

More Complex Conditions Route 2. Divisible by 4? No Not Leap Year Yes No Divisible by 100? Divisible by 400? Yes Yes No Leap Year © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

More Complex Conditions We need to map our way through each individual route and determine what it takes for it to be true. Each individual condition needs to be grouped with && (and) because we need all the conditions in the route to be satisfied. If we take the no branch of a condition, we need to put a ! in front of it. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions Route 1. (year % 4 == 0) && !(year % 100 == 0) (year divisible by 4) and not (year divisible by 100) Divisible by 4? No Not Leap Year Yes No Divisible by 100? Divisible by 400? Yes Yes No Leap Year © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions (year divisible by 4) and Route 2. (year % 4 ==0) && (year % 100 == 0) && (year % 400 ==0) Divisible by 4? No Not Leap Year Yes (year divisible by 4) and (year divisible by 100) and (year divisible by 400) No Divisible by 100? Divisible by 400? Yes Yes No Leap Year © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions Since either route could result in a leap year we can say that to get a leap year we must have that route1 or route2 is true. Or is represented by || ((year % 4 == 0) && (year % 100 != 0)) || ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0)) © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions We can look at the routes differently to make this simpler. The first decision box is part of two routes. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions Common. Route 1. Route 2. Divisible by 4? No Not Leap Year Yes No Divisible by 100? Divisible by 400? Yes Yes No Leap Year © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions We can look at the routes differently to make this a little simpler. Both successful routes have to go through the divisible by 4 box. Common. Route 1. (year % 4 == 0) && ((year % 100 != 0) || ((year % 100 == 0) && (year % 400 == 0))) Route 2. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions There is one more simplification. (year % 100 == 0) is redundant. Why? (year % 4 == 0) && ((year % 100 != 0) || ((year % 100 == 0) && (year % 400 == 0))) © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions So it comes down to this! and we can implement this very clearly in our code. (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions private void button1_Click(object sender, EventArgs e) { int year = Convert.ToInt32(inputBox.Text); if ((year % 4 == 0)&& ((year % 100 != 0) || (year % 400 == 0))) outputBox.Text = "Leap Year"; } else outputBox.Text = "Not Leap Year"; } much better  © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng

Complex Conditions private void button1_Click(object sender, EventArgs e) { int year = Convert.ToInt32(inputBox.Text); if ((year % 4 == 0)&& ((year % 100 != 0) || (year % 400 == 0))) outputBox.Text = "Leap Year"; } else outputBox.Text = "Not Leap Year"; } © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng