Download presentation

Presentation is loading. Please wait.

Published byRoxanne Barnett Modified over 2 years ago

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

2
Chapter 6Algorithm2 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

3
Chapter 6Algorithm3 InputOutputAlgorithm

4
Chapter 6Algorithm4 Understanding the problem Writing an algorithm Converting to code Verifying the algorithm qAlgorithm embeds logic of solution. qAlgorithm is converted to program. qAlgorithmic problem solving: uwriting an algorithm -- tough utranslate algorithm to code -- easy

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

6
Chapter 6Euclidean algorithm6 qFirst 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 = 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.

7
Chapter 6Euclidean algorithm7 qWalk through the algorithm with examples: A = 40, B =15. 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.

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

9
Chapter 6A C program9 qA sample C program /* A simple C program to read a number & compute and display its square by using the multiplication (*) operator. */ #include 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); }

10
Chapter 6Characteristics of an algorithm10 Characteristics of an algorithm qEach step must be exact. qMust terminate. qMust be effective. qMust be general.

11
Chapter 6Pseudo-code11 Pseudo-code qHow to represent an algorithm? uPseudo-code uFlowchart qPseudo-code: a combination of English text, mathematical notations, and keywords from the programming language.

12
Chapter 6Pseudo-code12 Pseudo-code qTo 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.

13
Chapter 6Pseudo-code13 Pseudo-code qTo find average, min and max among a list. 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

14
Chapter 6Pseudo-code14 Flowchart qDiagrammatic form: uterminator uconnector uprocess box udecision box

15
Chapter 6Pseudo-code15 Flowchart qTo find minimum and maximum among a list. start sum count 0 min ? max ? end of input? increment count sum sum + num num

16
Chapter 6Control structures16 Control structures qSequence uimplied; one after another qBranching (selection) uselect alternative path based on condition (true/false) uexample: if x 0 then a = b/x; if tired then rest else work;

17
Chapter 6Control structures17 Control structures qLoop (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

18
Chapter 6Homework18 Homework Try exercises behind chapter 6.

Similar presentations

OK

Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.

Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on transportation of substances in plants and animals Ppt on types of production process Maths ppt on surface area and volume Ppt on different types of computer softwares list File type ppt online voting system Ppt on windows 7 features Ppt on beer lambert law definition Ppt on bank lending rate Ppt on atrial septal defect definition Ppt on heritage and culture of rajasthan