Tuesday, September 7, 2021

Introduction to Problem Solving

Introduction to problem solving

Content:

  • Introduction to problem solving
  • Steps for Problem solving with computer:
    • Analysing the problem
    • Developing an Algorithm
    • Coding
    • Testing and Debugging
  • Algorithm
  • Representation of Algorithms:
    • Flowchart
    • Pseudocode
  • Decomposition

Introduction:

Computers are used for solving various day-to-day problems and thus problem solving is an essential skill that a computer science student should know. Computers themselves cannot solve a problem. Precise step-by-step instructions should be given by us to solve the problem. Thus, the success of a computer in solving a problem depends on how correctly and precisely we define the problem, design a solution (algorithm) and implement the solution (program) using a programming language.
DEF: Problem solving is the process of identifying a problem, developing an algorithm for the identified problem and finally implementing the algorithm to develop a computer program.

Steps for Problem solving with computer:

Analysing the problem:

By analysing a problem, we would be able to figure out what are the inputs that our program should accept and the outputs that it should produce.

Developing an Algorithm:

  • An algorith is a sequence of steps which when executed may solve a problem
  • In developing an algorithm, tentative solution plan and keep on refining the algorithm until the algorithm is able to capture all the aspects of the desired solution.
  • For a given problem, more than one algorithm is possible and we have to select the most suitable solution.

Coding

Coding is the process of conversion of Algorithm into a computer understandable format. Usually involves writing the algorithm in high level language like python, c++ etc.

Testing and Debugging:

  • The program should meet the requirements of the user. It must respond within the expected time. It should generate correct output for all possible inputs. Hence testing becomes imortant.
  • Software industry follows standardised testing methods like unit or component testing, integration testing, system testing, and acceptance testing while developing complex applications. This is to ensure that the software meets all the business and technical requirements and works as expected

Algorithm:

Writing an algorithm is mostly considered as a first step to programming. Once we have an algorithm to solve a problem, we can write the computer program for giving instructions to the computer in high level language

Characteristics of a good algorithm:

  • Precision — the steps are precisely stated or defined.
  • Uniqueness — results of each step are uniquely defined and only depend on the input and the result of the preceding steps.
  • Finiteness — the algorithm always stops after a finite number of steps.
  • Input — the algorithm receives some input.
  • Output — the algorithm produces some output.

NOTE:
While writing an algorithm, it is required to clearly identify the following:
  • The input to be taken from the user
  • Processing or computation to be performed to get the desired result
  • The output desired by the user

Representation of Algorithms:

Two representation will be considered here

  • Flowchart
  • pseudocode

Flowchart:A flowchart is a visual representation of an algorithm. A flowchart is a diagram made up of boxes, diamonds and other shapes, connected by arrows. Each shape represents a step of the solution process and the arrow represents the order or link among the steps.

Shapes used in flow chart:

bb9a9c752bb5b6c07b92a78abd76dec5.gif

Sample algorithm representation:

1.Write an algorithm to find the square of a number using flow chart.

Basic idea:
Input: Number whose square is required
Process: Multiply the number by itself to get its square and store it
Output: Square of the number

Flow Chart:

square%20flowchart%20snip.JPG

2.Write an algorithm to check if the numbers entered by user is odd or even, using flowchart.

Basic idea:
Input: Number which is to be checked
Process:

  • Take the number and divide it by 2
  • If the remainder of the division is zero then the number is Even
  • If the remainder of the division is not zero then the number is Odd
Output: Display odd or even based on the process stage

FLowchart:

Odd-even.png

Pseudocode:

It is a non-formal language with which algorithm can be constructed.It is a detailed description of instructions that a computer must follow in a particular order. It is intended for human reading and cannot be executed directly by the computer. No specific standard for writing a pseudocode exists.

some of the frequently used keywords while writing pseudocode:

  • INPUT
  • COMPUTE
  • PRINT
  • INCREMENT
  • DECREMENT
  • IF/ELSE
  • WHILE
  • TRUE/FALSE

Sample algorithm representation:

1.Write an algorithm to calculate area and perimeter of a rectangle, using pseudocode

Algorithm:

INPUT length
INPUT breadth
COMPUTE Area = length breadth
PRINT Area
COMPUTE Perim = 2
(length + breadth)
PRINT Perim

2.Write an algorithm to check whether a number is odd or even.

Algorithm

PRINT "Enter the Number"
INPUT number
IF number%2 == 0 THEN
   PRINT "Number is Even"
ELSE
   PRINT "Number is Odd"

Practice Question:

  1. Write pseudocode that reads two numbers and divide one by another and display the quotient.
  2. Write an algorithm to find the greatest among two different numbers entered by the user.
  3. Write an algorithm that accepts four numbers as input and find the largest and smallest of them.
  4. Draw a flowchart to check whether a given number is an Armstrong number. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

Decomposition:

The basic idea of solving a complex problem by decomposition is to 'decompose' or break down a complex problem into smaller sub problems. Each sub problem can be solved independently.

Reference:
Computer Science for class 11 - NCERT

Share: