1 of 67

Problem Solving

To solve a problem using computer, there are certain activities to be planned which include:

  • Specify the problem statement.
  • Identify one or more possible approaches to find a solution
  • Identify the assumptions / constraints involved
  • Choose a suitable approach and find the solution

2 of 67

Activities

Specify the problem statement: We can solve a problem only if it is known what is to be done. The specification of the problem statement involves the following:

  • Input : This part describes what is to be supplied to solve the given problem for producing the desired result. This may be related to the activity ‘Take the ingredients’ . Numerical example, the marks scored in maths, physics and chemistry are the input.
  • Output: This part specifies the solution to the given problem. Numerical example: Cut off mark.

3 of 67

Activities

  • Identify one or more possible approaches to find a solution: This involves finding the logic used for solving the given problem. There may be several possible ways for solving a given problem.
  • Identify the assumptions / constraints involved: We need to identify the underlying assumptions through which the solution is built.
  • Choose a suitable approach and find the solution : Among the several possible solutions identified, one has to choose the best possible solution.

4 of 67

Problem Solving Techniques

  • Set of techniques and graphical tools that help in providing logic for solving a problem.
  • There are three types of problem solving techniques:

Flowchart

Algorithm and

Pseudo code.

5 of 67

Flowchart

  • A flowchart is a graphical representation of the steps followed for solving a given problem.

6 of 67

Flowchart symbols

7 of 67

Flowchart symbols

8 of 67

Flowchart symbols and its purpose

  • Ellipse: is used to indicate the start and end of a flowchart. Start written in the ellipse indicates the beginning of a flowchart. End or Stop or Exit written in the ellipse indicates the end of the flowchart.
  • Parallelogram: A parallelogram is used to read data (input) or to print data (output).
  • Rectangle :A rectangle is used to show the processing that takes place in the flowchart.
  • Diamond: A diamond with two branches is used to show the decision making step in a flowchart. A question is specified in the diamond. The next step in the sequence is based on the answer to the question which is “Yes” or “No”.
  • Arrows : Arrows are used to connect the steps in a flowchart, to show the flow or sequence of the problem solving process

9 of 67

Guidelines for preparing flowcharts

  • All necessary requirements should be listed out in logical order.
  • The flowchart should be clear, neat and easy to follow. There should not be any ambiguity in understanding the flowchart.
  • The usual direction of the flow of a procedure or system is from left to right or top to bottom.
  • Only one flow line should come out from a process symbol.
  • Only one flow line should enter a decision symbol and two lines come out of the decision symbol each representing true and false.
  • If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines

10 of 67

Guidelines for preparing flowcharts

  • Avoid the intersection of flow lines if you want to make it more effective and better way of communication.
  • To ensure that the flowchart has a logical start and end. It is useful to test the validity of the flowchart by passing through it with a simple test data.

11 of 67

Benefits of flowcharts

  • Makes logic clear
  • Provide easy understanding
  • Effective analysis of the logic
  • Useful in coding
  • Enables proper testing and debugging
  • Provides better documentation and maintenance

12 of 67

Limitations of flowchart�

  • Complex logic : leads to complex and clumsy flowcharts if the logic is complex
  • Difficult to modify
  • No Update
  • Costly

13 of 67

TECHNIQUES

  • Flowcharts for computations
  • Flowcharts for decision making
  • Flowcharts for loops

14 of 67

Flowchart to find sum of two numbers�

15 of 67

Flowchart to find sum and average of three numbers

16 of 67

Flowchart to find the area of a rectangle�

17 of 67

Flowchart to convert temperature from Celsius to Fahrenheit

18 of 67

Flowchart to convert inches to centimetres

19 of 67

Flowcharts for decision making ( Pass/Fail) �

20 of 67

Flowchart to find maximum of three numbers

21 of 67

Calculating Cutoff mark for ‘N’ students

22 of 67

Algorithm

  • It is defined as a finite sequence of explicit instructions that, when provide with a set of input values, produces an output and terminates. Algorithms can have steps that repeat or require decisions.

23 of 67

Real Life Example

Algorithm to prepare a coffee

Step 1: Start

Step 2: Get the ingredients WATER, MILK, SUGAR and COFFEE POWDER

Step 3: Boil the WATER and add COFFEE POWDER

Step 4 : Add SUGAR and pour MILK

Step 5: Stir well

Step 6: Pour into cup

