Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris

Similar presentations


Presentation on theme: "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris"— Presentation transcript:

1 COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris jfparis@uh.edu

2 CHAPTER II COMPUTING

3 Chapter overview How computers work  Hardware  Software Algorithms and Heuristics  Algorithmic thinking

4 HOW COMPUTERS WORK

5 Overall organization CPU MAIN MEMORY programs with their data Hard disk User inputs and outputs

6 Inside the main memory Running programs: we call them "processes" KERNEL allocates memory to processes grants CPU to processes control disk accesses Operate in user mode Operates in kernel mode

7 The running programs Reside in main memory  while they are running Include many background processes  We do not see them  Take space and often CPU time Having a large main memory allows us to run more programs at the same time

8 TIP If your computer becomes slow whenever you switch among programs  You need more memory If your computer takes a lot of time to boot  You could have a slow disk  Your OS has a lot of things to load into main memory Useful and not so useful

9 The kernel Responsible for  Managing the resources Which process should get the CPU How our files are stored …  Enforcing security and preventing crashes

10 Security issues Must protect running programs from attempts to modify them by other programs  Mostly programming issues  Also viruses Must protect our data on disk  Especially if computer is shared

11 Running a program OS creates a process  Allocates memory space to the process  Disk copy of program is brought from disk into main memory Process competes with other processes for CPU time Process is deleted when program terminates

12 Saving the results Normally done by saving the results in a file stored on disk Can print them later

13 What is inside a program? Instructions:  Telling the CPU what to do Constants:  Stable values Variables: Memory locations where results can be stored

14 CHAPTER III ALGORITHMS Work in progress: Will add new material s

15 What is an algorithm? “Effective method expressed as a finite list of well-defined instructions for calculating a function”  Wikipedia

16 Three important points It must be an effective method  Guaranteed to produce the result The instructions should be well-defined  Anybody using the algorithm should obtain the same answer It should have a finite number of steps  An algorithm must terminate after a finite number of finite steps

17 These are not algorithms On a shampoo bottle:  Lather  Rinse  Repeat

18 These are not algorithms On a shampoo bottle:  Lather  Rinse  Repeat How many times?

19 These are not algorithms On fuel tank cap:  Turn until three o'clock

20 These are not algorithms On fuel tank cap:  Turn until three o'clock That could be a long time!

21 Example: Converting C into F If you travel outside of the US, temperatures are likely to be given in Celsius not Fahrenheit. How the scales differ:  In Fahrenheit: Water freezes at 32 F and boils at 212 F  In Celsius: Water freezes at 0 C and boils at 100 C

22 Example: Converting C into F 1. Read Celsius temperature x 2. Multiply by 1.8 3. Add 32 to obtain Fahrenheit temperature y

23 Example: Converting C into F Another way to do it: 1. Read Celsius temperature x 2. Fahrenheit temperature y = 1.8×x +32

24 Counter-example (I) British weatherman's rule of thumb:  Multiply C temperature by 2  Add 30 Very good for temperatures in 41-59 F range During a Texas summer, better use:  Multiply C temperature by 2  Add 25

25 Counter-example (II) These two rules are heuristics, not algorithms  Do not always give the right conversion  Still useful Double and add 25 rule converts 30 C into 85 F Right answer is 86 F

26 A program is not algorithm It is the expression of an algorithm in a programming language Picking the right algorithm is the most important task  After that, we just have to code!

27 Example Finding a name in a table Naïve solution is sequential search Binary search is much faster

28 Sequential search (I) We look for search_name in list list 1. Start at beginning of list 2. If list is empty stop and return NOT FOUND 3. If search_name matches name of first list entry stop and return list entry 4. If we have reached the end of the list stop and return NOT FOUND

29 Sequential search (II) 5. Look for next list entry 6. If search_name matches name of next list entry stop and return list entry 7. Go to step 4

30 Binary search (I) We look for search_name in list list 1. If list is empty stop and return NOT FOUND 2. Find entry exactly in middle of list 3. If search_name matches the name of that entry stop and return list entry

31 Binary search (II) 4. If search_name goes before the name of entry restart search for first half of list 5. If search_name goes after the name of entry restart search for second half of list

32 Example of binary search List contains Alan Alice Barbara Emily Francis Gina Peter

33 Example of binary search We look for Charles in a sorted list of names Alan Alice Barbara Emily Francis Gina Peter

34 Example of binary search We compare search name with entry exactly in the middle of the list (Emily) Alan Alice Barbara Emily Francis Gina Peter

35 Example of binary search Since Charles comes before Emily we can eliminate second half of list Alan Alice Barbara

36 Example of binary search We compare search name with entry exactly in the middle of the list (Alice) Alan Alice Barbara

37 Example of binary search Since Charles comes after Alice we can eliminate the first half of list Barbara

38 Example of binary search We compare search name with entry exactly in the middle of the list (Barbara) Barbara

39 Example of binary search Since Charles comes after Alice we can eliminate the first half of the list Barbara

40 Example of binary search Since Charles comes before Barbara we can eliminate one half of the list

41 Example of binary search Since list to be searched is now empty we conclude that the list does not contain Charles

42 Comparing performances List with 1024 entries Sequential search:  Maximum number of steps: 1024  Average number of steps: 512 (one half) Binary search:  Number of steps: 10 (= log 2 1024)

43 Heuristics (I) Many problems have no practical algorithmic solution  Algorithm will take too long Example:  Finding the absolute best price for an item  Should check everywhere  Not cost-effective

44 Heuristics (I) Heuristics provide solutions  That are not guaranteed to work in all cases  That provide a good approximation of the correct solution Example:  When we want to buy an item, we focus on the stores that are likely to sell the item at a good price

45 An example Finding the maximum of a curve  Start at any given point  Move on the curve following the upward direction  Stop when the curve reaches a start going downward

46 It works

47 It does not always work

48 Which one is the most useful? ALGORITHM Always provides the right answer Can be very slow HEURISTICS Normally provide a good approximation of the right answer Relatively fast

49 Algorithmic thinking Way to analyze problems and come with one or more algorithmic solutions that  fully describe the solution  handle all special cases  can implemented on a computer system  will run at a reasonable cost Most important skill for a programmer  Can be learned


Download ppt "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris"

Similar presentations


Ads by Google