0% found this document useful (0 votes)
38 views

Module 2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Module 2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

MODULE 2

Algorithm and Pseudo


code
Program Development
 A program is needed to instruct the computer about the way a task is
to be performed. The instructions in a program have three essential
parts:

 1. Instructions to accept the input data that needs to be processed,


 2. Instructions that will act upon the input data and process it, and
 3. Instructions to provide the output to user
steps that are followed by the programmer for
writing a program:

 Problem Analysis—The programmer first understands the problem to be solved. The


programmer determines the various ways in which the problem can be solved, and decides
upon a single solution which will be followed to solve the problem.

Program Design—The selected solution is represented in a form, so that it can be coded. This
requires three steps—
 An algorithm is written, which is an English-like explanation of the solution.
 A flowchart is drawn, which is a diagrammatic representation of the solution. The solution is
represented diagrammatically, for easy understanding and clarity.
 A pseudo code is written for the selected solution. Pseudo code uses the structured
programming constructs. The pseudo code becomes an input to the next phase.
 Program Development
The algorithm is coded using suitable programming languages.
 The coded algorithm or program is compiled for any syntax errors. Syntax errors arise
due to the incorrect use of programming language or due to the grammatical errors
with respect to the programming language used. During compilation, the syntax errors,
if any, are removed.
 The successfully compiled program is now ready for execution.

 Testing -The executed program generates the output result, which may be correct or
incorrect. The program is tested with various inputs, to see that it generates the desired
results. If incorrect results are displayed, then the program has semantic error (logical
error). The semantic errors are removed from the program to get the correct results.

 The successfully tested program is ready for use and is installed on the user’s
machine.
 Program Documentation and Maintenance—The program is properly documented,
so that later on, anyone can use it and understand its working. Any changes made to
the program, after installation, forms part of the maintenance of program. The program
may require updating, fixing of errors etc. during the maintenance phase.
What is an algorithm?
An algorithm is a step-by-step procedure or a set of well-defined
instructions for solving a problem or completing a task.
Each step in an algorithm must be clear, unambiguous, and finite, leading
to a specific result.
 Formal Definition: An algorithm is a finite sequence of well-defined
steps that take input(s) and transform them into an output to solve a
problem.
 Example: A simple algorithm to find the sum of two numbers:
 Start
 Read two numbers A and B
 Calculate Sum = A + B
 Display Sum
 End
Reasons for Using Algorithms
 Efficiency: Algorithms are designed to solve problems in the most
efficient way possible, minimizing time and resources.
 Clarity and Structure: Algorithms provide a clear, structured approach
to problem-solving. By following a series of logical steps, you ensure that
the problem is tackled systematically.
 Automation: Algorithms can be translated into code, allowing machines
to perform tasks automatically without human intervention. This is
crucial in fields such as computer science, artificial intelligence, and
automation.
 Reusability: Once an algorithm is designed and proven to be correct, it
can be reused to solve similar problems without rethinking the process
from scratch.
 Optimization: Algorithms help in finding optimal solutions for complex
problems, especially in fields like data science, operations research, and
machine learning, where optimization is key.
 Problem-solving: Algorithms break down complex problems into
smaller, manageable steps, which make them easier to solve. They act
as a blueprint for designing programs.
 Consistency: Algorithms ensure that a problem is solved the same way
every time, providing predictable and consistent results, essential for
software development.
 Verification: Algorithms can be tested and verified for correctness. By
analyzing the logic and flow, one can determine if the solution is correct
and whether the desired outcome is achieved.
Key Properties of an Algorithm
 Finiteness: An algorithm must terminate after a finite number of steps.
 Definiteness: Each step in the algorithm must be clearly defined
without ambiguity.
 Input: An algorithm must have zero or more inputs provided before
execution.
 Output: The algorithm should produce at least one output.
 Effectiveness: Each operation should be simple enough to be
performed in a finite time.
Pseudocode
 Pseudocode is an informal way of writing the steps of an algorithm in a
manner that is easy to understand.
 It uses a mixture of natural language and programming-like constructs
but does not follow the syntax of any specific programming language.
 The purpose of pseudocode is to provide a clear and human-readable
description of the logic of an algorithm without the technicalities of
code.
Reasons for Using Pseudocode
 Clarity: Pseudocode is easy to understand for people with varying levels
of technical expertise since it uses plain language.
 Focus on Logic: It allows you to focus on the core logic of the algorithm
without worrying about specific syntax.
 Communication: It is a useful tool for communicating algorithms to
others, especially those who may not be familiar with programming
languages.
 Planning: It helps in planning the structure of the program before
coding, making the development process more efficient.
 Error Prevention: By laying out the steps of an algorithm clearly,
pseudocode helps to identify potential issues early.
Main Constructs of Pseudocode

 1.Sequencing
 Definition: Refers to the execution of statements one after the other
in a specific order. It is the simplest construct where instructions are
executed in the order in which they are written.
 Example:
Start
Read A, B
Sum = A + B
Print Sum
End
 2.Selection (Decision-making)If-Else Structure: This is used to
