41
CSC 110 - Intro. to Computing Lecture 12: PALGO

CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available Download from Blackboard/web Quiz #3 will be in class

Embed Size (px)

Citation preview

Page 1: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

CSC 110 -Intro. to Computing

Lecture 12:

PALGO

Page 2: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Announcements

Homework #3 solutions availableDownload from Blackboard/web

Quiz #3 will be in class on ThursdayCovers same topics as the homeworkUseful to review material before midterm

Page 3: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Programming Languages

Solving problems using a computer, we usually need a programProgram could be written in machine code

Series of 1s and 0s that computer understands

Difficult to read & write in machine languageAlso very time consuming

Programs can contain billions of instructions

Page 4: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Low-Level Languages

Close to machine code, but easier to understandAssembly language replaces 1s & 0s with

mnemonics: add, sub, ldi“C/C++” uses code that is closer to EnglishStill takes a lot of practice to use them

But, also give programmers lots of power Important when efficiency is criticalBut introduces many potential errors

Page 5: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class
Page 6: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

High-level Languages

Examples include SQL, and spreadsheet commandsEasier to read & write than low level languagesLess powerful, but also less prone to errors

Most programs written in high-level languagesEnvironment that is most often used for

programming: Microsoft Excel

Page 7: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Palgo

Programming language we will useDeveloped and implemented by Dr. MeyerLink to PALGO on class webpageCan be run off of web or download and run on

individual machine

Page 8: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Programming Experience

Good news! You already have programming experienceWorked with algorithms since beginning of

classSpreadsheets and SQL queries are examples

of writing computer programsThis provides you a good base for this

discussion

Page 9: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Starting Palgo Web page gets filled with a grid Palgo editing window also pops up

Page 10: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Writing Your First Program

Traditional first program is “Hello World”Prints “Hello World” onto screenAnother example of wacky computer

programmers In Palgo, we enter into editing window:

print “Hello World”

… and then hit the button labeled “Run”

Page 11: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello World

Page 12: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Is That All There Is?

Programs can be longer than 1 command Otherwise, Palgo would be really boring…

When there are multiple lines* Palgo 1st executes first line of the program… … then executes program’s 2nd line … and then executes the 3rd line … and continues to execute line-by-line until it

reaches the end of the program

* While true for simple programs, later we will discuss how to make Palgo do more

Page 13: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friends

Suppose you only want to say hello to specific people (You really should get out more and be more

friendly, but…) In Palgo, we enter into editing window:

print “Hello Ishkibble”print “Hello Nevada”

… and again hit “Run”

Page 14: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friends

Page 15: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friend

Decide to get out more and say hello to the user(Now need to meet a nice girl, settle down,

and… sorry, channeling my mother) First, need to get user’s name

To do this we will need to use the input function defined by Palgo

Page 16: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Functions

You have already used a number of functions e.g., … within truth tablese.g., Min, Max, Sum… in spreadsheetsWrite function name, a left parenthesis the

function inputs, and a right parenthesis:Min(A2, A3, A4) or Average(A4:A9)

When evaluated, we replace them with their actual value

( ),( )a b c d

Page 17: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

input()

Trying to say hello to the user First need to get their name Do this by using Palgo’s input function:

input(“Tell me your name”)

When Palgo executes function:Prints the messageReturns the user’s reply as the result

Page 18: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friend

Text specified in input commandSpace for the user to enter input

Page 19: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Variables

Can use input to get user’s name, but how can we use this? We need to save this value for later use To do this we will use a variable

Getting user’s name and storing it in a variable: user = input(“Tell me your name”)

After executing this line, whatever the user types will be stored in a variable named user

Page 20: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Variables

Every variable in Palgo must have a nameNames start with a letter and then have any

letters, numbers, or underscores (“_”) Example variable names:

user, name, Foo, Bar, foo_bar, jamesBond007, peer2peer, r, thisIsALongNameContaining34Letters

Page 21: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Variables

Variable is created the first time program assigns it a valueAssign variable a value using the equals signFor example, following line creates “user”:

user = input(“Tell me your name”)Variable can have only 1 value at a timeSo variable’s will store only the newest value if

it is assigned a new value later in the program

Page 22: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Variables Palgo uses variable’s value whenever it executes a

line using the variableSimilar to spreadsheet using cell’s value whenever it sees

