Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm = Sequence of simple steps, combined with decisions and loops.
Computer Science
Hardware
+ Software
+ Algorithms
COSC 1100 Freshman Seminar Fall 2017
Remember from the Transition Week!
Algorithm = Sequence of simple steps, combined with decisions and loops.
COSC 1100 Freshman Seminar Fall 2017
COSC 1100 Freshman Seminar Fall 2017
Remember from the Transition Week!
We presented algorithms for:
• Performing simple arithmetic (multiply by 9 and 11, divide or multiply by 5)
• River-crossing (Wolf-Goat-Cabbage, Light-Medium-Heavy, Missionaries and Cannibals)
• Building hardware (adders)
• Adding a sequence of numbers (until a negative one is encountered)
COSC 1100 Freshman Seminar Fall 2017
Remember from the Transition Week!
All algorithms are about
breaking a problem into sub-problems
When do we stop in the “breaking” process?
When the sub-problems are simple enough for us to solve!
Algorithm for extracting parts of a string
Given a string, say Computer Science, let’s assume that we know how to split it into two parts:
• The first character, a.k.a. the head
• All the remaining characters, a.k.a. the body
COSC 1100 Freshman Seminar Fall 2017
We express these basic operations with functions
head(‘Computer science’) = ‘C’
body(‘Computer science’) = ‘omputer Science’
COSC 1100 Freshman Seminar Fall 2017
head and body are the sub-problems! (simple enough for us to solve)
Devise an algorithm to extract the secondcharacter of the string!
COSC 1100 Freshman Seminar Fall 2017
head(‘Computer science’) = ‘C’
body(‘Computer science’) = ‘omputer Science’
Devise an algorithm to extract the secondcharacter of the string!
COSC 1100 Freshman Seminar Fall 2017
head(‘Computer science’) = ‘C’
body(‘Computer science’) = ‘omputer Science’
head(body(‘Computer Science’)) = ‘o’
Devise an algorithm to extract all but the firsttwo characters of the string!
COSC 1100 Freshman Seminar Fall 2017
head(‘Computer science’) = ‘C’
body(‘Computer science’) = ‘omputer Science’
Devise an algorithm to extract the secondcharacter of the string!
COSC 1100 Freshman Seminar Fall 2017
head(‘Computer science’) = ‘C’
body(‘Computer science’) = ‘omputer Science’
body(body(‘Computer Science’))
= ‘mputer Science’
Devise an algorithm to extract the third character of the string!
The fourth?
All but the first three?
All but the first four?
Etc.
COSC 1100 Freshman Seminar Fall 2017
head(‘Computer science’) = ‘C’
body(‘Computer science’) = ‘omputer Science’
For individual work (do not turn in!)
A: We can do it provided we know the length of the string!
COSC 1100 Freshman Seminar Fall 2017
Food for thought:
What if we need to extract the last character in the string?
head(body(‘Computer Science’)) = ‘o’
Do you remember the function len from Python?
A: To save us a lot of writing, we need a loop!
COSC 1100 Freshman Seminar Fall 2017
Food for thought:
What if we need to extract the 1000th character in a (long) string?
head(body(‘Computer Science’)) = ‘o’
Algorithm for buying a car
Car 1
Cost: $35,000
MPG: 45
Car 2
Cost: $10,000
MPG: 10
Which car is best?
a. Car 1 d. Not enough information
b. Car 2
c. Same
Inspired by : Udacity – Introduction to ProgrammingCOSC 1100 Freshman Seminar Fall 2017
Algorithm for buying a car
Car 1
Cost: $35,000
MPG: 45
Car 2
Cost: $10,000
MPG: 10
Which car is best? – Best for what/whom?
a. Car 1 d. Not enough information
b. Car 2
c. Same
Inspired by : Udacity – Introduction to ProgrammingCOSC 1100 Freshman Seminar Fall 2017
Algorithm for buying a car
Car 1
Cost: $35,000
MPG: 45
Car 2
Cost: $10,000
MPG: 10
Which car is best for minimizing total owner’s cost ?
a. Car 1 d. Not enough information
b. Car 2
c. Same
COSC 1100 Freshman Seminar Fall 2017
Algorithm for buying a car
Car 1
Cost: $35,000
MPG: 45
Car 2
Cost: $10,000
MPG: 10
Which car is best for minimizing total owner’s cost ?
a. Car 1 d. Not enough information
b. Car 2
c. Same
How many miles will I drive?Repairs?Insurance?Resell value?Environmental impact?
Algorithm for buying a car
Car 1
Cost: $35,000
MPG: 45
Car 2
Cost: $10,000
MPG: 10
All we care about is the gas price!
Total miles to drive: 60,000
Average cost of gasoline: $3.10/gal
COSC 1100 Freshman Seminar Fall 2017
Car 1
Cost: $35,000
MPG: 45
Car 2
Cost: $10,000
MPG: 10
1. Calculate cost for each car.
2. Choose the one with lower cost.
miles: 60,000 gas price: $3.10/gal
COSC 1100 Freshman Seminar Fall 2017
Use Python!
COSC 1100 Freshman Seminar Fall 2017
winner
Use Python!
COSC 1100 Freshman Seminar Fall 2017
winner
Use Python to make the calculation for the current price of gas in S’ville!
COSC 1100 Freshman Seminar Fall 2017
To do for next time (do not turn in!)
Let’s assume that my algebra is really bad
COSC 1100 Freshman Seminar Fall 2017
1. Ask a friend for formula.
2. If friend doesn’t know, GO TO step 1.
3. Plug in numbers for each car.
4. Choose car with lower cost.
Car 1
Cost: $35,000
MPG: 45
Car 2
Cost: $10,000
MPG: 10
miles: 60,000 gas price: $3.10/gal
COSC 1100 Freshman Seminar Fall 2017
What’s wrong with this algorithm?
What’s wrong with this algorithm?
1. Ask a friend for formula.
2. If friend doesn’t know, GO TO step 1.
3. Plug in numbers for each car.
4. Choose car with lower cost.
Car 1
Cost: $35,000
MPG: 45
Car 2
Cost: $10,000
MPG: 10
miles: 60,000 gas price: $3.10/gal
COSC 1100 Freshman Seminar Fall 2017
Still, this loop could take forever to complete!
What if I run out of
friends?
Find new friends?
An algorithm must:
1. Be guaranteed to terminate.
2. Consume finite resources.
3. Be unambiguous, i.e. precise enough to be implemented (in a programming language).
COSC 1100 Freshman Seminar Fall 2017
An algorithm must:
1. Be guaranteed to terminate in “reasonable” time.
2. Consume finite and “reasonable” resources.
3. Be unambiguous, i.e. precise enough to be implemented (in a programming language).
From a practical perspective, we have additional constraints!
COSC 1100 Freshman Seminar Fall 2017
Example!
Example!
1. Ask a friend for formula.
2. If friend doesn’t know, GO TO step 1.
3. Plug in numbers for each car.
4. Choose car with lower cost.
Hint: Ask at most 5 friends!
How would you change this algorithm so it’s guaranteed to terminate?
COSC 1100 Freshman Seminar Fall 2017
1. Ask a friend for formula.
2. If friend doesn’t know and I asked less than 5 friends
1. GO TO step 1.
3. If # of friends asked is less or equal to 5
1. Plug in numbers for each car.
2. Choose car with lower cost.
4. Else
1. Give up
COSC 1100 Freshman Seminar Fall 2017
Is this unambiguous/precise enough?
1. Ask a friend for formula.
2. If friend doesn’t know and I asked less than 5 friends
1. GO TO step 1.
3. If # of friends asked is less or equal to 5
1. Plug in numbers for each car.
2. Choose car with lower cost.
4. Else
1. Give up
COSC 1100 Freshman Seminar Fall 2017
Add steps to keep track of the number of friends we have asked
Hint: Add a variable nr_friends
1. Ask a friend for formula.
2. If friend doesn’t know and I asked less than 5 friends
1. GO TO step 1.
3. If # of friends asked is less or equal to 5
1. Plug in numbers for each car.
2. Choose car with lower cost.
4. Else
1. Give up
COSC 1100 Freshman Seminar Fall 2017
Algorithm for buying a car
1. nr_friends = 0
2. Ask a friend for formula.
3. nr_friends = nr_friends + 1
4. If friend doesn’t know and nr_friends < 5
1. GO TO step 2.
5. If nr_friends ≤ 5
1. Plug in numbers for each car.
2. Choose car with lower cost.
4. Else
1. Give up
COSC 1100 Freshman Seminar Fall 2017
Algorithm for buying a car
1. nr_friends = 0
2. Ask a friend for formula.
3. nr_friends = nr_friends + 1
4. If friend doesn’t know and nr_friends ≤ 5
1. GO TO step 1.
5. If nr_friends ≤ 5
1. Plug in numbers for each car.
2. Choose car with lower cost.
4. Else
1. Give up
Why do we need to ask this again
here?
COSC 1100 Freshman Seminar Fall 2017
Conclusion
• Developing an algorithm is hard work!
• We call this algorithm design.
• It is not uncommon for the algorithm design phase to account for 50% or more of the total time needed for a computer project!
EOL1COSC 1100 Freshman Seminar Fall 2017
What are the three conditions any algorithm must satisfy?
COSC 1100 Freshman Seminar Fall 2017
QUIZ
An algorithm must:
1. Be guaranteed to terminate.
2. Consume finite resources.
3. Be unambiguous, i.e. precise enough to be implemented (in a programming language).
COSC 1100 Freshman Seminar Fall 2017
A better algorithm for buying a car
COSC 1100 Freshman Seminar Fall 2017
Is this a valid algorithm?
1. For each car:
1. gas_cost = miles/mpg*gallon_price
2. total_cost = gas_cost + purchase_cost
2. If total_cost1 < total_cost2
1. Buy car 1
3. Else
1. Buy car 2
COSC 1100 Freshman Seminar Fall 2017
Is this a valid algorithm?
1. For each car:
1. gas_cost = miles/mpg*gallon_price
2. total_cost = gas_cost + purchase_cost
2. If total_cost1 < total_cost2
1. Buy car 1
3. Else
1. Buy car 2
No, it’s not precise enough: the variables have not been initialized!
COSC 1100 Freshman Seminar Fall 2017
1. gallon_price = 3.10
2. miles = 60000
3. mpg1 = 45, mpg2 = 10
4. purchase_cost1 = 35000, purchase_cost2 = 10000
5. For each car:
1. gas_cost = miles/mpg*gallon_price
2. total_cost = gas_cost + purchase_cost
6. If total_cost1 < total_cost2
1. Output “Buy car 1”
7. Else
1. Output “Buy car 2”
Now we can follow the algorithm and decide!
COSC 1100 Freshman Seminar Fall 2017
Conclusion: Decision-making statements (a.k.a. if or conditional) enable algorithms to perform different operations, based on the input data.
COSC 1100 Freshman Seminar Fall 2017
Without the ability to make decisions, computers are just calculators!
Source: "FX-77" by Sergei Frolov, Soviet Calculators Collection - Own work. Licensed under Public domain via Wikimedia Commons http://commons.wikimedia.org/wiki/File:FX-77.JPG#mediaviewer/File:FX-77.JPG
COSC 1100 Freshman Seminar Fall 2017
The presence of conditional statements is what makes a computer language a programming language (e.g., HTML, XML and P3P are not programming languages)
COSC 1100 Freshman Seminar Fall 2017
Source: AP CS Principles – Course and Exam DescriptionsCOSC 1100 Freshman Seminar Fall 2017
Flowchart Symbols
Flowchart Rules
• Always have a Start and a Stop
• Blocks are connected with lines and arrows indicating the direction of process flow
• Try to orient all flows top to bottom or left to right
Input/Output
Process stepor instruction
Conditional Test(decision)
Start/Stop
COSC 1100 Freshman Seminar Fall 2017
Algorithms are built from three basic structures
• Sequential Actions
– Series of actions performed in the same order
• Decision (Conditional, or Selection)
– Two possible outcomes; one is selected based on a condition
• Loop (Repetition)
– An action is repeated until a condition is met
Nesting: Any of the structures can be inside any other, on multiple levels!
COSC 1100 Freshman Seminar Fall 2017
Building blocks for algorithms
COSC 1100 Freshman Seminar Fall 2017
Examples of sequential algorithms
The algorithm always goes through exactly the same steps, no matter what inputs are provided
COSC 1100 Freshman Seminar Fall 2017
Follow this algorithm, assuming the values input are x = 3 and y = 4. What is the output?
PseudocodeFlowchart
COSC 1100 Freshman Seminar Fall 2017
Devise a similar algorithm to find the volume of a rectangular box. (pseudocode and flowchart)
COSC 1100 Freshman Seminar Fall 2017
Draw the flowchart for this pseudocode:
COSC 1100 Freshman Seminar Fall 2017
COSC 1100 Freshman Seminar Fall 2017
Examples of algorithms with selection/decision
The algorithm branches into alternate paths.
Each time it’s executed, the concrete path is selected based on the inputs provided.
COSC 1100 Freshman Seminar Fall 2017
COSC 1100 Freshman Seminar Fall 2017Source: http://cs.iupui.edu/~telliott/n100/images/flowChart.html
COSC 1100 Freshman Seminar Fall 2017Source: http://www.the-diy-income-investor.com/2011_11_01_archive.html
Exchange-Traded Funds
What conditions must be true in order to pay down debts?
COSC 1100 Freshman Seminar Fall 2017Source: http://www.the-diy-income-investor.com/2011_11_01_archive.html
Exchange-Traded Funds
What conditions must be true in order to invest in ETFs?
Examples of algorithms with loops
The algorithm can “move backwards” and repeat the same steps multiple times.
COSC 1100 Freshman Seminar Fall 2017
Explain in your own words what this algorithm does
COSC 1100 Freshman Seminar Fall 2017
Modify the algorithm to count backwardsfrom 42 to 30 (inclusive!)
COSC 1100 Freshman Seminar Fall 2017
1. gallon_price = 3.10
2. mpg1 = 45, mpg2 = 10
3. miles = 60000
4. purchase_cost1 = 35000, purchase_cost2 = 10000
5. For each car:
1. gas_cost = miles/mpg*gallon_price
2. total_cost = gas_cost + purchase_cost
6. If total_cost1 < total_cost2
1. Output “Buy car 1”
7. Else
1. Output “Buy car 2”
Is there a loop in this algorithm?
COSC 1100 Freshman Seminar Fall 2017
COSC 1100 Freshman Seminar Fall 2017
Explain in your own words what this algorithm does
Handout
Source: AP CS Principles – Course and Exam DescriptionsCOSC 1100 Freshman Seminar Fall 2017