make decisions based on conditions. If the condition is true, one block
of code is executed; otherwise, another block is executed.
 Example:
 If grade >= 50
 Print "Pass"
 Else
 Print "Fail"
 EndIf
 Case Structure (Switch): Used when multiple conditions or values
are compared, and different actions are taken for each case.

 Example:
 Case day of the week
 1: Print "Monday"
 2: Print "Tuesday"
 3: Print "Wednesday"
 Else: Print "Invalid Day"
 EndCase
 3.Repetition (Loops)
 For Loop: Used when the number of iterations is known in advance.
 Example:
 For i = 1 to 10
 Print i
 EndFor
 While Loop: Executes a block of code repeatedly as long as a condition
is true.
 Example:
 While count < 10
 Print count
 count = count + 1
 EndWhile
 Repeat-Until Loop: Similar to the while loop but ensures that the
loop runs at least once, as the condition is checked at the end.
 Example:
 Repeat
 Print count
 count = count + 1
 Until count >= 10
Flowchart
 What is a Flowchart?
 A flowchart is a graphical representation of an algorithm or a process, using different
symbols to denote various types of operations, decisions, or flows of control.
 Each symbol in a flowchart represents a specific type of action.
 Seven most common shapes used in a flowchart:-
 Ovals represent the start and end of the process..
 Rectangles represent processes or instructions.
 Diamonds represent decision points (e.g., yes/no or true/false choices).
 Parallelogram represent input output symbol.
 Arrows represent the flow of control from one step to the next.
 Circle on-page connector
 Five pointed polygon off-page connector
How Flowcharts Help in Program
Development
 Visualizing the Problem: Flowcharts provide a clear and organized visual
representation of the steps involved in solving a problem, making it easier to
understand complex processes.

 Designing the Algorithm: They help in designing the logical flow of an algorithm,
including sequencing, decision-making, and loops, ensuring a structured approach.

 Code Planning: Flowcharts act as a blueprint for writing code, providing a clear
structure that can be easily translated into program instructions.

 Error Prevention: They allow developers to spot logical errors, missing steps, or
inefficiencies in the algorithm before actual coding begins.
 Debugging and Testing: Flowcharts guide debugging by allowing
developers to trace the logical flow and identify issues. They also help in
generating comprehensive test cases by covering all possible paths.

 Team Communication: Flowcharts improve communication among


team members and stakeholders, ensuring everyone has a common
understanding of the process.

 Documentation and Maintenance: Flowcharts serve as


documentation, making it easier for future developers to understand and
modify the program.

 Optimization: By visually analyzing the process, developers can identify


opportunities to simplify or optimize the code for better performance.
Area and Circumference of the
circle
Step 1: Start
Step 2: Read radius r
Step 3: Calculate area A=3.14*r*r
Step 4: Calculate circumference C=2*3.14*r
Step 5: Display A,C
Step 6: Stop


BEGIN
READ r
CALCULATE A and C
A=3.14*r*r
C=2*3.14*r
DISPLAY A, C
END
To check greatest of two numbers a,b
Step 1: Start
Step 2: Get a,b value
Step 3: check if(a>b) print 'a is greater'
Step 4: else print 'b is greater'
Step 5: Stop

BEGIN
READ a,b
IF (a>b) THEN
DISPLAY 'a is greater'
ELSE
DISPLAY 'b is greater'
END IF
END
Introduction to Programming
with RAPTOR
 RAPTOR is a flowchart-based programming environment, designed specifically to help
students visualize their algorithms and avoid syntactic baggage.
 RAPTOR programs are created visually and executed visually by tracing the execution through
the flowchart.
 RAPTOR is freely distributed as a service to the education community.
 RAPTOR was originally developed by and for the US Air Force Academy, Department of
Computer Science, but its use has spread and RAPTOR is now used for CS education in over 28
countries.
 Overview of Raptor
 The primary window consists of four main areas:
 The Symbols area in the upper left presents the 6 primary graphical symbols that can
be used in Raptor
 The assignment symbol is used to give a variable a numeric or string value.
 The call symbol is used to make calls to outside procedures, such as graphics
routines.
 The input symbol is used for getting input from the user.
 The output symbol is used to display text to the Master Console.
 The selection structure is used for decision making.
 The loop structure is used for iteration and repetition.
 The area immediately below the Symbols area is the Watch Window.
 This area allows the user to view the current contents of any variables and arrays as
the flowchart is executing.
 The large, white area to the right is the primary Workspace.
 Users can build their flowcharts in this area and watch them update as they execute.
Key features of RAPTOR
 Student can prepare flowchart. RAPTOR is a flowchart-based
programming environment.

 Student can visualize their algorithms.



Flowchart tracing is possible in RAPTOR.

RAPTOR can generate C++, Java code from the given Flowchart.

 One can download official free version from URL:


http://raptor.martincarlisle.com/
Sample Flowchart using
RAPTOR
This flowchart will read time from user and based on entered time, it will display Good morning /
Good Day message.
Assignment
 Display Good morning and Good Day message based on given time
 Check positive and negative number
 Odd or Even Number using modulus (%) operator
 Distance between two points
 Print 10, 9, 8, .... 1 using Raptor tool
 Print Series of Numbers Divisible by 3
 Print sum of the digits of a number
 Factorial of a given number

You might also like