Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Problem Solving

Grh

Uploaded by

GHoSt GÀmINĞ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Problem Solving

Grh

Uploaded by

GHoSt GÀmINĞ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Pseudocode

 A pseudocode is another way of representing an


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.
 The word “pseudo” means “not real,” so “pseudocode”
means “not real code”. Keywords while writing
pseudocode: INPUT ,COMPUTE, PRINT, INCREMENT,
DECREMENT, IF/ELSE, WHILE, TRUE/FALSE
Example 1: Write an algorithm to display the sum of two
numbers entered by user, using both pseudocode and
flowchart.
Pseudocode:
INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result
Flowchart:
Example 2: Write an algorithm to calculate area and
perimeter of a rectangle, using both pseudocode and
flowchart.
Pseudocode.
INPUT length
INPUT breadth
COMPUTE Area = length * breadth
PRINT Area
COMPUTE Perim = 2 * (length + breadth)
PRINT Perim
Flowchart:
Benefits of Algorithm:
 Pseudocode can be quickly and easily converted into an
actual code of a programming language
 It is easy to understand and modify the computer
program
 The algorithm errors can be identified and corrected easily
before running the code
Difference between ALGORITHM and PESUDOCODE
Algorithm Pseudocode

1. It is a step by step procedure 1. It is a step-by-step


using natural language description of an algorithm in
code–like structure
2. An algorithm uses simple 2. Pseudocode uses both
English words. keywords of computer language
and English words
3. There are no rules for writing 3. There are certain rules for
algorithm writing pseudocode
4. Algorithm can be considered 4. Pseudocode cannot be
as pseudocode considered as algorithm
5. It is difficult to understand 5. It is easy to understand and
and interpret interpret

1. Pseudocode and flowchart for finding sum of two


numbers:
Pseudocode: Flowchart

INPUT x
INPUT y
Compute sum=x+y
PRINT sum
2. The pseudocode and flowchart for finding area and
perimeter of a rectangle:
Pseudocode:
INPUT length
INPUT breadth
Compute: area=length x breadth
Compute: perimeter = 2 x length x breadth
PRINT area
PRINT perimeter
Flowchart:
FLOW OF CONTROL:
 The flow of control depicts the flow of events as
represented in the flow chart.
 The events can flow in a sequence, or on branch based on
a decision or even repeat some part for a finite number of
times.
The types of Flow Control are:
1. Sequence
2. Selection
3. Repetition / Looping
SEQUENCE:
 A sequence programming construct is the most simple
and basic construct used for programming
 It instruct the CPU which statement is to be executed
 It execute the statement from beginning to end
Example: Algorithm and flowchart to display the total
marks obtained by the student

SELECTION:
 Depending on the needs certain number of statements are
selected for execution
 This type of selection is performed using selection
statement.
 The inclusion or exclusion of the statement is performed
through the result of the given condition.
If statement:
 It is a simple selection statement.
 When the condition is TRUE, the code which is associated
with IF statement will be executed. If the condition is
FALSE, control goes out of the IF statement and proceed
with the execution of other statement present in the
program.
Example : Pseudocode and flowchart to find whether
the given number is even or not.
Pseudocode:
INPUT num
IF(num mod 2 == 0) THEN
PRINT “Even number”
[End of IF]
Flowchart:

IF - ELSE statement
 This type of selection statement is used to define
TRUE/FALSE condition.
 If the condition is TRUE, the group if statement
associated with IF statement will be executed, otherwise,
the statement which are associated with ELSE statement
will be executed.
Pseudocode:
INPUT num
IF(num mod 2 !=0) THEN
PRINT “Odd number”
ELSE
PRINT “Even number”
[End of IF]
Flowchart:

IF – ELSE – IF statement
 This type of statement is used for defining multiple
conditions.
 If first condition is TRUE, then execute the statement
associated with the corresponding IF statement, otherwise
same input is checked with other conditions. If no
condition is TRUE then it executes the statement
associated with the ELSE part is executed.

Pseudocode:
INPUT age
IF(age < 13) THEN
PRINT “child”
ELSE IF(age < 20) THEN
PRINT “teenager”
ELSE
PRINT “adult”
[End of IF]

Flowchart:

Repetition/Looping
 The process of performing task again and again is
called Repetition
 The repetitive actions are known as iteration or looping.
 Loop refers to a programming construct that are used to
repeatedly execute selected codes.
 The types of loop in python are: while-loop and for-loop
Example 1: Write a pseudocode and flowchart to accept
N numbers and find their average.
Pseudocode:
STEP1: SET COUNT=0, SUM=0
STEP 2: INPUT N
STEP 3: WHILE COUNT<N REPEAT step 4 through step 6
STEP 4: SUM=SUM+N
STEP 5: COUNT=COUNT + 1
[End of step 3 WHILE loop]
STEP 6: COMPUTE AVG=SUM/COUNT
STEP 7: PRINT AVG
Flowchart:

Verifying Algorithm:
 Verification means, a testing the procedure with various
inputs and finds that there are multiple cases where it
does not produce the expected output.
 Verification of algorithm is an important step in problem
solving. Because, if the algorithm is not correct, computer
will run the program, but produce wrong result.
Comparison of algorithm:
Algorithm analysis is the process of evaluating the
performance of an algorithm usually in terms of its time and
complexity.
The key areas of focus involves:
 Time complexity: Time complexity refers to how much
time an algorithm needs to run a program.
 Space complexity: Refers to how much memory an
algorithm needs to run a program.
 Scalability: This means how best an algorithm performs
as size of the dataset increases.
Coding:
 The term coding refers to the process of writing
instruction for a computer to execute the program.
 In computer programming, coding involves translating
human readable instruction into a format that a computer
can understand and execute.
 The goal of coding is to create software, applications or
scripts that perform specific task.
 The program which is written using machine codes is
directly understand by the hardware devices.
 High-level languages are human understandable and were
introduced for easy programming.
 The codes written in assembly level or high-level
languages are called as source code
 The computer uses compiler and interpreter as translators
for translating source code into object code.
Key aspects of Coding involves:
 Algorithm design: The coding begins with designing
algorithm for solving particular problem. Algorithm serves
as a blueprint for coding.
 Programming language: there are numerous
programming language, each with their own syntaxes and
feature. The common programming language includes
C++, JAVA, Python, and JavaScript etc.
 Code implementation: The developers write code based
on the chosen programming language, following the rules
and syntax of that language.
 Debugging: it is a process of identifying and fixing errors
or bugs in the program to ensure that program works as
intended is known as Debugging.
 Testing: testing involves the code with various inputs to
verify its correctness and efficiency.
Decomposition
Decomposition is the process of breaking-down the
complex problem/ program into smaller sub-problem/program.
Breaking down the problem into smaller sub-programs are
often easier to handle, debug and maintain.
Key features of Decomposition are:
 Division: The problem or system is divided into smaller
parts.
 Abstraction: Each part is simplified and dealt with in
isolation from the rest of the problem or system.
 Solving: Every individual part is then solved or
addressed.
 Integration: The solution to the individual parts are then
integrated to solve the original problem or to form the
whole system.
Importance of Decomposition
 It helps in solving complex problems easily.
 The decomposed sub-tasks can be carried or solved by
different persons or a group of persons, which helps in
teamwork.
 When the problem is divided into sub-tasks each sub-
tasks can be examined in detail.

You might also like