Step 7: A CUP OF COFFEE is ready to drink

Step 8 : Stop

24 of 67

Algorithm

To find total and average of three numbers

Step 1 : Start

Step 2 : Read the numbers, say A, B and C

Step 3 : Add A, B and C and store it in SUM

Step 4: Divide the SUM by 3 and store it in AVERAGE

Step 4 : Display the SUM, AVERAGE

Step 5 : Stop

25 of 67

Algorithm to find area of a rectangle

Step 1: Start

Step 2: Read the LENGTH and BREADTH of the rectangle

Step 3: Multiply LENGTH and BREADTH to compute the AREA

Step 4: Display the AREA

Step 5: Stop

26 of 67

Algorithm to find maximum of three numbers

Step 1 : Start

Step 2 : Read the three numbers, say A, B and C

Step 3 : Check whether A is greater than B and C. If, so display A is biggest

number and Stop

Step 4 : Otherwise, check whether B is greater than C. If, so display B is the

biggest number and stop.

Step 5 : Otherwise, display C is the biggest number

Step 6 : Stop

27 of 67

Calculating cutoff mark for N students

Step 1 : Start

Step 2: Read the number of students

Step 3: Initialize the COUNT to 1

Step 4: Initialize the CUTOFF to 0

Step 5 : If COUNT is less than or equal to N, Read MATHS, PHYSICS and

CHEMISTRY marks of a student. Otherwise go to Step 10

Step 6: Calculate CUTOFF by dividing the MATHS mark by 2, PHYSICS

and CHEMISTRY marks by 4 and adding them together (i.e)

CUTOFF = MATHS/2+PHYSICS/4+CHEMISTRY/4

Step 7: Increment the COUNT by 1

Step 8: Display the CUTOFF

Step 9: Go to Step 4

Step 10: Stop

28 of 67

Properties of an Algorithm

  • Clear and Unambiguous: The algorithm should be clear and unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning.
  • Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It may or may not take input.
  • Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-defined as well. It should produce at least 1 output.
  • Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
  • Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available resources. It must not contain some future technology or anything.
  • Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be the same, as expected.

29 of 67

Advantages of Algorithms�

  • It is easy to understand.
  • An algorithm is a step-wise representation of a solution to a given problem.
  • In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

30 of 67

Disadvantages of Algorithms

  • Writing an algorithm takes a long time so it is time-consuming.

  • Understanding complex logic through algorithms can be very difficult.

  • Branching and Looping statements are difficult to show in Algorithms.

31 of 67

Pseudocode

  • Pseudo means imitation and code refers to instructions.

  • It is a way of describing an algorithm without using any specific programming language related notations.

  • It is written using plain English statements rather than symbols, to represent the processes of computer program.

  • It is also known as PDL (Programming Design Language)

32 of 67

Guidelines for writing pseudocode

  • Should be written in simple English
  • Steps must be understandable
  • Should be concise
  • Each set of instructions is written from top to bottom, with only one entry and one exit

33 of 67

Advantages of pseudo code

  • Allows the developer to express the design in natural language
  • It is easier to develop program from pseudo code than with a flowchart
  • It is easy to translate the pseudo code into programming language
  • Compact and concise

34 of 67

Limitations of pseudo code

  • Does not provide visual representation of the program logic
  • No accepted standards for writing pseudo code
  • cannot be complied or executed

35 of 67

Keywords

  • Input: Read, Obtain, Get ,Input
  • Output: Print, Display, Show, Output
  • Calculation: Compute, Calculate
  • Incrementing : Increment
  • Keywords: ADD, Subtract, Initialize
  • Assignment / Process:. The format of the assignment operation is as follows:

Left side 🡨Right Side

Right side value is assigned to the left side variable.

Constructs

    • Selection: If-Then-Else, Case
    • Repetition: While, Repeat-Until, For

36 of 67

a. If-Then-Else

  • The If construct is used to execute the selected sequence based on the condition. The If construct has four keywords: If, Then, Else, and Endif. The general form is:

If condition Then

sequence 1

Else

sequence 2

Endif

  • The Else keyword and "sequence 2" are optional. If the condition is true, sequence 1 is performed, otherwise sequence 2 is performed.

37 of 67

b. While

  • The While construct is used to specify a loop with a test at the top. The beginning and ending of the loop are indicated by two keywords While and Endwhile. The general form is:

While condition

sequence

