Class 10 Lecture Notes
-
Upload
stephen-parsons -
Category
Documents
-
view
1.334 -
download
1
description
Transcript of Class 10 Lecture Notes
Class 10 – Arrays
Agenda
Lateral Thinking Warm-up Exam Results, Issues Discussion: SAAD - Problem Solving with
Code Discussion/Demo: Intro to Arrays Intro to Assignment 7: Daily Agenda Intro to Project 2: Battleship
Team Formation Task assignment
Project Work and Wrap-up
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)
SAAD – 3-tier (n-tier)
Presentation Logic Data
SAAD - MVC
Model View Controller
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
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]
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”]
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
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
Creating and Accessing Arrays
PracticeHandout: Exercise 10-1, Creating and Accessing an Array
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”
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
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
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
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.
Wrap-up
Summary Arrays Analysis & Design Assignment Project
Next Week Brief intro to Classes Business Problems with Fishbone Diagram Project Time*
Practice/ Set-up
Use this time to attempt the assignment to prepare you for the project, OR
Begin to set up your team and the 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/Understa
nding_Ruby_Arrays