1 / 18

Chapter 6

Algorithmic Problem Solving. Chapter 6. Algorithm. Algorithm : a well defined computational procedure consisting of a set of instructions , that takes some value(s) as input , and produces some value(s), as output. Al-Khowarizmi  Algorismus  Algorithm. Input. Algorithm. Output.

cosima
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithmic Problem Solving Chapter 6 Chapter 6: Algorithmic Problem Solving

  2. Algorithm Algorithm: a well defined computational procedure consisting of a set of instructions, that takes some value(s) as input, and produces some value(s), as output. Al-Khowarizmi  Algorismus  Algorithm Algorithm

  3. Input Algorithm Output Algorithm Algorithm

  4. Algorithm embeds logic of solution. Algorithm is converted to program. Algorithmic problem solving: writing an algorithm -- tough translate algorithm to code -- easy Understanding the problem Writing an algorithm Verifying the algorithm Converting to code Algorithm Algorithm

  5. Software: algorithms, programs. Hardware: computers (CPU, disk drive, keyboard, etc.) ingredients recipe (software) Cooking utensils (hardware) : bah kut teh Software and hardware Software and hardware

  6. First documented algorithm by Euclid (300 B.C.) to compute greatest common divisor (gcd). Examples: gcd(3,21) = 3; gcd(15,40) = 5. Euclidean algorithm 1. Let A and B be integers with A > B 0. 2. If B = 0, then the gcd is A and the algorithm ends. 3. Otherwise, find q and r such that A = qB + r where 0 r < B Note that we have 0 r < B < A and gcd(A,B) = gcd(B,r). Replace A by B, B by r. Go to step 2. Euclidean algorithm

  7. Walk through the algorithm with examples: A = 40, B =15. Euclidean algorithm A = 2B + 10 A = 15 ; B = 10 A = 1B + 5 A = 10 ; B = 5 A = 2B + 0 A = 5 ; B = 0 gcd is 5 1. Let A and B be integers with A > B 0. 2. If B = 0, then the gcd is A and the algorithm ends. 3. Otherwise, find q and r such that A = qB + r where 0 r < B Note that we have 0 r < B < A and gcd(A,B) = gcd(B,r). Replace A by B, B by r. Go to step 2. Euclidean algorithm

  8. Data types: integer, real number, character, Boolean, pointer, etc. Examples: 32, 0.0264, 'a', true. Data structures: arrays, records, files, etc. Program = Algorithm + Data Structures Data are stored in variables in a program. Variables take up memory space in the computer. Data types and structures Data types and structures

  9. A sample C program /* A simple C program to read a number & compute and display its square by using the multiplication (*) operator. */ #include <stdio.h> main () /* a C program always has a main function */ { int n; /* this declares an integer variable 'n' */ printf ("\nEnter the number to be squared: "); scanf ("%d", &n); printf ("Using the * operator, the square of %d is %d.\n\n", n, n*n); } A C program A C program

  10. Each step must be exact. Must terminate. Must be effective. Must be general. Characteristics of an algorithm Characteristics of an algorithm

  11. How to represent an algorithm? Pseudo-code Flowchart Pseudo-code: a combination of English text, mathematical notations, and keywords from the programming language. Pseudo-code Pseudo-code

  12. To find average, min and max among a list. Pseudo-code First, you initialise sum to zero, min to a very big number, and max to a very small number. Then, you enter the numbers, one by one. For each number that you have entered, assign it to num and add it to the sum. At the same time, you compare num with min, if num is smaller than min, let min be num instead. Similarly, you compare num with max, if num is larger than max, let max be num instead. After all the numbers have been entered, you divide sum by the numbers of items entered, and let ave be this result. End of algorithm. Pseudo-code

  13. To find average, min and max among a list. Pseudo-code sumcount  0 { sum = sum of numbers; count = how many numbers are entered? } min ? { min to hold the smallest value eventually } max ? { max to hold the largest value eventually } for each num entered, increment count sumsum + num if num < min then minnum if num > max then maxnum avesum/count Pseudo-code

  14. Diagrammatic form: terminator connector process box decision box Flowchart Pseudo-code

  15. To find minimum and maximum among a list. A Yes end of input? No incrementcount sum sum + num start ave num/count sumcount  0 min  ? max  ? Yes num<min? min num No end A Yes num>max? max num No Flowchart Pseudo-code

  16. Sequence implied; one after another Branching (selection) select alternative path based on condition (true/false) example: if x  0 then a = b/x; if tired then rest else work; Control structures Control structures

  17. Loop (repetition) bounded loop: for x = 1 to 10 do { statements }; unbounded loop: while (condition) do { statements }; repeat/iterate statements in loop body until condition changes must make sure loop terminates Control structures Control structures

  18. Try exercises behind chapter 6. Homework Homework

More Related