Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software +...
Transcript of Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software +...
![Page 1: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/1.jpg)
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.
![Page 2: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/2.jpg)
COSC 1100 Freshman Seminar Fall 2017
![Page 3: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/3.jpg)
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)
![Page 4: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/4.jpg)
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!
![Page 5: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/5.jpg)
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
![Page 6: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/6.jpg)
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)
![Page 7: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/7.jpg)
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’
![Page 8: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/8.jpg)
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’
![Page 9: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/9.jpg)
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’
![Page 10: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/10.jpg)
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’
![Page 11: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/11.jpg)
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!)
![Page 12: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/12.jpg)
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?
![Page 13: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/13.jpg)
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’
![Page 14: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/14.jpg)
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
![Page 15: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/15.jpg)
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
![Page 16: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/16.jpg)
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
![Page 17: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/17.jpg)
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?
![Page 18: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/18.jpg)
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
![Page 19: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/19.jpg)
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
![Page 20: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/20.jpg)
Use Python!
COSC 1100 Freshman Seminar Fall 2017
winner
![Page 21: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/21.jpg)
Use Python!
COSC 1100 Freshman Seminar Fall 2017
winner
![Page 22: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/22.jpg)
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!)
![Page 23: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/23.jpg)
Let’s assume that my algebra is really bad
COSC 1100 Freshman Seminar Fall 2017
![Page 24: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/24.jpg)
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?
![Page 25: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/25.jpg)
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?
![Page 26: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/26.jpg)
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
![Page 27: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/27.jpg)
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!
![Page 28: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/28.jpg)
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
![Page 29: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/29.jpg)
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
![Page 30: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/30.jpg)
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
![Page 31: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/31.jpg)
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
![Page 32: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/32.jpg)
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
![Page 33: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/33.jpg)
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
![Page 34: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/34.jpg)
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
![Page 35: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/35.jpg)
What are the three conditions any algorithm must satisfy?
COSC 1100 Freshman Seminar Fall 2017
QUIZ
![Page 36: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/36.jpg)
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
![Page 37: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/37.jpg)
A better algorithm for buying a car
COSC 1100 Freshman Seminar Fall 2017
![Page 38: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/38.jpg)
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
![Page 39: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/39.jpg)
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
![Page 40: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/40.jpg)
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
![Page 41: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/41.jpg)
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
![Page 42: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/42.jpg)
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
![Page 43: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/43.jpg)
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
![Page 44: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/44.jpg)
Source: AP CS Principles – Course and Exam DescriptionsCOSC 1100 Freshman Seminar Fall 2017
![Page 45: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/45.jpg)
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
![Page 46: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/46.jpg)
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
![Page 47: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/47.jpg)
Building blocks for algorithms
COSC 1100 Freshman Seminar Fall 2017
![Page 48: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/48.jpg)
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
![Page 49: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/49.jpg)
Follow this algorithm, assuming the values input are x = 3 and y = 4. What is the output?
PseudocodeFlowchart
COSC 1100 Freshman Seminar Fall 2017
![Page 50: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/50.jpg)
Devise a similar algorithm to find the volume of a rectangular box. (pseudocode and flowchart)
COSC 1100 Freshman Seminar Fall 2017
![Page 51: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/51.jpg)
Draw the flowchart for this pseudocode:
COSC 1100 Freshman Seminar Fall 2017
![Page 52: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/52.jpg)
COSC 1100 Freshman Seminar Fall 2017
![Page 53: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/53.jpg)
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
![Page 54: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/54.jpg)
COSC 1100 Freshman Seminar Fall 2017Source: http://cs.iupui.edu/~telliott/n100/images/flowChart.html
![Page 55: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/55.jpg)
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?
![Page 56: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/56.jpg)
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?
![Page 57: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/57.jpg)
Examples of algorithms with loops
The algorithm can “move backwards” and repeat the same steps multiple times.
COSC 1100 Freshman Seminar Fall 2017
![Page 58: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/58.jpg)
Explain in your own words what this algorithm does
COSC 1100 Freshman Seminar Fall 2017
![Page 59: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/59.jpg)
Modify the algorithm to count backwardsfrom 42 to 30 (inclusive!)
COSC 1100 Freshman Seminar Fall 2017
![Page 60: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/60.jpg)
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
![Page 61: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/61.jpg)
COSC 1100 Freshman Seminar Fall 2017
Explain in your own words what this algorithm does
![Page 62: Remember from the Transition Week! Hardware Software ...Computer Science Hardware + Software + Algorithms COSC 1100 Freshman Seminar Fall 2017 Remember from the Transition Week! Algorithm](https://reader033.fdocuments.in/reader033/viewer/2022041618/5e3d08954197fd3e5c1d8595/html5/thumbnails/62.jpg)
Handout
Source: AP CS Principles – Course and Exam DescriptionsCOSC 1100 Freshman Seminar Fall 2017