a cell name Palgo does NOT reassign variables’ values like a

spreadsheetPalgo ONLY changes variable’s value during assignmentsPalgo executes programs line-by-line* and will not

automatically propagate changes

Page 23: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friend

Can use print to say “Hello” to the userSo our final program is:name = input(“Tell me your name”)print “Hello ” + name

Page 24: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friend

“Prof. Hertz” is here since that is value of user

Page 25: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Data Type

All data objects define the type of data “One”, “1”, 1, and 1.0 are not equal

“One”, “1” are Strings Strings contain anything inside quotes

“Bob is my #1 uncle” “Prof. Hertz is the greatest teacher EVER!”

1 is an int ints are positive and negative whole numbers

0, -1, 1, 64, -483291, 234897432 are all ints

Page 26: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Other Palgo Data Types

“One”, “1”, 1, and 1.0 are not equal1.0 is a realreals include numbers with a decimal

1.2, 456.787, -19321732.131321, 0.00000000001

Whenever we reassign a variable, we will also reassign its data type

It is not always easy to convert between the different data types

Page 27: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Palgo Operations

Palgo does variety of mathematical functions+, -, *, / will add, subtract, multiply, or

divide two numbersDoes not matter whether or not the

numbers are stored in a variable

Page 28: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Program Trace

Trace programs to see what it will do or why it did something Important for understanding new code, fixing

non-working code, and getting good exam grades

Shows how Palgo executes program step-by-step

Shows the line being executed, the value assigned to any variable, and other important information

Page 29: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Program Trace

1 x = 4 + 22 y = 8 * 13 z = y – 34 x = x + 15 z = y - x6 y = y / 27 z = “x”8 y = z + “b”

Line# x y z

Page 30: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Program Trace

1 x = 4 + 22 y = 8 * 13 z = y – 34 x = x + 15 z = y - x6 y = y / 27 z = “x”8 y = z + “b”

Line# x y z

1 6

Line# x y z

2

Line# x y z

2 8

3

Line# x y z

2 8

3 5

4

Line# x y z

7

5

Line# x y z

1

6

Line# x y z

6 4

7

Line# x y z

x

8

Line# x y z

xb

Page 31: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friends

What if one or two people are using the computer?Want to say hello to either the single user or both

usersTo do this we first need to ask how many people

are at the computernum = input_number("Are there 1 or 2?")

input_number is like input, but returns a number

Page 32: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

if---then---else---end

num = input_number("Are there 1 or 2?")

Program differs depending on num. users We use if—then—else—end to do this

if—then tests boolean (true or false) expression If true, Palgo executes code between then and else

Otherwise, Palgo executes code between else and end

Palgo will then continue execution after end (Part of how Palgo does not always execute line-

by-line)

Page 33: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Palgo Operators

Some operators compute boolean values== compares if two elements are equal != compares if two elements are NOT equal>, <, >=, & <= do the obvious comparisonsa && b returns true if a AND b are truea || b returns true if either a OR b are true

Page 34: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

if---then--else

num = input_number("Are there 1 or 2?")if num == 1 then user = input("What is your name?") print "Hello " + userelse user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + user + " & " + user2end

Palgo checks if value num variable is equal to 1

Page 35: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

if---then--else

num = input_number("Are there 1 or 2?")if num == 1 then user = input("What is your name?") print "Hello " + nameelse user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + name + " & " + name2end

If in this execution the value of num is 1, Palgo will run these statements

Page 36: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

if---then--else

num = input_number("Are there 1 or 2?")if num == 1 then user = input("What is your name?") print "Hello " + nameelse user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + name + " & " + name2end

If the value of num is not 1, Palgo runs these statements

Page 37: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

if---then--else

num = input_number("Are there 1 or 2?")if num == 1 then user = input("What is your name?") print "Hello " + nameelse user = input("What is the first name?") user2=input("What is the second name?") print "Hello " + name + " & " + name2end The end keyword marks where

Palgo should again execute the program as normal

Page 38: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friends

Page 39: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friends

Page 40: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

Hello Friends

What happened?

Page 41: CSC 110 - Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class

For Next Lecture

Finish reading about Palgo Study for Quiz #3 Play around with Palgo

Run the demo programsTry writing something on your ownPaint a picture for someone special