Download presentation
Presentation is loading. Please wait.
Published byCuthbert Boone Modified over 8 years ago
1
ALGORITHM Algorithm, is the step by step procedure for calculation or problem solving. E.G: Step 1: Unlock the Phone Step 2: Open Menu and Goto Message Step 3: Select option “Create Message” Step 4: Now type the message and send it This is an algorithm to send a message. Definition: A finite set of instructions which if followed, accomplishes a particular task or it is description of logical order of actions.
2
Advantages of Algorithm: 1)It is neat representation of solution 2)Stepwise representation makes it easy to understand the solution 3)Important to estimate performance of the solution 4)It makes it easy to find errors and faults for each computer program, first we define a algorithm
3
Characteristics of Algorithm: 1) Input: An algorithm should have finite no of input 2) Output: An algorithm must produce at least one output 3) Definiteness: Each instruction should be clear & ambiguous 4) Finiteness: An algorithm must have finite no of instructions. i.e it should terminate 5) Effectiveness: Every instruction of an algorithm must be feasible
4
Problem Definition: Given a number “N” different than ‘0’, we want to determine if it is positive or negative….. The algorithm in the following figure identifies whether i/p given is “positive” or “negative” Algorithm Input -2 Negative
5
Algorithm Input 150Positive Output
6
Program for the Addition of two Numbers #include main() { int a, b, c; clrscr();` printf(“Enter the two numbers a & b”); scanf(“%d %d”, &a, &b); c=a+b; printf(“The sum is %d”, c); getch(); return 0; }
7
Algorithm for the Addition of two Numbers Step 1 : Start Step 2 : Accept two numbers in variables a & b Step 3 : Perform addition (a+b) and store the result in third variable say c i.e c=a+b Step 4 : Print the value of c Step 5 : Stop
8
Types of Algorithm Brute Force Algorithm: The brute force approach starts at some point and search through all possibilities until it finds the solution Divide And Conquer Algorithm: It works by dividing a problem into sub-problem till the sub-problem is very small. Then find the solution to each problem sub-problem. Solution to the original problem is combination of all solutions to sub- problem Greedy Algorithm: A greedy person tries to get best. Similarly this algorithm try to find best solution for the given problem Dynamic Programming Algorithm: This type of algorithm remembers result from previous steps and uses them find new results
9
Generalized Algorithm There are two types of solutions generalized and specialized. Generalized solutions are applicable to variety of problems and varieties of inputs. Specialized solutions are only applicable for particular problem for particular input. Algorithms are designed generalized so that they can be applicable to varieties of inputs.
10
Advantages of Generalized Algorithm: The algorithm do not restrict the programmer to know any language It helps to focus solely on the solution to the problem No need to use many special algorithms If programmer does not know the knowledge of implementation of algorithm but the basic concept can be well understood
11
Dis-advantages of Generalized Algorithm: They become very complicated in many cases Modifications or changes may cause critical conditions for some set of inputs Finding and fixing errors is a tough task
12
Infinite Loop Loop: It is programming technique whereby a group of instructions is repeated. The simplest of all the looping structure in C is while statement. The basic format of while statement is : while(test condition) { body of the loop } The while is an entry-controlled loop structure. The test- condition is evaluated & if condition is true, then body of loop is executed.
13
An infinite loop or endless loop is a sequence of instructions in a computer program which loops endlessly : The loop having no terminating condition OR Having a condition that can never be met e.g #include main() { while(1) //Condition will remain always true { printf(“Infinite Loop”); } This is a loop that will print “Infinite Loop” without halting
14
#include main() { while(1) //Condition will remain always true { int a=5, b=6, c; c=a+b; printf(“Hello All”); printf(“\n %d”,c); } O/P: Hello All 11 Hello All 11.
15
E.g. for(i=1; i>0; i++) { printf(“\nMescoe Students”); } O/P: Mescoe Students. Mescoe Students
16
Methods to Avoid Infinite Loop Following are methods to avoid infinite loop : Repetitions are controlled by : 1)Counter 2)Using Sentinal Value Counter : A counter is a variable that can be used to prespecify the number of times the statements inside the loop will be executed.
17
#include void main() { int count=0; while(count<4) { printf(“\nHello All”); count= count+1; } O/P: Hello All
18
Methods to Avoid Infinite Loop by Sentinel Value Sentinel value is used for controlling indefinite loops. It is response or signal to end a loop. In computer programming, sentinel value is also referred to as a flag value.
19
Different ways of Representing Algorithm There are number of ways to represent an algorithm: An algorithm can be represented using: 1)A Program 2)A Flowchart 3) A Pseudo Code
20
Representing Algorithm as a Program An algorithm is a set of instruction written in plain English language which can represented as a Program. To represent algorithm as Program we can use of many HLL Languages like C,C++,JAVA..etc Consider an algorithm for addition of two numbers: Step 1 : Start Step 2 : Accept two numbers in variables a & b Step 3 : Perform addition (a+b) and store the result in third variable say c i.e c=a+b Step 4 : Print the value of c Step 5 : Stop
21
C Program for above algorithm: #include void main() { int a, b, c; printf(“Enter the two numbers a & b”); scanf(“%d %d”, &a, &b); c=a+b; printf(“The sum is %d”, c); }
22
Consider an algorithm for Calculating the area and perimeter of a rectangle: Step 1 : Start Step 2 : Declare variables Length, Breadth, Area and Perimeter Step 3 : Accept/ Read Length and Breadth Step 4 : Calculate the Area of rectangle with formula Area=Length * Breadth Step 5 : Calculate the Perimeter of rectangle with formula 2*(Length+Breadth) Step 6 : Print the Area of rectangle Step 7 : Print the Perimeter of rectangle Step 8 : Stop
23
Representing Algorithm as a Flowchart A flowchart is a diagrammatic representation of an algorithm. After an algorithm has been written, we convert it into a flowchart. It makes use of certain standard symbols.
24
Given below are some symbols which are used for constructing a Flowchart: A Terminal Block: It is used to indicate beginning or end the flowchart The Input/Output Symbol: Data Input or Output The Process: Symbol:All calculations and formulas or Computation or Processing Step 1) 2) 3)
25
The Decision Box : Whenever a decision is to be taken depending on a certain condition being met or not The Flow Lines:This indicate the direction of the logic flow The Connector: Whenever we are not able to complete our flowchart on the same page, we make use of what is called as connector 4) 5) 6)
26
Flowchart As name suggests these are charts and diagram which represent the flow of the algorithm A flowchart is specific shape to represent different types of steps in algorithm Using these shapes makes representation structure and easy to understand The arrows connecting the symbols, called flow lines, show the progression in which the steps take place. It gives graphical representation if the program
27
Flowchart for addition of two numbers: Start Declare variables a,b,sum Read numbers a and b sum=a+b Display sum Stop
28
Flowchart for Calculating the Area and Perimeter of a Rectangle: Read Length and Breadth Area=Length*Breadth Perimeter=2*(Length + Breadth) Print Area Print Perimeter Stop Start Declare variables len and bred
29
Representing Algorithm as a Pseudo Code The prefix Pseudo means “Fake” or “False” Pseudo code, therefore, literally means “fake code” that is, not the code that is actually entered into the computer Pseudo code does not follow a specific programming language syntax Advantages of Pseudo Code: 1)It is very easy to represent the program 1)This tool is used for generic representation of program Disadvantages of Pseudo Code: 1)Complex programs can become confusing as general language is used 2) Error may come while translating the code
30
Pseudo Code for calculating and displaying the area and perimeter of rectangle START READ Length READ Breadth Calculate Area Area = Length * Breadth Calculate Perimeter Perimeter=2*(Length+Breadth) Display Area Display Perimeter END
31
Pseudo Code for displaying larger number from two numbers: START IF A<B Print “A” ELSE Print “B” END
32
Program Planning Creating a program is third stage or coding stage of software Before coding we need to plan and design the program Need of Program Planning or Designing : Planning helps us to get overview of whole program Planning or designing becomes a major guideline for the developer in developing the program Major errors can be detected at the early stage
33
Program Planning Tools I.Flowcharts II. Pseudo codes III. Structure Charts
34
Structure Charts Main Get DataProcess Data Show Results Stop Fig: Structure Chart for System
35
Structure Charts: Structure charts shows how the different parts of a program relate to each other Structure chart are generally used in the large programs where programs are divided into many modules Hierarchy charts may also be called as : 1.Structure charts 2.Top Down Charts 3.VTOC(Visual Table OF Contents)
36
Importance of use of Indentation of Programming In computer programming language, indentation is used to format program source code to improve readability Indentation is generally only of use to programmers and common users Compilers and Interpreters rarely care how much whitespace is present in between programming statements Indenting is adding spaces/tabs in front of ‘blocks’ of code, so that it is easier for us and other people to see how code flows..
37
Program with Indentation #include void main() { int a,b,sum; printf(“Enter the two no’s to add”); scanf(“%d %d”, &a,&b); sum=a+b; printf(“Addition is %d”, sum); } Program without Indentation #include void main() { int a,b,sum; printf(“Enter the two no’s to add”); scanf(“%d %d”, &a,&b); sum=a+b; printf(“Addition is %d”, sum); } Advantages of Indentation : 1)Increases the readability 2)Helps to locate errors easily in a program 3)Helps to find the beginning and end of the section of code
38
Structured Programming Concepts Structure programming is a method used to design and code programs in a systematic, organized manner It emerged in the year of 1960 as programming protocol, aimed on improving the clarity, quality and development time of a computer program by using functions, looping structure etc.. Use of “Go to” statement is discouraged which is both difficult to follow and maintain
39
“Go to” Statement: The “goto” statement is used to alter normal sequence of program execution by transferring control to some part of program unconditionally. Using “goto” statement is not good programming practice In general it is written as goto label; where label is identifier that is used to label the target statement to which control is transferred to anywhere within current function The target statement must be labeled, and a colon must be follow the label
40
Example of go to statement : #include void main() { int n = 0; loop : printf(“\n %d”, n); n++; if(n<10) { goto loop; } getch(); }
41
Writing Programs Using Sequence, Selection and Iteration Logic In Structured Programming there exists three ways of combining programs i.e Sequencing, Selection and Iteration Sequence Logic: It is a logic which describes a sequence of actions that a program carries out one after another, unconditionally Selection Logic: It is program construct that allows a program to choose between different actions. It allows for the alternative paths to be taken through a program Iteration Logic: It is a logic in which programmer specifies an action to be repeated while some condition remains true
42
Sequence Logic This instructions do the actual data processing such as i/p, o/p, add, subtract, copy, move..etc By sequential we mean ‘in-sequence”, one-after-another Sequential logic is easier to construct and follow The statements are placed in such a way that we want them to be executed and the program executes them in sequence from start to end The e.g to the right hand side, the arrows linking represent the execution flow Start Statement 1 Statement 2 Statement 3 Statement 4 Stop
43
Selection Logic In selection structure, the prog is executed based upon the given condition Only instructions that satisfy the given condition are executed 1)if One alternative 2)if…..else Two alternatives 3)Nested if…..else Multiple alternatives 4)switch Multiple alternatives
44
Iteration Logic It is used to execute a number of statements from the program more than one time without having to write the statements multiple times To execute a number of instructions from the program for a finite, predetermined number of time (Counter Controlled Loop) There are three types of iteration loops in C: 1) while 2) do….while 3) for
46
Functions What is a Function....? A function is a self-contained block of statements that perform a coherent task of some kind. main( ) { message( ) ; printf( "\n IS A LOGICAL SUBJECT" ) ; } message( ) { printf( "\n FUNDAMENTALS OF PROG LANG" ) ; } O/P: FUNDAMENTALS OF PROG LANG IS A LOGICAL SUBJECT
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.