Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design.

Similar presentations


Presentation on theme: "CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design."— Presentation transcript:

1 CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design

2 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.

3 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

4 Summary of Operation Types for Algorithms Sequential operations to carry out computation, input, and output. Conditional operations. Iterative operations.

5 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!!

6 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 10000. 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!!!

7 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} 10001. If Name is N 10000, then print T 10000. Stop. 10002. Print "Not found" 10003. Stop.

8 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.

9 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 <= 10000 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. If (Found is No) then 9.Print "Not found" 10. Stop.

10 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!


Download ppt "CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design."

Similar presentations


Ads by Google