Introduction to Computer Science and Engineering by :Nandini Sidana LOGIC DEVELOPMENT AND ALGORITHM
Various Techniques to solve a problem
Introduction  A computer is a very powerful and versatile machine, yet it has no intelligence or thinking power.  This places responsibility on the user to instruct the computer in a correct and precise manner, so that the machine is able to perform the required job in a proper way.  In order to instruct a computer correctly: ● the user must have clear understanding of the problem to be solved ● he should be able to develop a method, in the form of series of sequential steps, to solve it
Procedure (Steps Involved in Problem Solving) In order to solve a problem by the computer, one has to pass though certain stages or steps. They are  Step 1: Understanding the problem Here we try to understand the problem to be solved in totally. Before with the next stage or step, we should be absolutely sure about the objectives of the given problem.
Step 2: . Analysing the problem After understanding thoroughly the problem to be solved, we look different ways of solving the problem and evaluate each of these methods. The idea here is to search an appropriate solution to the problem under consideration. The end result of this stage is a broad overview of the sequence of operations that are to be carries out to solve the given problem. Step 3: Developing the solution Here the overview of the sequence of operations that was the result of analysis stage is expanded to form a detailed step by step solution to the problem under consideration.
Step 4: Coding and Implementation The last stage of the problem solving is the conversion of the detailed sequence of operations in to a language that the computer can understand. Here each step is converted to its equivalent instruction or instructions in the computer language that has been chosen for the implantation.
An algorithm is a plan for solving a problem.  Every problem solution starts with a plan and that plan is called an algorithm. There are many ways to write an algorithm: • Formal • Informal • Mathematical or • Graphical The form is not particularly important as long as it provides a good way to describe and check the logic of the plan.
A set of sequential steps usually written in Ordinary Language to solve a given problem is called Algorithm.  It may be possible to solve to problem in more than one ways, resulting in more than one algorithm.  The choice of various algorithms depends on the factors like: reliability accuracy easy to modify and time requirement to execute it  The algorithm which will need the least time when executed is considered the best.
An algorithm can be defined as “a complete, unambiguous, finite number of logical steps for solving a specific problem “ Steps involved in algorithm development : • Identification of input: For an algorithm, there are quantities to be supplied called input. The input is to be identified first for any specified problem. • Identification of output: From an algorithm, at least one quantity is produced, called for any specified problem. • Identification of processing operations : All the calculations to be performed in order to lead to output from the input are to be identified in an orderly manner.
• Processing Definiteness : The instructions composing the algorithm must be clear and there should not be any ambiguity in them. • Processing Finiteness : If we go through the algorithm, then for all cases, the algorithm should terminate after a finite number of steps. • Possessing Effectiveness : The instructions in the algorithm must be sufficiently basic and in practice they can be carries out easily.
Properties of Algorithm:  Finiteness: An algorithm must terminate in a finite number of steps  Definiteness: Each step of the algorithm must be precise and definite  Effectiveness: Each step must be effective, in the sense that it should be easily convertible into program statement and can be performed exactly in a finite amount of time.  Generality: The algorithm must be complete in itself so that it can be used to solve problems of a specific type for any input data.  Input/output: Each algorithm must take zero, one or more quantities as input data produce one or more output values
Try it out !!!! Examples:  Find the average of three numbers.  Write an algorithm to calculate the simple interest using the formula. Simple interest = P*N* R/100. Where P is principle Amount, N is the number of years and R is the rate of interest.  Write an algorithm to find the area of the triangle. Let b, c be the sides of the triangle ABC and A the included angle between the given sides.  Write an algorithm to find the largest of three numbers X, Y,Z  Write an algorithm which will test whether a given integer value is prime or not.  Write algorithm to find the factorial of a given number N  Write an algorithm to find sum of given data values until negative value is entered.  Write an algorithm to calculate the perimeter and area of rectangle. Given its length and width
❑ Write algorithm to find the factorial of a given number N STEP 1: PROD =1 STEP 2: I = 0 STEP 3: READ N STEP 4: WHILE I < N DO 4.1 I = I + 1 4.2. PROD = PROD* I STEP 5: WRITE “FACTORIAL OF”, N, “IS”, PROD STEP 6: END.
❑ Find the average of three numbers. STEP 1: READ THE NUMBERS A, B, C STEP 2 : COMPUTE THE SUM OF A, B AND C STEP 3 : DIVIDE THE SUM BY 3 STEP 4 : STORE THE RESULT IN VARIABLE D STEP 5 : PRINT THE VALUE OF D STEP 6 : END OF THE PROGRAM
❑ Write an algorithm to calculate the simple interest using the formula. Simple interest = P*N* R/100. Where P is principle Amount, N is the number of years and R is the rate of interest. STEP 1: READ THE THREE INPUT QUANTITIES’ P, N AND R. STEP 2 : CALCULATE SIMPLE INTEREST AS SIMPLE INTEREST = P* N* R/100 STEP 3: PRINT SIMPLE INTEREST. STEP 4: STOP.
❑ Write an algorithm to find the area of the triangle. Let b, c be the sides of the triangle ABC and A the included angle between the given sides. STEP 1: INPUT THE GIVEN ELEMENTS OF THE TRIANGLE NAMELY SIDES B, C AND ANGLE BETWEEN THE SIDES A. STEP 2: AREA = (1/2) *B*C* SIN A STEP 3: OUTPUT THE AREA STEP 4: STOP.
❑ Write an algorithm to find the largest of three numbers X, Y,Z STEP 1: READ THE NUMBERS X,Y,Z. STEP 2: IF (X > Y) BIG = X ELSE BIG = Y STEP 3 : IF (BIG < Z) STEP 4: BIG = Z STEP 5: PRINT THE LARGEST NUMBER I.E. BIG STEP 6: STOP.
❑ Write an algorithm which will test whether a given integer value is prime or not. STEP 1: M=2 STEP 2: READ N STEP 3: MAX=SQRT (N) STEP 4: WHILE M < = MAX DO 4.1 IF (M* (N/M) = N 4.1.1 THEN 4.1.1.1 GO TO STEP 7 4.2. M = M + 1 STEP 5: WRITE “NUMBER IS PRIME” STEP 6: GO TO STEP 8 STEP 7: WRITE “NUMBER IS NOT A PRIME” STEP 8: END.
❑ Write an algorithm to find sum of given data values until negative value is entered. STEP 1: SUM = 0 STEP 2: I = 0 STEP 3: READ NEW VALUE STEP 4: WHILE NEW VALUE < = 0 DO 4.1 SUM = SUM + NEW VALUE 4.2 1 = I + 1 4.3 READ NEW VALUE STEP 5: WRITE “SUM OF”, I, “DATA VALUE IS, “SUM STEP 6: END
❑ Write an algorithm to calculate the perimeter and area of rectangle. Given its length and width STEP 1: READ LENGTH OF THE RECTANGLE. STEP 2: READ WIDTH OF THE RECTANGLE. STEP 3: CALCULATE PERIMETER OF THE RECTANGLE USING THE FORMULA PERIMETER = 2* (LENGTH + WIDTH) STEP 4: CALCULATE AREA OF THE RECTANGLE USING THE FORMULA AREA = LENGTH *WIDTH. 256 COMPUTER SCIENCE AND ENGINEERING STEP 5: PRINT PERIMETER. STEP 6: PRINT AREA. STEP 7: STOP.
Ways to specify an algorithm ‘FLOWCHARTS’ AND ‘PSEUDO CODES’
Flowcharts A flowchart is visual or graphical representation of an algorithm.
Introduction  A flow chart is a step by step diagrammatic representation of the logic paths to solve a given problem.  The flowcharts are pictorial representation of the methods to b used to solve a given problem and help a great deal to analyze the problem and plan its solution in a systematic and orderly manner.  A flowchart when translated in to a proper computer language, results in a complete program.
Advantages of Flowcharts:  The flowchart shows the logic of a problem displayed in pictorial fashion which felicitates easier checking of an algorithm.  The Flowchart is good means of communication to other users. It is also a compact means of recording an algorithm solution to a problem.  The flowchart allows the problem solver to break the problem into parts. These parts can be connected to make master chart.  The flowchart is a permanent record of the solution which can be consulted at a later time.
Difference between Algorithm and Flowchart Algorithm  A method of representing the step-by- step logical procedure for solving a problem  It contains step-by-step English descriptions, each step representing a particular operation leading to solution of problem  These are particularly useful for small problems  For complex programs, algorithms prove to be Inadequate Flowchart  Flowchart is diagrammatic representation of an algorithm. It is constructed using different types of boxes and symbols.  The flowchart employs a series of blocks and arrows, each of which represents a particular step in an algorithm  These are useful for detailed representations of complicated programs  For complex programs, Flowcharts prove to be adequate
Symbols used in flowcharts: The symbols that we make use while drawing flowcharts as given below are as per conventions followed by International Standard Organization (ISO).  Oval: Rectangle with rounded sides is used to indicate either START/ STOP of the program.  Input and output indicators: Parallelograms are used to represent input and output operations. Statements like INPUT, READ and PRINT are represented in these Parallelograms.
Process Indicators: - Rectangle is used to indicate any set of processing operation such as for storing arithmetic operations. Decision Makers: The diamond is used for indicating the step of decision making and therefore known as decision box. Decision boxes are used to test the conditions or ask questions and depending upon the answers, the appropriate actions are taken by the computer. The decision box symbol is Flow Lines: Flow lines indicate the direction being followed in the flowchart. In a Flowchart, every line must have an arrow on it to indicate the direction. The arrows may be in any direction
On- Page connectors: Circles are used to join the different parts of a flowchart and these circles are called on-page connectors. The uses of these connectors give a neat shape to the flowcharts. Ina complicated problems, a flowchart may run in to several pages. The parts of the flowchart on different pages are to be joined with each other. The parts to be joined are indicated by the circle. Off-page connectors: This connector represents a break in the path of flowchart which is too large to fit on a single page. It is similar to on-page connector. The connector symbol marks where the algorithm ends on the first page and where it continues on the second
Example: Draw a flowchart for adding the integers from 1 to 100 and to print the sum. Start Sum=0 N=1 Stop Is N>100? Print Sum Sum=Sum+N N=N+1 Yes No
Try it yourself:  Draw the Flowchart to find Roots of Quadratic equation a𝑥2 + b𝑥 + c = 0. The coefficients a, b, c are the input data.  Draw a flowchart to find out the biggest of the three unequal positive numbers.  Draw a flowchart to find the factorial of given positive integer N.  ABC company plans to give a 6% year-end bonus to each of its employees earning $6,000 or more per month , and a fixed $250/- - bonus to the remaining employees. Draw a flowchart for calculating the bonus for an employee
Pseudo Codes ❑ The Pseudo code is neither an algorithm nor a program. ❑ It is an abstract form of a program. It consists of English like statements which perform the specific operations. ❑ It is defined for an algorithm. ❑ It does not use any graphical representation. ❑ In pseudo code, the program is represented in terms of words and phrases, but the syntax of program is not strictly followed.
Advantages of Pseudo Code:  Easy to read  Easy to understand  Easy to modify
Example: Write a pseudo code to perform the basic arithmetic operations.  Read n1, n2  Sum = n1 + n2  Diff = n1 – n2  Mult = n1 * n2  Quot = n1/n2  Print sum, diff, mult, quot  End.
Good Luck ☺

Logic Development and Algorithm.

  • 1.
    Introduction to ComputerScience and Engineering by :Nandini Sidana LOGIC DEVELOPMENT AND ALGORITHM
  • 2.
    Various Techniques tosolve a problem
  • 3.
    Introduction  A computeris a very powerful and versatile machine, yet it has no intelligence or thinking power.  This places responsibility on the user to instruct the computer in a correct and precise manner, so that the machine is able to perform the required job in a proper way.  In order to instruct a computer correctly: ● the user must have clear understanding of the problem to be solved ● he should be able to develop a method, in the form of series of sequential steps, to solve it
  • 4.
    Procedure (Steps Involvedin Problem Solving) In order to solve a problem by the computer, one has to pass though certain stages or steps. They are  Step 1: Understanding the problem Here we try to understand the problem to be solved in totally. Before with the next stage or step, we should be absolutely sure about the objectives of the given problem.
  • 5.
    Step 2: .Analysing the problem After understanding thoroughly the problem to be solved, we look different ways of solving the problem and evaluate each of these methods. The idea here is to search an appropriate solution to the problem under consideration. The end result of this stage is a broad overview of the sequence of operations that are to be carries out to solve the given problem. Step 3: Developing the solution Here the overview of the sequence of operations that was the result of analysis stage is expanded to form a detailed step by step solution to the problem under consideration.
  • 6.
    Step 4: Codingand Implementation The last stage of the problem solving is the conversion of the detailed sequence of operations in to a language that the computer can understand. Here each step is converted to its equivalent instruction or instructions in the computer language that has been chosen for the implantation.
  • 7.
    An algorithm isa plan for solving a problem.  Every problem solution starts with a plan and that plan is called an algorithm. There are many ways to write an algorithm: • Formal • Informal • Mathematical or • Graphical The form is not particularly important as long as it provides a good way to describe and check the logic of the plan.
  • 8.
    A set ofsequential steps usually written in Ordinary Language to solve a given problem is called Algorithm.  It may be possible to solve to problem in more than one ways, resulting in more than one algorithm.  The choice of various algorithms depends on the factors like: reliability accuracy easy to modify and time requirement to execute it  The algorithm which will need the least time when executed is considered the best.
  • 9.
    An algorithm canbe defined as “a complete, unambiguous, finite number of logical steps for solving a specific problem “ Steps involved in algorithm development : • Identification of input: For an algorithm, there are quantities to be supplied called input. The input is to be identified first for any specified problem. • Identification of output: From an algorithm, at least one quantity is produced, called for any specified problem. • Identification of processing operations : All the calculations to be performed in order to lead to output from the input are to be identified in an orderly manner.
  • 10.
    • Processing Definiteness: The instructions composing the algorithm must be clear and there should not be any ambiguity in them. • Processing Finiteness : If we go through the algorithm, then for all cases, the algorithm should terminate after a finite number of steps. • Possessing Effectiveness : The instructions in the algorithm must be sufficiently basic and in practice they can be carries out easily.
  • 11.
    Properties of Algorithm: Finiteness: An algorithm must terminate in a finite number of steps  Definiteness: Each step of the algorithm must be precise and definite  Effectiveness: Each step must be effective, in the sense that it should be easily convertible into program statement and can be performed exactly in a finite amount of time.  Generality: The algorithm must be complete in itself so that it can be used to solve problems of a specific type for any input data.  Input/output: Each algorithm must take zero, one or more quantities as input data produce one or more output values
  • 12.
    Try it out!!!! Examples:  Find the average of three numbers.  Write an algorithm to calculate the simple interest using the formula. Simple interest = P*N* R/100. Where P is principle Amount, N is the number of years and R is the rate of interest.  Write an algorithm to find the area of the triangle. Let b, c be the sides of the triangle ABC and A the included angle between the given sides.  Write an algorithm to find the largest of three numbers X, Y,Z  Write an algorithm which will test whether a given integer value is prime or not.  Write algorithm to find the factorial of a given number N  Write an algorithm to find sum of given data values until negative value is entered.  Write an algorithm to calculate the perimeter and area of rectangle. Given its length and width
  • 13.
    ❑ Write algorithmto find the factorial of a given number N STEP 1: PROD =1 STEP 2: I = 0 STEP 3: READ N STEP 4: WHILE I < N DO 4.1 I = I + 1 4.2. PROD = PROD* I STEP 5: WRITE “FACTORIAL OF”, N, “IS”, PROD STEP 6: END.
  • 14.
    ❑ Find theaverage of three numbers. STEP 1: READ THE NUMBERS A, B, C STEP 2 : COMPUTE THE SUM OF A, B AND C STEP 3 : DIVIDE THE SUM BY 3 STEP 4 : STORE THE RESULT IN VARIABLE D STEP 5 : PRINT THE VALUE OF D STEP 6 : END OF THE PROGRAM
  • 15.
    ❑ Write analgorithm to calculate the simple interest using the formula. Simple interest = P*N* R/100. Where P is principle Amount, N is the number of years and R is the rate of interest. STEP 1: READ THE THREE INPUT QUANTITIES’ P, N AND R. STEP 2 : CALCULATE SIMPLE INTEREST AS SIMPLE INTEREST = P* N* R/100 STEP 3: PRINT SIMPLE INTEREST. STEP 4: STOP.
  • 16.
    ❑ Write analgorithm to find the area of the triangle. Let b, c be the sides of the triangle ABC and A the included angle between the given sides. STEP 1: INPUT THE GIVEN ELEMENTS OF THE TRIANGLE NAMELY SIDES B, C AND ANGLE BETWEEN THE SIDES A. STEP 2: AREA = (1/2) *B*C* SIN A STEP 3: OUTPUT THE AREA STEP 4: STOP.
  • 17.
    ❑ Write analgorithm to find the largest of three numbers X, Y,Z STEP 1: READ THE NUMBERS X,Y,Z. STEP 2: IF (X > Y) BIG = X ELSE BIG = Y STEP 3 : IF (BIG < Z) STEP 4: BIG = Z STEP 5: PRINT THE LARGEST NUMBER I.E. BIG STEP 6: STOP.
  • 18.
    ❑ Write analgorithm which will test whether a given integer value is prime or not. STEP 1: M=2 STEP 2: READ N STEP 3: MAX=SQRT (N) STEP 4: WHILE M < = MAX DO 4.1 IF (M* (N/M) = N 4.1.1 THEN 4.1.1.1 GO TO STEP 7 4.2. M = M + 1 STEP 5: WRITE “NUMBER IS PRIME” STEP 6: GO TO STEP 8 STEP 7: WRITE “NUMBER IS NOT A PRIME” STEP 8: END.
  • 19.
    ❑ Write analgorithm to find sum of given data values until negative value is entered. STEP 1: SUM = 0 STEP 2: I = 0 STEP 3: READ NEW VALUE STEP 4: WHILE NEW VALUE < = 0 DO 4.1 SUM = SUM + NEW VALUE 4.2 1 = I + 1 4.3 READ NEW VALUE STEP 5: WRITE “SUM OF”, I, “DATA VALUE IS, “SUM STEP 6: END
  • 20.
    ❑ Write analgorithm to calculate the perimeter and area of rectangle. Given its length and width STEP 1: READ LENGTH OF THE RECTANGLE. STEP 2: READ WIDTH OF THE RECTANGLE. STEP 3: CALCULATE PERIMETER OF THE RECTANGLE USING THE FORMULA PERIMETER = 2* (LENGTH + WIDTH) STEP 4: CALCULATE AREA OF THE RECTANGLE USING THE FORMULA AREA = LENGTH *WIDTH. 256 COMPUTER SCIENCE AND ENGINEERING STEP 5: PRINT PERIMETER. STEP 6: PRINT AREA. STEP 7: STOP.
  • 21.
    Ways to specifyan algorithm ‘FLOWCHARTS’ AND ‘PSEUDO CODES’
  • 22.
    Flowcharts A flowchart isvisual or graphical representation of an algorithm.
  • 23.
    Introduction  A flowchart is a step by step diagrammatic representation of the logic paths to solve a given problem.  The flowcharts are pictorial representation of the methods to b used to solve a given problem and help a great deal to analyze the problem and plan its solution in a systematic and orderly manner.  A flowchart when translated in to a proper computer language, results in a complete program.
  • 24.
    Advantages of Flowcharts: The flowchart shows the logic of a problem displayed in pictorial fashion which felicitates easier checking of an algorithm.  The Flowchart is good means of communication to other users. It is also a compact means of recording an algorithm solution to a problem.  The flowchart allows the problem solver to break the problem into parts. These parts can be connected to make master chart.  The flowchart is a permanent record of the solution which can be consulted at a later time.
  • 25.
    Difference between Algorithmand Flowchart Algorithm  A method of representing the step-by- step logical procedure for solving a problem  It contains step-by-step English descriptions, each step representing a particular operation leading to solution of problem  These are particularly useful for small problems  For complex programs, algorithms prove to be Inadequate Flowchart  Flowchart is diagrammatic representation of an algorithm. It is constructed using different types of boxes and symbols.  The flowchart employs a series of blocks and arrows, each of which represents a particular step in an algorithm  These are useful for detailed representations of complicated programs  For complex programs, Flowcharts prove to be adequate
  • 26.
    Symbols used inflowcharts: The symbols that we make use while drawing flowcharts as given below are as per conventions followed by International Standard Organization (ISO).  Oval: Rectangle with rounded sides is used to indicate either START/ STOP of the program.  Input and output indicators: Parallelograms are used to represent input and output operations. Statements like INPUT, READ and PRINT are represented in these Parallelograms.
  • 27.
    Process Indicators: -Rectangle is used to indicate any set of processing operation such as for storing arithmetic operations. Decision Makers: The diamond is used for indicating the step of decision making and therefore known as decision box. Decision boxes are used to test the conditions or ask questions and depending upon the answers, the appropriate actions are taken by the computer. The decision box symbol is Flow Lines: Flow lines indicate the direction being followed in the flowchart. In a Flowchart, every line must have an arrow on it to indicate the direction. The arrows may be in any direction
  • 28.
    On- Page connectors:Circles are used to join the different parts of a flowchart and these circles are called on-page connectors. The uses of these connectors give a neat shape to the flowcharts. Ina complicated problems, a flowchart may run in to several pages. The parts of the flowchart on different pages are to be joined with each other. The parts to be joined are indicated by the circle. Off-page connectors: This connector represents a break in the path of flowchart which is too large to fit on a single page. It is similar to on-page connector. The connector symbol marks where the algorithm ends on the first page and where it continues on the second
  • 29.
    Example: Draw aflowchart for adding the integers from 1 to 100 and to print the sum. Start Sum=0 N=1 Stop Is N>100? Print Sum Sum=Sum+N N=N+1 Yes No
  • 30.
    Try it yourself: Draw the Flowchart to find Roots of Quadratic equation a𝑥2 + b𝑥 + c = 0. The coefficients a, b, c are the input data.  Draw a flowchart to find out the biggest of the three unequal positive numbers.  Draw a flowchart to find the factorial of given positive integer N.  ABC company plans to give a 6% year-end bonus to each of its employees earning $6,000 or more per month , and a fixed $250/- - bonus to the remaining employees. Draw a flowchart for calculating the bonus for an employee
  • 31.
    Pseudo Codes ❑ ThePseudo code is neither an algorithm nor a program. ❑ It is an abstract form of a program. It consists of English like statements which perform the specific operations. ❑ It is defined for an algorithm. ❑ It does not use any graphical representation. ❑ In pseudo code, the program is represented in terms of words and phrases, but the syntax of program is not strictly followed.
  • 32.
    Advantages of PseudoCode:  Easy to read  Easy to understand  Easy to modify
  • 33.
    Example: Write apseudo code to perform the basic arithmetic operations.  Read n1, n2  Sum = n1 + n2  Diff = n1 – n2  Mult = n1 * n2  Quot = n1/n2  Print sum, diff, mult, quot  End.
  • 34.