Algorithm Design & Implementation

umairsimjee 4,419 views 19 slides Jan 11, 2016
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

For Algorithm Design & Implementation


Slide Content

Lecture 7 Algorithm Design & Implementation Lecturer: Sumaira Hussain S.M.I University

Algorithm Building Blocks All problems can be solved by employing any one of the following building blocks or their combinations Sequences Conditionals Loops

Sequences A sequence of instructions that are executed in the precise order they are written in: statement block 1 statement block 2 statement block 3 statement block 1 statement block 2 statement block 3

Conditionals Select between alternate courses of action depending upon the evaluation of a condition If ( condition = true ) statement block 1 Else statement block 2 End if statement block 1 condition True False statement block 2

Loops Loop through a set of statements as long as a condition is true Loop while ( condition = true ) statement block End Loop condition True False statement block

example We will first go through the problem statement and then present the algorithm in three different formats: 1. Pseudo code 2. Flowchart 3. Actual code

example Convert a decimal number into binary

Convert 75 to Binary 75 2 37 1 2 18 1 2 9 2 4 1 2 2 2 1 2 1 1001011 remainder

Pseudo Code Let the decimal number be an integer x, x > 0 Let the binary equivalent be an empty string y Repeat while x > 0 { Determine the quotient & remainder of x ÷ 2 y = CONCATENATE( remainder, y ) x = quotient } Print “2”, y Stop

Start Find quotient & remainder of x ÷ 2 Get x x >1 ? Stop y = CONC( x, remainder) x = quotient x is the decimal number y is the binary equivalent Flowchart of Decimal to Binary Conversion Yes No Print “2”, y

Actual code #include< stdio.h > int  main(){     long  int   decimalNumber,remainder,quotient ;      int   binaryNumber [100], i =1,j;     printf ("Enter any decimal number: ");     scanf ("% ld",&decimalNumber );     quotient = decimalNumber ;     while(quotient>1){          binaryNumber [ i ++]= quotient % 2;          quotient = quotient / 2;     }     printf ("Equivalent binary value of decimal number %d: ", decimalNumber );     for(j = i -1 ;j> 0;j--)          printf ("% d",binaryNumber [j]);     return 0; }

12 How to write a good pseudo code Use indention for improved clarity Do not put “code” in pseudo code make your pseudo code language independent Don’t write pseudo code for yourself – write it in an unambiguous fashion so that anyone with a reasonable knowledge can understand and implement it Be consistent Prefer formulas over English language descriptions

Pros & Cons of Flowchart Advantages Communication: Flowcharts are better way of communicating the logic of a system to all concerned. Effective analysis: With the help of flowchart, problem can be analyzed in more effective way. Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase. Proper Debugging: The flowchart helps in debugging process. Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part

Pros & cons of flowchart Disadvantages Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy. Alterations and Modifications: If alterations are required the flowchart may require re-drawing completely. Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem. The essentials of what is done can easily be lost in the technical details of how it is done.

Pros & cons of pseudo code Advantages It can be easily in any word processor. It can be easily modified as compared to flowchart. It's implementation is very useful in structured design elements. It can be written easily. It can be read and understood easily. Converting a pseudocode to programming language is very easy as compared with converting a flowchart to programming language.

Pros & cons of pseudocode Disadvantages It is not visual. We do not get a picture of the design. There is no standardized style or format, so one pseudocode may be different from another. For a beginner, It is more difficult to follow the logic or write pseudocode as compared to flowchart.

Exercise (for practice) Write an algorithm and draw a flowchart to print all numbers between LOW and HIGH that are divisible by NUMBER Write an algorithm and draw a flowchart to print the multiplication table for 6's Write an algorithm and draw a flowchart to arrange N values read from the input in ascending order

Exercise (for practice) Write an algorithm and draw a flowchart that will find and print the number of vowels in a given set of characters and print there number of occurrences Write an algorithm and draw a flowchart that will find and print The factorial of NUMBER is FACTORIAL. Test the flowchart for NUMBER=5

The end