an “enjoyable” introduction to programming dr. jeyakesavan veerasamy the university of texas at...

73
An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: [email protected] Alice 2.2 Software: www.alice.org Examples: www.utdallas.edu/~jeyv/alice

Upload: joshua-morgan

Post on 23-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

An “enjoyable” introduction to Programming

Dr. Jeyakesavan VeerasamyThe University of Texas at Dallas, USA

Email: [email protected] Alice 2.2 Software: www.alice.org

Examples: www.utdallas.edu/~jeyv/alice

Page 2: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

What is programming?

• Developing applications & games • Software is not limited to PC – most complex systems run software– smart phones, game devices, even DVD players

Page 3: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Programming …• is NOT a boring or repetitive activity• does NOT require you to sit in dark room and

type in computer all day! • does NOT involve complex Math

• requires logical thinking – technical common sense

• write minimal code & combine with existing components to build new applications

• Solve customers’ problems & improves quality of life for every one.

Page 4: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Why learn programming?

• Software Engineers get great pay!• Less stressful compared to several other high

paying jobs• Automation continues…• Computer touches our lives more & more every

day…• More component based programming always

room for simple programs to do large tasks!

Page 5: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Learning to ride bicycle

• Difficulties for beginners:

• Difficulties for experienced folks:

Page 6: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Learning to ride bicycle

• Difficulties for beginners:– Learning to balance & go forward together

• Difficulties for experienced folks:– None.

Page 7: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Solution for beginners

• Training wheels• Helmet

• Makes learning enjoyable and safe!

• Similar difficulties are there while learning to program in a computer.

Page 8: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Learning to program:Difficulties for beginners

Page 9: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Learning to program:Difficulties for beginners

1. Syntax errors • struggle for hours to fix syntax errors• Loose confidence • Frustrating experience• Run away & never come back if possible!

2. Logic errors Not a serious issue.

Page 10: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Difficulties for experienced programmers

Logic errorsContinuous learning

Page 11: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Learning to program

• Most students try to learn syntax and logic together, waste lot of hours and get confused & frustrated.

• Indian students resort to memorization of programs. US students run away from programming. Both are NOT good

• It is possible to do much better!

Page 12: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Solution

• Visual Programming Tools to teach programming concepts without encountering syntax errors.

• Focus on the logic first & build confidence.

Page 13: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Free Visual Programming ToolsTool Provider Web-site

Alice 2.2 Carnegie Mellon University, USA

www.alice.org

Scratch MIT, USA scratch.mit.eduSnap! UCBerkeley, USA byob.berkeley.edu

Lego MindStorm Lego, USA mindstorms.lego.com

Several more…

Page 14: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Sample program in Snap 4.0

Page 15: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program Output

Page 16: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Programming Concepts

Page 17: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

A few examples

• Recipe to make your favorite food• Assembly instructions for a toy• Coming to college from homeWhat is common about these activities?

Page 18: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

A few examples

• Recipe to make your favorite food• Assembly instructions for a toy• Coming to college from homeWhat is common about these activities?

Sequence

Page 19: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Programming concepts:Sequence structure

instruction 1;instruction 2;instruction 3;…

Page 20: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

A few more examples

• Study at home or play cricket with friends?• Eat Parotta or Poori?• Go to Vijay’s movie or Karthik’s movie?• Go to job or go for higher studies?What is the common thing here?

Page 21: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

A few more examples

• Study at home or play cricket with friends?• Eat Parotta or Poori?• Go to Vijay’s movie or Karthik’s movie?• Go to job or go for higher studies?What is the common thing here?

Selection

Page 22: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Selection structure

IF condition is true THEN do this;ELSE do that;ENDIF

Page 23: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

A few more examples

• Eat chips from a packet• Go on a shopping spree with lot of cash!• Take an exam that has several questionsWhat is the common thing here?

Page 24: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

A few more examples

• Eat chips from a packet• Go on a shopping spree with lot of cash!• Take an exam that has several questionsWhat is the common thing here?

Repetition

Page 25: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Repetition structure

WHILE (more items to process) process the next item;ENDWHILE

FOR month = 1 to 12 do monthly processingENDFOR

Page 26: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Programming Concepts

• Structures: Sequence, Selection & Repetition• Foundation for Programming• Every complex program is only a combination

of these structures.

Page 27: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

More things we do…

• Use a box to move lots of things from one room to another

• Carry a pack of candies to class on your birthday!

