ALGORITHM & FLOWCHARTS Definition: \u201cAn effective procedure for solving a problem in a finite no of steps.
OR An algorithm is a finite set of well defined instructions or step by step procedure to solve a given problem. Programs= Algorithms + Data. Characteristics of an Algorithm
1) Input : It may accept zero or more inputs 2) Definiteness : Each instruction must be clear, well-defined and precise. There should not be any ambiguity 3) Effectiveness : Each instruction must be simple and be carried out in a finite amount of time. 4) Finiteness : Algorithm should have finite sequence of instructions. That is, it should end after a fixed time. It should not enter into an infinite loop 5) Output :It should produce at least one output. Algorithm Notation
1) Name of algorithm: Specifies problem to be solved. 2) Step no - Identification tag of an instruction and it is an unsigned position number. 3) Explanatory comment within the square brackets 4) Termination Advantages of Algorithm \u2022 Effective communication: Since algorithm is written in English like language, logic of the problem can be explained easily to others \u2022 Easy and efficient coding : It acts as a blue print during a program development \u2022 Program debugging : Helps in debugging so that we can identify the logical errors in a program \u2022 Program maintenance : maintaining the software becomes much easier
1
Programming Fundamentals with C
Padmashree Desai, Asst.Prof, CSE Dept
Different patterns of algorithms 1) Sequential : In this different steps occur in a sequence 2) Conditional : In this different steps are executed based on a condition(whether true/false) . In programming languages, an conditional pattern is implemented using decision making statements. 3) I teration al : In this a task (one or more steps ) is repeated more than once. In programming languages, an iterational pattern is implemented using loops. An iterational construct is also known as \u201crepetitive\u201d construct Examples 1. Write an algorithm to find sum and average of 3 numbers Step Step Step Step Step Step
1: s tart 2: read a,b,c [ input 3 numbers] 3: sum \u00df a + b + c[ find sum] 4: avg \u00df sum/3 [find average] 5: print sum, avg [output the result] 6: stop
2. Write an Algorithm to find whether an entered integer number is positive or negative (Decision making type) Step 1: Start [Beginning of the Algorithm] Step 2: Read a number A [Input the integer number] Step 3: If A is greater than 0 [compare A to zero] print \u201cnumber as positive\u201d else print \u201cnumber as negative\u201d Step 4: Stop [End of the Algorithm] 3.
Write an algorithm to find largest of two numbers
[Beginning of the Algorithm] Step 1: Start Step 2: Read a and b [Input two numbers a,b] Step 3: if a is greater than b [ compare a and b] print \u201c a as large\u201d else print \u201cb as large\u201d [output the result] Step 4: Stop
2
Programming Fundamentals with C
Padmashree Desai, Asst.Prof, CSE Dept
Disadvantage of algorithm \u2022 Developing algorithm for large and complex problems would be time consuming and difficult to understand \u2022 Understanding complex logic through algorithms would be difficult
Flowcharts It is diagrammatic or pictorial representation of an algorithm. In other words \u201c A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem.\u201d The various symbols are used to
instructions/operations. Symbols :
For Diagrams/Block Diagrams refer class notes.
3
write different
Programming Fundamentals with C
Padmashree Desai, Asst.Prof, CSE Dept
Advantages • Flowchart form a good visual aid to represent the logic for the problem solution • Easy and efficient coding: Writing program is easy • Program debugging : Helps in debugging so that we can identify the logical errors in a program • Program maintenance : maintaining the software becomes much easier Disadvantages of flowchart Complex logic: For complicated logic, the flowchart becomes complex and clumsy Alterations and modifications : If there is change in the logic, the flowchart becomes to be completely rewritten and requires lot of time.
---------------------------------------------------Uploaded by Nix