class 10 arrays

21
Class 10 – Arrays

Upload: stephen-parsons

Post on 16-Jan-2015

641 views

Category:

Education


3 download

DESCRIPTION

This is the presentation from class # 10 of the Logic and Problem-solving Course (on Arrays)

TRANSCRIPT

Page 1: Class 10 Arrays

Class 10 – Arrays

Page 2: Class 10 Arrays

Agenda

Lateral Thinking Warm-up Project 1 Results, Issues Discussion: SAAD - Problem Solving with

Code Discussion/Demo: Intro to Arrays Intro to Assignment 7: Daily Agenda Intro to Project 2: Battleship

Task assignment Rubric

Project Work and Wrap-up

Page 3: Class 10 Arrays

System Analysis & Design

Problem Solving with Code Tools

IPO (outside in) Pseudocode (linear, integrative) Flowchart (linear, integrative)

Models & Approaches SDLC 3-tier (n-tier) MVC (OOP)

Page 4: Class 10 Arrays

SAAD – 3-tier (n-tier)

Presentation Logic Data

Page 5: Class 10 Arrays

SAAD - MVC

Model View Controller

Page 6: Class 10 Arrays

MVVM – the NeXt Generation

Model View View-Model

Data Binding Service Structure

Page 7: Class 10 Arrays

MVC, N-Tier, MVVM – who cares? Moving towards OOP Clean division of work between

functional units Example: Calculator Converter:

Model? View? Controller? Project 2 – later tonight…

Page 8: Class 10 Arrays

Intro to Arrays

What is an array? A collection of related data Similar to a set of mailboxes at a site

The site is the array, each mailbox represents an element

The mailbox may contain a specific value or object

The contents can be strings, integers, or even another array

Addressed using brackets or parentheses (zero-based)

Can be examined iteratively or uniquely

Page 9: Class 10 Arrays

Arrays Illustrated

Single Dimension Array=days_of_week(7)

2-Dimensional (table) Array=address_book(4,7)

Mon Tues Wed Thurs

Fri Sat Sun

fname lname addr city prov post Email

joe smith 123 My Street

Halifax NS B1B 1B1 [email protected]

jane jones 1 Main Dr Dartmouth NS B7B 7B7 [email protected]

pat white 21 Goode Dr.

Halifax NS B1A 2C3 [email protected]

sam davis 19 Smith Dr. Timberlea NS B9G 2K8 [email protected]

Page 10: Class 10 Arrays

Creating and Accessing Arrays

Single Dimensiona = Array.new # an empty array

a = [] # same as above

puts a => nil (no output)

a[0]=“hello” # assigns the value

a[1]=“ world”

puts a[0] + a[1]

puts a

x = [1,”green”,”martian”]

Page 11: Class 10 Arrays

Array Properties and Methods

Properties a=[“hello”, 1, “world”,2,”!”]

a.last => “!” a.first => “hello” a[0].class => String a[1].class => Fixnum a.at(1) => 1 a.length => 5

Page 12: Class 10 Arrays

Array Properties and Methods

Methodsa.push (“my friend”) => [ “hello”, 1,

“world”,2,”!”,”my friend”]a.pop => [ “hello”, 1, “world”,2,”!”]a.sort => [1,2, “!”,”hello”,”world”]a.reverse = [“!”,2,”world”,1,”hello”]a.sort! => applied to array (permanent

change)a.delete_at(2) => [ “hello”, 1, 2,”!”]a.each do |variable| end => iterates through the array

Page 13: Class 10 Arrays

Creating and Accessing Arrays

PracticeHandout: Exercise 10-1, Creating and Accessing an Array

Page 14: Class 10 Arrays

2-Dimensional Arrays

Creating x=Array.new(4) {Array.new(7)} a=[[1,"green"],[2,"red"],[3,"blue"]]

Accessing & Assigning myval= a[1][0] => “green” x[2][4]=“Halifax” a[1][1]=“orange”

Page 15: Class 10 Arrays

Iterating Through Arrays

Using eachrows=5cols=5countr=0mtrx=Array.new(rows) {Array.new(cols)}mtrx.each do |myvar|myvar.each do |ovar|

ovar=countrcountr=countr+1

endend

Page 16: Class 10 Arrays

Iterating Through Arrays

Using forrows=5cols=5countr=0for x in (0..rows-1) for y in (0..cols-1) mtrx[x][y]=countr countr=countr+1 endend

Page 17: Class 10 Arrays

Creating and Accessing Arrays

PracticeHandout: Exercise 10-2, Creating and Accessing a multi-dimensional Array

Page 18: Class 10 Arrays

Assignment 7: Daily Agenda

Use a 2D Array to hold details Prompt for time (hour only) Prompt for appointment Prompt for location Assign inputs to elements Continue to prompt until user types

in a blank in the time slot Sort the Agenda and then generate

Daily Agenda by iterating through the array

Page 19: Class 10 Arrays

Project 2: Battleship

Simple Version 8 x 8 grid (A-H, 1-8) 2 ships each player Each ship assigned to a particular grid location 10 shots each player A shot to the location of a vessel sinks it Draw grid and results after each shot “~” is unknown, “!” is sunk, “0” is a miss If two of the ships of any player are sunk, the

other player wins, end game If after ten shots one player has more ships than

the other, he/she wins, otherwise it’s a tie.

Page 20: Class 10 Arrays

Wrap-up

Summary Arrays Analysis & Design Assignment Project

Next Week Brief intro to Classes Business Problems with Fishbone Diagram Project Time*

Page 21: Class 10 Arrays

Practice/ Set-up

Use this time to attempt the assignment to prepare you for the project, OR

Begin to set up your tasks, OR See me about any issues with grades,

etc. OR Do any research you need to get the

assignment or project completed. GREAT TIP SITE:

http://www.techotopia.com/index.php/Understanding_Ruby_Arrays