• What is the common thing here?

Page 28: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

More things we do…

• Use a box to move lots of things from one room to another

• Carry a pack of candies to class on your birthday!

• What is the common thing here?

Collection / Arrays

Page 29: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Arrays

• enable us to store data of similar type together.

• enables us to handle varying size data.• Lines of code do not increase with more data!

FOR each item in array add item to total

ENDFOR

Page 30: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Even more things we do…

• Get phone call when you are driving a car• Friend knocks on the door when you are

watching a movie

• What is the common thing here?

Page 31: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Even more things we do…

• Get phone call when you are driving a car• Friend knocks on the door when you are

watching a movie

• What is the common thing here?

Interrupts / events

Page 32: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Event driven programming

• Suspend current processing to process the event, or process it in parallel.

• “Regular” processing flow & separate processing routine for each event.

Page 33: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Object Oriented Programming (OOP)

• Models the real-world better• Concepts learned from the manufactoring

industry

Page 34: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Hands-on Introduction to Alice

It is a good idea to save each program separately so that you can

go back and review later.

Page 35: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Purpose of Alice

• Enjoyable introduction to Programming• Natural introduction to OOP• Learning to program with training wheels• Stepping stone to learn more serious

languages (C/C++/Java)

Page 36: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Alice environment

• World• Gallery• Scene Setup– Adding objects– Working with objects

• Similar to Scene setup in move-making– Every object needs to be in starting position

Page 37: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Methods & Actions

• You are welcome to play along with me.• Add one object– Test out various movements (moves in 6

directions, turns in 4 ways & roll in 4 ways)• Add one more object– Test out relative movements

Page 38: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #1: Choose your favorite object from the gallery and test various methods.

10 minutes?

Page 39: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Our first program!

• Sequence structure• do a few interesting actions!

Page 40: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #2: Make your object do a few funny actions!

10 minutes?

Page 41: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #3

• Introduce multiple objects• do a few interesting actions!

Page 42: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #3: Make your objects do a few funny actions!

15 minutes?

Page 43: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #4

• Use DO TOGETHER structure• Add realism & sophistication!

Page 44: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #4: Use DO TOGETHER to do multiple things at same time!

15 minutes?

Page 45: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #5

• Add Vehicles/Seaplane from local gallery.• Make it do loop the loop.• Use DO TOGETHER structure– seaplane move forward– seaplane turn backward– seaplane’s propeller roll right

• Use “style = abruptly” to make the motion smooth

Page 46: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #5: Use DO TOGETHER to make the plane do loop the loop!

20 minutes?

Page 47: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #6

• Introduce repetition – counted loop• do a few interesting actions!

Page 48: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #6: Make your objects do a few funny actions multiple times!

10 minutes?

Page 49: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #7

• How to do realistic walking?• Leg & body movements• Better to do with a human object from

“People/Walking People”• Write your own methods.

Page 50: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #7: Practice human walking/running animation

15 minutes?

Page 51: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #8

• Walking back and forth between 2 points?• Use 2 objects as end-points.• What should you do when you get close to

those objects?

Page 52: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #8: Practice human walking/running back & forth.

15 minutes?

Page 53: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #9

• Alternate implementation of Program #8 using IF statement

• Can we use same code for step 1 and step 2?

Page 54: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #9: Practice human walking/running back & forth. Use variables and decision

structure

15 minutes?

Page 55: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Logical thinking

• You have been introduced to several building blocks – a few more on the way too.

• Now, it is all about putting/using them together in various combinations.

• I encourage you to think in high level, note down a few ideas (design), then proceed to implementation.

• Direct coding is NOT recommended.

Page 56: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #10: Lists/Arrays

Arrays cannot expand during run-time, but lists can. Alice supports more functionality with lists too. In simple words, lists are more powerful.

• Make a list of your favorite objects (multiple copies of an object is ok too!)

• Make them do something one at a time.• Make them do something in random order• Make them do something in sync.• You are not limited to these - use your imagination!

Page 57: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #10: Group & individual dance moves!Use similar objects so that you can work with

the parts too.

15 minutes?

Page 58: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #11: Events

• So far, we did everything under “my first method” or we invoked other methods from there.

• But when it comes to games or complex apps, user may press various buttons/items & we need to respond to each event.

Page 59: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #11: We will start with a coach and a ball. Coach keeps doing some exercise. We will use key based events to move the ball to top of his head. Then, coach should stop exercising and do something funny.