Endwhile

  • The loop is entered only if the condition is true. The "sequence" is performed for each iteration. At the conclusion of each iteration, the condition is evaluated and the loop continues as long as the condition is true.

38 of 67

c. Case

  • A Case construct indicates a multiway branch based on conditions. It consists of four keywords, Case, Of, Others, and Endcase, and conditions are used to indicate the various alternatives. The general form is:

Case expression of

condition 1 : sequence 1 � condition 2 : sequence 2 � ... � condition n : sequence n �Others: � default sequence

Endcase

  • The Others clause with its default sequence is optional. Conditions are normally numbers or characters.

39 of 67

d.Repeat-Until

  • This loop is similar to the While loop except that the test is performed at the bottom of the loop instead of at the top. Two keywords, Repeat and Until are used. The general form is:

Repeat

sequence

Until condition

  • The "sequence" in this type of loop is always performed at least once, because the test is performed after the sequence is executed. At the conclusion of each iteration, the condition is evaluated, and the loop repeats if the condition is false. The loop terminates when the condition becomes true.

40 of 67

e.For

  • This loop is a specialized construct for iterating a specific number of times. Two keywords, For and Endfor are used. The general form is:

For variable 🡨 start to end increment/ decrement by number

sequence

Endfor

  • The constructs that are embedded within each other are called nested constructs.The pseudocode is placed within Begin and End.

41 of 67

Pseudo code to find sum and �Average of Three Numbers

Begin

Get A,B,C

SUM 🡨 A + B + C

AVERAGE🡨SUM/3

Display SUM

End

42 of 67

��Pseudo code to find maximum of three numbers

Begin

Get A, B and C

If A > B and A > C then display A

Else If B > C then display B

Else Display C

Endif

Endif

End

43 of 67

Calculating cutoff mark for N students

Begin

Get N

For COUNT🡨1 to N

Read MATHS, PHYSICS, CHEMISTRY

CUTOFF 🡨 MATHS/2+PHYSICS/4+CHEMISTRY/4

Display CUTOFF

Endfor

End

44 of 67

Pseudo code to find sum till -1 is entered

Begin

SUM 🡨 0

Repeat

Get A

If A < > -1 then

SUM 🡨SUM+A

Endif

Until A = = -1

Display SUM

End

45 of 67

�Exchanging the values of two variables

  • Given two variables, FIRST and SECOND, swap the values assigned to them.

Algorithm

Step 1: Start

Step 2: Read two numbers, say FIRST and SECOND

Step 3: Move the value of FIRST to T

Step 4: Move the value of SECOND to FIRST

Step 5: Move the value of T to SECOND

Step 6: Display the value of FIRST and SECOND

Step 7: Stop

 

46 of 67

Pseudocode

Begin

Get FIRST, SECOND

T 🡨 FIRST

FIRST 🡨 FIRST

SECOND 🡨 T

Display FIRST, SECOND

End

47 of 67

Flowchart

48 of 67

Finding the biggest among N numbers

Algorithm

Step 1 : Start

Step 2 : Read the number of elements among which maximum is to be found, say N

Step 3 : Initialize COUNT to 1

Step 4 : Read the first number, NUMBER and consider it as the BIGGEST

Step 5 : If COUNT is less than or equal to N, then read the next number, NUMBER

Otherwise go to Step 9.

Step 6: If NUMBER is greater than BIGGEST, the assign NUMBER to BIGGEST

Step 7 : Increment the COUNT by 1

Step 8: Go to Step 5

Step 9 : Display the BIGGEST

Step 10: Stop

49 of 67

Pseudocode

Begin

Get N

COUNT 🡨 1

Get NUMBER

BIGGEST 🡨 NUMBER

 For COUNT🡨 2 to N

Get NUMBER

If NUMBER > BIGGEST then

BIGGEST 🡨 NUMBER

Endif

Endfor

Display BIGGEST

End

 

50 of 67

Flow Chart

51 of 67

�Counting

a. Algorithm

Step 1: Start

Step 2 : Read the number of students whose marks are to be counted as pass

or fail, say N

Step 3 : Initialize I to 1, PASS_COUNT and FAIL_COUNT to 0

Step 4 : Read the mark, say MARK

Step 5 : If MARK is greater than or equal to 50, then PASS_COUNT is

incremented Otherwise, FAIL_COUNT is incremented

Step 6 : Increment I by 1

