4 Programming

19
© Jalal Kawash 2010 4 Programming Peeking into Computer Science 1

description

4 Programming. Peeking into Computer Science. Mandatory: Chapter 5 – Section 5.5 Jalal’s resources: “How to” movies and example programs available at: http://pages.cpsc.ucalgary.ca/~kawash/peeking/alice-how-to.html JT’s resources: www.cpsc.ucalgary.ca/~tamj/203/extras/alice. - PowerPoint PPT Presentation

Transcript of 4 Programming

Page 1: 4 Programming

© Jalal Kawash 2010

4 ProgrammingPeeking into Computer Science

1

Page 2: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

Reading Assignment

Mandatory: Chapter 5 – Section 5.5Jalal’s resources:

◦ “How to” movies and example programs available at:

http://pages.cpsc.ucalgary.ca/~kawash/peeking/alice-how-to.htmlJT’s resources:

◦ www.cpsc.ucalgary.ca/~tamj/203/extras/alice

2

Page 3: 4 Programming

© Jalal Kawash 2010Peeking into Computer ScienceJT’s Extra: What Is A List?

A list is a variable that can be treated as one entity.

…but also a list consists of elements and the individual elements can be accessed separately.

3

Student 1 Student 2 Student 3 Student 4 Student 5

Example: I need to print out the whole class list

Student 1 Student 2 Student 3 Student 4 Student 5

Example: I need to fix this one student’s grade

Page 4: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

Objectives

At the end of this section, you will be able to:

1. Understand the list structure2. Create lists in Alice3. Apply the for all in order construct

to lists4. Apply the for all together construct

to lists5. Use functions to collect user input

Page 5: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

A list of Astronauts in Alice5

Page 6: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

Lists

A list is a tuple of numbered (indexed) objects

A list is a variable

John

0

Frank

1

Alicia

2

Jenn

3

James

4

Page 7: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

Creating a List in Alice7

Page 8: 4 Programming

© Jalal Kawash 2010Peeking into Computer ScienceJT’s Extra: What If A List Weren’t Used?

8

Example 12:‘How not to’

approach

March 3 soldiers forward

Without lists: you would create three soldier objects The actions for each

list would have to be repeated three times (once per object)

A list would allow the soldiers to be grouped together and instructions wouldn’t have to be repeated explicitly

Page 9: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra: For all in order and together

9

FOR ALL IN ORDEREach list element carries out instructions one element-at-a-time

FOR ALL TOGETHERThe instructions are performed at the same time by each list element

Page 10: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

JT’s Extra: March Example (Soldiers Again) 10

Example 13:Lists, for all in

order, for all together

For all in order: body of loop repeats three actions for each list element (soldier) one at-a-time

For all together: body of loop repeats three actions for each list element (soldier) simultaneously

Page 11: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

JT: For allINORDER Flowchart

11

i = 0

Start

Whilei < size-

of list

falsetrue

Stop

for-all-body

Increment I by 1

For all INORDER list JT: for-all-body action(s) to perform, each element list[i]

• Play sound• Each soldier

‘say’• Each soldier

moves

Page 12: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

JT’s: For allTOGETHER

12

Start

ifList is empty

true false

Stop

for-all-body(list[0])

Ffor-all-body(list[n-1]) ...

For all TOGETHER list for-all-body (JT: each simultaneous action)

JT’s Extra: perform same action on each list element at same time

Example program• Play sound• Each soldier ‘say’• Each soldier moves

Page 13: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

More Complex Example13

Page 14: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

world.dance Method14

Page 15: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

world.dance Method15

Page 16: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

For allINORDER

16

i = 0

Start

Whilei < size-

of list

falsetrue

Stop

for-all-body(list[i])

Increment I by 1

For all INORDER list for-all-body

Page 17: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

For allTOGETHER

17

Start

ifList is empty

true false

Stop

for-all-body(list[0])

Ffor-all-body(list[n-1]) ...

For all TOGETHER list for-all-body

Page 18: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

For

all i

n Fl

owch

arts

18

Loop rounds times

Start

Stop

x turns backward

x turns forward

x moves up

x rolls left

x moves down

For all x in spaceTeam INORDER

For all x in spaceTeam TOGETHER

Page 19: 4 Programming

© Jalal Kawash 2010Peeking into Computer Science

Collecting User Input19