15 minutes?

Page 60: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #12: Frog’s jumping game!

• Swimming pool – moving lilypads – make the frog to jump on lilypads carefully and reach other side to win!

• Make the lilypads to move independently back and forth first. Then, work on the game.

• If the frog jumps and touches the water, it will sink! I guess this frog does not know to swim

• Plan and write the pseudocode first, before coding!

Page 61: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Program #12: Frog jumping game. Discuss/share ideas with your neighbour and help each

other to progress …

Move frog 0.7 meters forward when up-arrow is pressed.For moving lilypads, use distance (pool width – cricle width) with bit random speed.Use vehicle property to stick to lilypad.

Page 62: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

High level idea/pseudocode #1move the frog by 0.7 meters.IF index is beyond lilypads

declare win!ELSE

IF frog is close to circles[index]frog is safe set vehicle for frog to corresponding circle

ELSEfrog dies! – make frog disappeardisplay a message

ENDIFENDIF increment index

Page 63: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

High level idea/pseudocode #2move the frog by 0.7 meters.IF frog is close to “finish” object, declare win!ELSE

IF frog is close to any of the moving circlesfrog is safe set vehicle for frog to corresponding circle

ELSEfrog dies! – make frog disappeardisplay a message

ENDIFENDIF

Page 64: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Transition to C/C++/Java

• Most of you have become “mature” Alice programmers – you can see the limitations of Alice now.

• Now that your fundamentals are bit strengthened, this is a great time to get started with C/C++/Java!

• I encourage you to do the following problems in both languages: Alice and C/C++/Java.

• Time management is crucial – I encourage you to spend a few hours to solve programming problems periodically to strengthen your skills.

Page 65: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Transition to C/C++/Java: Math Practice program

• Display 2 random numbers (limit the range for each number to 1-100) and ask the user to multiply manually & input the result. Verify whether the user input is correct. Repeat it for 10 times, keep track of the score and display it in the end like this:

Your score: 8 correct, 2 incorrect.

Page 66: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Transition to C/C++/Java: Guessing Game

• First, guess a number between 1 and 100. Then, your program should ask you a series of questions and determine your guess based on your answers.

Question format: How about NN (<,=,>)? • Write the high level pseudocode, manually

verify it before proceeding to code.

Page 67: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Pseudocodeinitialize low = 1, high = 100WHILE low < high

mid = (low + high) / 2ask the user how about mid?IF user response is <

high = mid-1ELSE IF user resposne is >

low = mid+1ELSE IF user response is =

print great!break;

IF low == great print “Your guess is” low

ENDIF

Page 68: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Sample run #1 (user input in RED)

Guess a number between 1 and 100 (both inclusive)and get ready to answer a few questions.How about 50 (<,=,>)? <How about 25 (<,=,>)? <How about 12 (<,=,>)? >How about 18 (<,=,>)? >How about 21 (<,=,>)? <How about 19 (<,=,>)? >Your guess is 20.

Page 69: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Sample run #2 (user input in RED)

Guess a number between 1 and 100 (both inclusive) and get ready to answer a few questions.How about 50 (<,=,>)? >How about 75 (<,=,>)? <How about 62 (<,=,>)? >How about 68 (<,=,>)? =Your guess is 68.

Page 70: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Transition to C/C++/Java: Guessing Game

• Play the reverse game. Computer should guess a number between 1 and 100. Now, you should ask questions & determine that number.

• C/C++: use srand() and rand() to generate random numbers

• Write the pseudocode, manually verify it before proceeding to code.

Page 71: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Pseudocodeguess = Generate a random number 1 … 100Ask for user inputWHILE user input != guess

IF guess < user input output “<“

ELSEoutput “>”

Ask for user inputOutput “You guessed it right. Good game!”

Page 72: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

Sample run

I have guessed a number. You can ask questions now.50<25<12>

18>21<19>20=Good job!

Page 73: An “enjoyable” introduction to Programming Dr. Jeyakesavan Veerasamy The University of Texas at Dallas, USA Email: jeyv@utdallas.edujeyv@utdallas.edu Alice

References

• Alice website: www.alice.org (download Alice 2.2 software)

• Textbook: Learning to Program with Alice by Wanda Dann, Steve Cooper and Randy PauschISBN: 0-1318-7289-3

• Free Ebook: www.dickbaldwin.com/tocalice.htm • My Alice examples: www.utdallas.edu/~

jeyv/alice