Step 7 : If I is less than or equal to N, go to step 4

Step 8 : Display the PASS_COUNT and FAIL_COUNT

Step 9 : Stop

52 of 67

Pseudocode

Begin

Get N

PASS_COUNT 🡨0

FAIL_COUNT 🡨0

For I 🡨 1 to N

Get MARK

If MARK > = 50 then

PASS_COUNT 🡨 PASS_COUNT + 1

Else

FAIL_COUNT 🡨 FAIL_COUNT + 1

Endif

Endfor

Display PASS_COUNT, FAIL_COUNT

End

53 of 67

Flowchart

54 of 67

�Summation of ‘n’ numbers

a. Algorithm

Step 1: Start

Step 2: Read the number upto which sum is to be found, say N

Step 3: Initialize the COUNT to 1 and SUM to 0

Step 4: Read the number to be added, say A

Step 5: Add A to SUM

Step 6: Increment COUNT by 1

Step 7: If the value of COUNT is greater than N, then goto Step 9

Step 8:otherwise Goto Step 4

Step 9: Display the value of SUM

Step 10: Stop

55 of 67

Pseudocode

Begin

Get N

COUNT 🡨 1

SUM 🡨 0

For COUNT 🡨 1 to N

Get A

SUM🡨SUM+A

Endfor

Display SUM

Stop

56 of 67

Flowchart

57 of 67

Finding the factorial of a given number

Algorithm:

Step 1: Start

Step 2: Initialize I to 1 and FACT to 1

Step 3: Read the number to find factorial,say , N

Step 4: Multiply FACT and I and store the result in FACT

Step 5: Increment I by 1

Step 6 : If I is greater than N then goto step 8

Step 7: Goto Step 4

Step 8: Display the value of FACT

Step 9: Stop

58 of 67

Pseudocode

Begin

Get N

FACT 🡨 1

For I 🡨 1 to N

FACT 🡨 FACT * I

Endfor

Display FACT

End

59 of 67

Flowchart

60 of 67

Generation of Fibonacci series

  • The Fibonacci sequence will be as follows:

0, 1 , 1, 2, 3, 5, 8, 13,. . .

From the Fibonacci sequence, we know that

New term = previous term + term before previous term

Let us define

FIB as the new term

K as the preceding term of FIB

J as the term before the preceding term of k

The initial values are:

J 🡨 0 First Fibonacci number

K 🡨1 Second Fibonacci number

FIB 🡨 J+K Third Fibonacci number

61 of 67

  • After that, the new term becomes the previous term. The previous term becomes the term before the present previous term. To achieve this, we need the following assignment.
  • J 🡨K term before the previous term becomes previous term
  • K 🡨FIB previous term becomes new term.
  • Then, generate the Fibonacci number by adding J and K. To count the number of terms generated, variable I is used. I is incremented every time and the Fibonacci number generation process is repeated until I reaches N.

62 of 67

Algorithm

Step 1: Start

Step 2: Initialize I, J and FIB to 0 and K to 1

Step 3: Read the number upto which the Fibonacci series to be

generated, say N

Step 4: Display the FIB

Step 5 : Assign K to J and FIB to K

Step 6: Add J and K and store it in FIB

Step 7 : Increment I by 1

Step 8: If I is less than N goto Step 4

Step 9: Stop

63 of 67

Pseudocode

Begin

I 🡨 0, FIB 🡨 0, J 🡨 0, K 🡨1

Get N

For I 🡨 1 to N

Display FIB

J 🡨 K

K 🡨 FIB

FIB 🡨 J+K

Endfor

End

64 of 67

Flowchart

65 of 67

Reversing the digits of an integer

  • Algorithm

Step 1: Start

Step 2: Read the number whose reverse is to be found, say N

Step 3: Initialize REVERSE to 0

Step 4 : Divide N by 10 and store the reminder in REM

Step 5: Multiply the REVERSE by 10 and add the REM to it

Step 6 : Divide N by 10 and store the quotient in N

Step 8: If N is greater than 0, the goto Step 4

Step 9: Display the reverse number, REVERSE

Step 10: Stop

66 of 67

Reversing the digits of an integer

67 of 67

Pseudocode

Begin

Get N

REVERSE 🡨 0

Repeat

REM 🡨 N % 10

REVERSE 🡨 REVERSE * 10 + REM

N 🡨 N/10

Until N = 0

Display REVERSE

End