CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design
Reading Assignment 2 Chapter 2 in Textbook Homework Assignment 2 Due September 11, beginning of class Page 75, # 3,7,11,15,18,19,20 Also: Design an algorithm to always beat me in the game 21.
An Algorithm is A well-ordered collection of Unambiguous and Effectively computable operations that, when executed Produces a result and Halts in a finite amount of time Textbook definition
Summary of Operation Types for Algorithms Sequential operations to carry out computation, input, and output. Conditional operations. Iterative operations.
There is a theorem in theoretical computer science that proves that these operations are sufficient to represent ANY algorithm! Algorithms are used to do everything you see on a computer! Do word processing. Fly NASA probes to the planets. Run the international telephone switching system. Create CAT scan images. Process your pay checks. Run computer games. Etc. Powerful Algorithms!!
EXAMPLE OF AN ALGORITHM PROBLEM: Start with a collection of names N 1, N 2,..., N 10000, and corresponding telephone numbers T 1, T 2,..., T Given a name, Name, find and print the telephone number for that name if a match on an N i occurs; otherwise, print "Not Found". Given a problem, there are often many ways to provide an algorithm for solving the problem. Note: You must understand the methodology for solving the problem in order to write an algorithm for the solution!!!
First Try 1. Get values for N 1, N 2,..., N 10000, T 1, T 2,,,,, T 10000, and Name. 2. If Name is N 1, then print T 1 Stop 3. If Name is N 2, then print T 2. Stop. {a lot of tedious writing here that is being skipped} If Name is N 10000, then print T Stop Print "Not found" Stop.
Second Try 1. Get values for N 1, N 2,..., N 10000, T 1, T 2,,,,, T 10000, and Name. 2. Set the value of i to 1 and the value of Found to NO. 3. While Found is NO 4.If Name is equal to N i, then 5.Print the telephone number T i 6. Set the value of Found to Yes Else 7.Add 1 to the value of i 8. Stop.
Third Try 1. Get values for N 1, N 2,..., N 10000, T 1, T 2,,,,, T 10000, and Name. 2. Set the value of i to 1 and the value of Found to NO. 3. While Found is NO and i <= If Name is equal to N i, then 5.Print the telephone number T i 6. Set the value of Found to Yes Else 7.Add 1 to the value of i 8. If (Found is No) then 9.Print "Not found" 10. Stop.
Game Time Who can beat me in the game 21? Objective: Get your opponent to say the number 21. Rules: One player goes first starting counting from 1. On each turn a player may say one or two consecutive numbers. You must begin where your opponent left off. The first person to say the number 21 loses. There is a way to always beat me!