Problem solving is the systematic approach to define the problem and creating number of solutions. The problem-solving process starts with the problem specifications and ends with a Correct program. P r o b l e m s o l vi n g t ec h n i que is a s e t o f t ec hn i ques t h a t h elps i n providing logic for solving a problem . The p r obl e m- so l v i n g T ec h niq u es can b e e xp r e ssed in t he f orm o f Algorithms, Flowcharts, Pseudo code and Programs PROBLEM SOLVING 4
Steps in Problem solving 5 Problem definition Problem analysis Algorithm Development Program coding Program testing and Debugging Documentation
Problem Definition - understand the problem Define or specify the problem by answering the following questions: What the computer program do? What tasks will it perform? What kind of data will it use and where will it get the data? What will be the output of the program? How will the program interact with the computer user? Problem Analysis – determine the requirements Analysing the problem involves Identifying the problem inputs Check any additional requirements or constraints Determine the required format of the results to be displayed 6
Algorithm Development - develop a step by step procedure to solve the problem Find an algorithm for its solution An a l g o r i t h m is a f i ni t e s e t o f s t e p s d e f i n i n g t h e s o l u t i o n o f a particular problem The algorithm development can be expressed by Pseudo code and Flowchart Program Coding - uses a programming language to write or implement the actual programming instructions Coding or programming is the process of translating the algorithm into the syntax of a given programming language. Convert each step in the algorithm into one or more statements in a programming language 7
Program Testing and Debugging P r og r a m t e s t i n g me a n s running t he p r og r a m a n d ex e c u t i n g a l l i t s instructions or functions step by step. Debugging is the process of finding and correcting program code mistakes. Errors in the program may be Syntax errors, Run time errors and Logic Errors(bugs) Documentation Internal Documentation: It consists of remarks or comments written with the program instructions to explain what is being done in the program External Documentation: It is made up of the manuals or help menus written about the solution 8
Three ways of Representation Algorithm Pseudo-code Flowchart Algorithm Set of step-by-step instructions that perform a specific task or operation Representation of Problem Solving Solution 9
Pseudo-code Set of instructions that mimic programming language instructions Representation of Solution 10
Difference between Algorithm and Pseudocode An algorithm is a precise and well-defined procedure P seudocode is a more informal and high-level representation of an algorithm. 11 Representation of Solution
Flowchart Schematic representation of an algorithm Representation of Solution 12
13
14 An algorithm is a detailed step by step instructions for solving a problem in a finite amount of time Used in solving mathematical or sometimes computational problems Algorithm
15 Algorithm Steps involved in algorithm development An algorithm can be defined as “a complete, unambiguous, finite number of logical steps for solving a specific problem “ . Step1. Identification of input: For an algorithm, there are quantities to be supplied called input and these are fed externally. The input is to be indentified first for any specified problem. Step2: Identification of output: From an algorithm, at least one quantity is produced, called for any specified problem. Step3 : Identification the 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.
16 Step 4: Processing Definiteness: The instruction composing the algorithm must be clear and there should not be any ambiguity in them. Step5 : Processing Finiteness : If we go through the algorithm, then for all cases, the algorithm should terminate after a finite number of steps. Step6 : Possessing Effectiveness : The instructions in the algorithm must be sufficiently basic and in practice they can be carries out easily
Introduction to Algorithm Input Output Finiteness Definiteness/Concise Effectiveness Characteristics of Algorithm 12
18 Input Algorithm should have well-defined inputs Output Algorithm should have well-defined outputs, and should match the desired output Finiteness Algorithms must terminate after a finite number of steps Definiteness/Concise Each step must be precisely defined Actions to carryout must be unambiguously (not confusing) specified Effectiveness All operations must achieve the goal Characteristics of Algorithm
19 A Jeroo starts at (0, 0) facing East with no flowers in its pouch. There is a flower at location (3, 0). Write an algorithm that directs the Jeroo to pick the flower and plant it at location (3, 2). After planting the flower, the Jeroo should hop one space East and stop. There are no other nets, flowers, or Jeroos on the island. WRITE ALGORITHM – SIMPLE EXAMPLE
The flower is exactly three spaces ahead of the jeroo . The flower is to be planted exactly two spaces South of its current location. The Jeroo is to finish facing East one space East of the planted flower. There are no nets to worry about. Get the flower Put the flower Hop East Get the flower Hop 3 times Pick the flower Put the flower Turn right Hop 2 times Plant a flower Hop East Turn left Hop once Analysis of the Problem High-level Algorithm Detailed Algorithm WRITE ALGORITHM
21 Example for Algorithm Problem - Write an Algorithm to Multiply two numbers Step 1 - Start Step 2 - Declare three Integers a, b, c Step 3 - Define Values of a, b Step 4 - Multiplication of a, b Step 5 - Store the Output of step 4 to c Step 6 - Print the value c Step 7 - Stop
22 Applications of Algorithm Google Search Uber App Video Games NASA’s Manipulation of Space Ships FaceBook Timeline
23 Advantages & Disadvantages of Algorithm Advantages It makes the representation of a solution to a problem easy , which makes easier in understanding It can be easily understood by a person without even having the knowledge of programming It follows a definite procedure Disadvantages It takes very long in writing an algorithm It is not a computer program, neither it helps in reducing the difficulties while writing a code
LETS HAVE A BREAK A doctor’s son’s father was not a doctor. How is this possible? A man was born in 1945, but he’s only 30 years old now. How is this possible? 24
LETS HAVE A BREAK A doctor’s son’s father was not a doctor. How is this possible? The doctor is the mother. A man was born in 1945, but he’s only 30 years old now. How is this possible? 1945 was the number of the hospital room. 25
17 Summary
27 Overview of Computer Programming Problem Solving Steps in Problem Solving Representation of Problem Solving Solution Algorithm Characteristics of Algorithm Example for Algorithm Applications of Algorithm Advantages & Disadvantages of Algorithm Introduction to Problem Solving, Algorithm Implementation
28 Addition of three numbers entered by the user Find the largest number among three different numbers Check whether a number is a prime number or not Factorial of a number entered by the user Mary Williams needs to change a Fahrenheit temperature to Celsius. Use the Fahrenheit temperature of 80 degrees to test your solution. Celsius = ((Fahrenheit - 32) * 5/9) 6. Calculate the area for a circle and display the result with proper output message. (Hint 𝐴𝑟𝑒𝑎=𝜋𝑟^2) Try Yourself
Simple Strategies For Developing Algorithms 29 Developing an algorithm is a very important step in problem-solving. There are different types of strategies that could be used to develop an algorithm to solve a problem. Two of the most frequently used strategies are: Iteration Recursion
1. Iterations : A sequence of statements is executed until a specified condition is true is called iterations. 1. 2. for loop While loop For loop : FOR( start-value to end-value) DO Statement ... ENDFOR 30
Example: Print n natural numbers BE G IN GET n INITIALIZE i=1 FOR (i<=n) DO PRINT i i=i+1 END F OR END 31
Syntax for While: WHILE (condition) DO Statement ... EN D WH I LE Print n natural numbers BE G IN GET n INITIALIZE i=1 WHILE ( i < = n ) DO PRINT i i=i+1 EN D WH I LE END 32
Recursion is a technique in which a function calls itself repeatedly until a concluding situation happen . A function which utilizes this technique is called a recursive function Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH) In order/Preorder/Post order Tree Traversals, DFS of Graph, etc. recursion 33