Introduction to Problem Solving
Notes
Topics:
- Introduction to problem solving:
- Steps for problem solving ( analysing the problem, developing an algorithm, coding, testing and debugging).
- Representation of algorithms using
- flow chart and
- pseudo code,
- Decomposition
Introduction
Computers is machine that not only use to develop the software. It is also used for solving various day-to-day problems.
Computers cannot solve a problem by themselves. It solve the problem on basic of the step-by-step instructions given by us.
Thus, the success of a computer in solving a problem depends on how correctly and precisely we –
- Identifying (define) the problem
- Designing & developing an algorithm and
- Implementing the algorithm (solution) do develop a program using any programming language.
Thus problem solving is an essential skill that a computer science student should know.
Steps for Problem Solving-
1. Analysing the problem
Analysing the problems means understand a problem clearly before we begin to find the solution for it. Analysing a problem helps to figure out what are the inputs that our program should accept and the outputs that it should produce.
2. Developing an Algorithm
It is essential to device a solution before writing a program code for a given problem. The solution is represented in natural language and is called an algorithm.
Algorithm: A set of exact steps which when followed, solve the problem or accomplish the required task.
3. Coding
Coding is the process of converting the algorithm into the program which can be understood by the computer to generate the desired solution.
You can use any high level programming languages for writing a program.
4. Testing and Debugging
The program created should be tested on various parameters.
- 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.
- In the presence of syntactical errors, no output will be obtained.
- In case the output generated is incorrect, then the program should be checked for logical errors, if any.
Software Testing methods are
- unit or component testing,
- integration testing,
- system testing, and
- acceptance testing
Debugging – The errors or defects found in the testing phases are debugged or rectified and the program is again tested. This continues till all the errors are removed from the program.
Algorithm
Algorithm is a set of sequence which followed to solve a problem.
Algorithm for an activity ‘riding a bicycle’:
1) remove the bicycle from the stand,
2) sit on the seat of the bicycle,
3) start peddling,
4) use breaks whenever needed and
5) stop on reaching the destination.
Algorithm for Computing GCD of two numbers:
Step 1: Find the numbers (divisors) which can divide the given numbers.
Step 2: Then find the largest common number from these two lists.
A finite sequence of steps required to get the desired output is called an algorithm. Algorithm has a definite beginning and a definite end, and consists of a finite number of steps.
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.
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
There are two common methods of representing an algorithm —
- Flow chart
- Pseudocode
Flowchart — Visual Representation of Algorithms
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. There are standardised symbols to draw flowcharts.
Start/End – Also called “Terminator” symbol. It indicates where the flow starts and ends.
Process – Also called “Action Symbol,” it represents a process, action, or a single step.
Decision – A decision or branching point, usually a yes/no or true/ false question is asked, and based on the answer, the path gets split into two branches.
Input / Output – Also called data symbol, this parallelogram shape is used to input or output data.
Arrow – Connector to show order of flow between shapes.
Question: Write an algorithm to find the square of a number.
Algorithm to find square of a number.
Step 1: Input a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Print square
The algorithm to find square of a number can be represented pictorially using flowchart
Table of Contents
Pseudocode
A pseudocode (pronounced Soo-doh-kohd) is another way of representing an algorithm. It is considered as a non-formal language that helps programmers to write algorithm. 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.
- The word “pseudo” means “not real,” so “pseudocode” means “not real code”.
Keywords are used in pseudocode:
- INPUT
- COMPUTE
- INCREMENT
- DECREMENT
- IF/ELSE
- WHILE
- TRUE/FALSE
Question : Write an algorithm to calculate area and perimeter of a rectangle, using both pseudocode and flowchart.
Pseudocode for calculating area and perimeter of a rectangle.
INPUT length
INPUT breadth
COMPUTE Area = length * breadth
PRINT Area
COMPUTE Perim = 2 * (length + breadth)
PRINT Perim
The flowchart for this algorithm
Benefits of Pseudocode
- A pseudocode of a program helps in representing the basic functionality of the intended program.
- By writing the code first in a human readable language, the programmer safeguards against leaving out any important step.
- For non-programmers, actual programs are difficult to read and understand, but pseudocode helps them to review the steps to confirm that the proposed implementation is going to achieve the desire output.
Flow of Control:
The flow of control depicts the flow of process as represented in the flow chart. The process can flow in
- Sequence
- Selection
- Repetition
Sequence
In a sequence steps of algorithms (i.e. statements) are executed one after the other.
Selection
In a selection, steps of algorithm is depend upon the conditions i.e. any one of the alternatives statement is selected based on the outcome of a condition.
Conditionals are used to check possibilities. The program checks one or more conditions and perform operations (sequence of actions) depending on true or false value of the condition.
Conditionals are written in the algorithm as follows:
If is true then
steps to be taken when the condition is true/fulfilled
otherwise
steps to be taken when the condition is false/not fulfilled
Question : Write an algorithm to check whether a number is odd or even.
• Input: Any number
• Process: Check whether the number is even or not
• Output: Message “Even” or “Odd”
Pseudocode of the algorithm can be written as follows:
PRINT “Enter the Number”
INPUT number
IF number MOD 2 == 0 THEN
PRINT “Number is Even”
ELSE
PRINT “Number is Odd”
Repetition
Repetitions are used, when we want to do something repeatedly, for a given number of times.
Question : Write pseudocode and draw flowchart to accept numbers till the user enters 0 and then find their average.
Pseudocode is as follows:
Step 1: Set count = 0, sum = 0
Step 2: Input num
Step 3: While num is not equal to 0, repeat Steps 4 to 6
Step 4: sum = sum + num
Step 5: count = count + 1
Step 6: Input num
Step 7: Compute average = sum/count
Step 8: Print average
The flowchart representation is
Coding
Once an algorithm is finalised, it should be coded in a high-level programming language as selected by the programmer. The ordered set of instructions are written in that programming language by following its syntax.
The syntax is the set of rules or grammar that governs the formulation of the statements in the language, such as spelling, order of words, punctuation, etc.
Source Code: A program written in a high-level language is called source code.
We need to translate the source code into machine language using a compiler or an interpreter so that it can be understood by the computer.
Decomposition
Decomposition is a process to ‘decompose’ or break down a complex problem into smaller subproblems. It is helpful when we have to solve any big or complex problem.
- Breaking down a complex problem into sub problems also means that each subproblem can be examined in detail.
- Each subproblem can be solved independently and by different persons (or teams).
- Having different teams working on different sub-problems can also be advantageous because specific sub-problems can be assigned to teams who are experts in solving such problems.
Once the individual sub-problems are solved, it is necessary to test them for their correctness and integrate them to get the complete solution.
Discover more from EduGrown School
Subscribe to get the latest posts sent to your email.