Lecture 7 Algorithm Design & Implementation Lecturer: Sumaira Hussain S.M.I University
All problems can be solved by employing any one of the following building blocks or their combinations 1. Sequences 2. Conditionals 3. Loops
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
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
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
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
Convert a decimal number into binary
752 37 12 18 12 9 02 4 12 2 02 1 02 0 1 1001011 remainder
1. Let the decimal number be an integer x, x > 0 2. Let the binary equivalent be an empty string y 3. Repeat while x > 0 { Determine the quotient & remainder of x ÷ 2 y = CONCATENATE( remainder, y ) x = quotient } 4. Print “2”, y 5. 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
#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 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
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
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.
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.
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.
 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
 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
Algorithm Design & Implementation

Algorithm Design & Implementation

  • 1.
    Lecture 7 Algorithm Design& Implementation Lecturer: Sumaira Hussain S.M.I University
  • 2.
    All problems canbe solved by employing any one of the following building blocks or their combinations 1. Sequences 2. Conditionals 3. Loops
  • 3.
    A sequence ofinstructions 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
  • 4.
    Select between alternatecourses 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
  • 5.
    Loop through aset of statements as long as a condition is true Loop while ( condition = true ) statement block End Loop condition True False statement block
  • 6.
    We will firstgo through the problem statement and then present the algorithm in three different formats: 1. Pseudo code 2. Flowchart 3. Actual code
  • 7.
    Convert a decimalnumber into binary
  • 8.
    752 37 12 18 12 902 4 12 2 02 1 02 0 1 1001011 remainder
  • 9.
    1. Let thedecimal number be an integer x, x > 0 2. Let the binary equivalent be an empty string y 3. Repeat while x > 0 { Determine the quotient & remainder of x ÷ 2 y = CONCATENATE( remainder, y ) x = quotient } 4. Print “2”, y 5. Stop
  • 10.
    Start Find quotient & remainder ofx ÷ 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
  • 11.
    #include<stdio.h> int main(){ long intdecimalNumber,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.
    12 Use indention forimproved 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
  • 13.
    Advantages  Communication: Flowchartsare 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
  • 14.
    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.
  • 15.
    Advantages  It canbe 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.
  • 16.
    Disadvantages  It isnot 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.
  • 17.
     Write analgorithm 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
  • 18.
     Write analgorithm 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