Module 2
Module 2
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.
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.