Algorithm Design & Implementation

Post on 15-Jan-2017

290 views 0 download

Transcript of Algorithm Design & Implementation

Lecture 7

Algorithm Design & Implementation

Lecturer: Sumaira Hussain

S.M.I University

ALGORITHM BUILDING BLOCKSAll problems can be solved by employing any one of the following building blocks or their combinations

1. Sequences2. Conditionals3. Loops

SEQUENCESA sequence of instructions that are executed in the precise order they are written in:

statement block 1statement block 2statement block 3

statement block 1

statement block 2

statement block 3

CONDITIONALSSelect between alternate courses of action depending upon the evaluation of a condition

If ( condition = true )statement block 1

Elsestatement block 2

End if statementblock 1

conditionTrue False

statementblock 2

LOOPSLoop through a set of statements as long as a condition is true

Loop while ( condition = true )statement block

End Loop

conditionTrue

False

statementblock

EXAMPLEWe will first go through the problem statement and then present the algorithm in three different formats:

1. Pseudo code 2. Flowchart 3. Actual code

EXAMPLEConvert a decimal number into binary

CONVERT 75 TO BINARY75237 1218 129 024 122 021 020 1

1001011

remainder

PSEUDO CODE1. Let the decimal number be an integer x, x > 02. Let the binary equivalent be an empty string y3. Repeat while x > 0 {

Determine the quotient & remainder of x ÷ 2

y = CONCATENATE( remainder, y )x = quotient

}4. Print “2”, y5. Stop

Start

Find quotient& remainder

of x ÷ 2

Get x

x>1 ?

Stop

y = CONC( x, remainder)x = quotient

x is the decimal numbery is the binary equivalent

Flowchart of Decimalto Binary

ConversionYes

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 clarityDo not put “code” in pseudo code make your pseudo code language independentDon’t write pseudo code for yourself – write it in an unambiguous fashion so that anyone with a reasonable knowledge can understand and implement itBe consistentPrefer formulas over English language descriptions

PROS & CONS OF FLOWCHARTAdvantages 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 FLOWCHARTDisadvantages 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 CODEAdvantages 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 PSEUDOCODEDisadvantages 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