Chapter 6 Algorithmic Problem Solving Chapter 6 Algorithmic
Chapter 6 Algorithmic Problem Solving Chapter 6: Algorithmic Problem Solving 1
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 Chapter 6 Algorithm 2
Algorithm Input Chapter 6 Algorithm Output 3
Algorithm q Algorithm embeds logic of solution. Understanding the problem q Algorithm is converted to program. q Algorithmic problem solving: uwriting an algorithm -- tough utranslate algorithm to code -- easy Writing an algorithm Verifying the algorithm Converting to code Chapter 6 Algorithm 4
Software and hardware q Software: algorithms, programs. q Hardware: computers (CPU, disk drive, keyboard, etc. ) ingredients recipe (software) : Cooking utensils (hardware) bah kut teh Chapter 6 Software and hardware 5
Euclidean algorithm q First documented algorithm by Euclid (300 B. C. ) to compute greatest common divisor (gcd). Examples: gcd(3, 21) = 3; gcd(15, 40) = 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 = q. B + 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. Chapter 6 Euclidean algorithm 6
Euclidean algorithm q Walk through the algorithm with examples: A = 40, B =15. A = 2 B + 10 A = 15 ; B = 10 A = 1 B + 5 A = 2 B + 0 gcd is 5 A = 10 ; B = 5 A=5; B=0 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 = q. B + 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. Chapter 6 Euclidean algorithm 7
Data types and structures q Data types: integer, real number, character, Boolean, pointer, etc. Examples: 32, 0. 0264, 'a', true. q Data structures: arrays, records, files, etc. Program = Algorithm + Data Structures q Data are stored in variables in a program. Variables take up memory space in the computer. Chapter 6 Data types and structures 8
A C program q 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 ("n. Enter the number to be squared: "); scanf ("%d", &n); printf ("Using the * operator, the square of %d is %d. nn", n, n*n); } Chapter 6 A C program 9
Characteristics of an algorithm q Each step must be exact. q Must terminate. q Must be effective. q Must be general. Chapter 6 Characteristics of an algorithm 10
Pseudo-code q How to represent an algorithm? u. Pseudo-code u. Flowchart q Pseudo-code: a combination of English text, mathematical notations, and keywords from the programming language. Chapter 6 Pseudo-code 11
Pseudo-code q To find average, min and max among a list. 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. Chapter 6 Pseudo-code 12
Pseudo-code q To find average, min and max among a list. sum count 0 { sum = sum of numbers; count = how many numbers are entered? } { min to hold the smallest value min ? eventually } max ? { max to hold the largest value eventually } for each num entered, increment count sum + num if num < min then min num if num > max then max num ave sum/count Chapter 6 Pseudo-code 13
Flowchart q Diagrammatic form: uterminator uconnector uprocess box udecision box Chapter 6 Pseudo-code 14
Flowchart A q To find minimum and maximum among a list. Yes No increment count sum + num start sum count 0 min ? max ? end of input? ave num/count num<min? Yes min num No A Chapter 6 end Pseudo-code num>max? Yes No max num 15
Control structures q Sequence uimplied; one after another q Branching (selection) uselect alternative path based on condition (true/false) uexample: if x 0 then a = b/x; if tired then rest else work; Chapter 6 Control structures 16
Control structures q Loop (repetition) ubounded loop: for x = 1 to 10 do { statements }; uunbounded loop: while (condition) do { statements }; urepeat/iterate statements in loop body until condition changes umust make sure loop terminates Chapter 6 Control structures 17
Homework Try exercises behind chapter 6. Chapter 6 Homework 18
- Slides: 18