0% found this document useful (0 votes)
39 views29 pages

Algorithms and Flowcharts Overview

This document contains lecture notes on basic algorithms and flowcharts. It introduces variables, data types, valid and invalid variable names. It provides examples of algorithms to add two numbers, find the average of 3 numbers, calculate the area of a circle, and convert days to months. Relational operators and flowchart symbols are defined. Examples are given for algorithms to calculate the area of a rectangle, determine the largest of two values, and calculate a student's grade. Pseudocode and detailed algorithms are provided for each example.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views29 pages

Algorithms and Flowcharts Overview

This document contains lecture notes on basic algorithms and flowcharts. It introduces variables, data types, valid and invalid variable names. It provides examples of algorithms to add two numbers, find the average of 3 numbers, calculate the area of a circle, and convert days to months. Relational operators and flowchart symbols are defined. Examples are given for algorithms to calculate the area of a rectangle, determine the largest of two values, and calculate a student's grade. Pseudocode and detailed algorithms are provided for each example.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Lecture Notes 7

Department of Computer Science and Engineering


Sec-K,O,E,R,V
Course: CSC103 Fundamental Computer and
Applications

Basic Algorithm
Given By- Lecturer Fardina Fathmiul Alam
CSE Dept. IUBAT

Variable
A variable is a data name that may be
used to store a data value.
Variable hold a space in the computer
memory. Variable a declaring means
its hold a position in the computer
memory:

Valid name:
Average, hight,Counter_1, x1,ph_value
Invalid Names:
123,(area),25th,%

Algorithm: Adding Two


Numbers

Start
Declare 3 variables x,y,z of int type
Input the value on x
Input the value on y
Calculate z=x+y and store the
result on z
Print z
stop

Declare 3 variables means:


x
y
z

Input the value on x means:


x
y
z

Input the value on y means:


x
y
z

Calculate z=x+y means:


x
y
z

Store the result on z means:


x
y
z

12

Algorithm: Find out average of


3 numbers

Start
Declare 4 variables a,b,c,d of int type
Put the values on a,b,c
Calculate d=(a+b+c)/3
Store the result on d
Print d
stop

Algorithm: Find out the area of


a circle
Start
Declare 2 variables a and r of float
type
Put the value on r
Calculate a=3.1416*(r2)
Store the result on a
Print a
Stop

Algorithm: Convert day to


month

Start
Declare two variables d and m.
Put the value on d.
Calculate m=d/30.
Store the result on m
Print m
Stop

EXERSICE
Ask for float number and divide
second by first
Convert meter to kilometer

Algorithm: Read an integer


and print next & previous
integer

Start
Declare three variables c, x, y
Put the value on c.
Calculate x=c+1 (next int value)
Print x.
Calculate y=c-1 (prev int value)
Print y.
Stop

Relational Operators
Relational Operators

Operator

Description

>

Greater than

<

Less than

Equal to

Greater than or equal to

Less than or equal to

Not equal to

FLOW CHART

Flow Chart
A Flowchart
Graphical Representation of Algorithm
shows logic of an algorithm
shows individual steps and their
interconnections

Flowchart Symbols
Basic

Example 3
Write an algorithm and draw a
flowchart that will read the two sides
of a rectangle and calculate its area.
Pseudocode
Input the width (W) and Length (L) of a
rectangle
Calculate the area (A) by multiplying L
with W
Print A

Example 3
Algorithm
Step 1:
Step 2:
Step 3:

START

Input W,L
AL x W
Print A

Input
W, L

ALxW

Print
A

STOP

IFTHENELSE
STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif

IFTHENELSE
STRUCTURE
The algorithm for the flowchart is as
follows:
If A>B then
print A
Y
N
is
A>B
else
print B
Print
Print
A
B
endif

Example 5
Write an algorithm that reads two values,
determines the largest value and prints the
largest value.
ALGORITHM
Step 1:
Input VALUE1, VALUE2
Step 2:
if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3:
Print The largest value is, MAX

Example 5
START

Input
VALUE1,VALUE2

is
VALUE1>VALUE2

MAX VALUE1

MAX VALUE2

Print
The largest value is,
MAX
STOP

Pseudocode & Algorithm


Example 1: Write an algorithm to
determine a students final grade
and indicate whether it is passing or
failing. The final grade is calculated
as the average of four marks.

Pseudocode & Algorithm


Pseudocode:
Input a set of 4 marks
Calculate their average by summing and
dividing by 4
if average is below 50
Print FAIL
else
Print PASS

Pseudocode & Algorithm


Detailed Algorithm

Step 1:
Input M1,M2,M3,M4 of float
type
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print FAIL
else
Print PASS
endif

Example
START

Step 1: Input M1,M2,M3,M4 of float type


Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE <50) then
Print FAIL
else
Print PASS
endif

Input
M1,M2,M3,M4

GRADE(M1+M2+M3+M4)/4

IS
GRADE<5
0

PRINT
PASS

PRINT
FAIL

STOP

The End

Common questions

Powered by AI

The use of a pseudo constant like π in area calculations is essential as it provides a precise value necessary for correctness in geometric computations. This illustrates the use of constants in programming to maintain consistency and accuracy, reducing the chance of erroneous calculations with arbitrary values. Constants also make code more understandable, as the purpose of π is clear and universally recognized .

Flowcharts are preferred when visualizing the flow of an algorithm is necessary, particularly for complex processes with many steps or decisions. They offer advantages such as ease of understanding through visual representation, quick identification of the flow and decision points, and simplification of communication between stakeholders who might not have technical backgrounds. This is especially useful in collaborative environments or during initial planning phases .

Variable naming conventions are essential in algorithm development as they improve code readability and maintainability by providing clear and meaningful descriptions of the data being manipulated. Invalid names, such as 123 or (area), introduce confusion and errors because they do not adhere to programming language rules or standards, making the code less intuitive and potentially causing syntax errors .

The IF–THEN–ELSE structure enhances decision-making in algorithms by enabling conditional branching based on certain conditions, allowing the program to execute different paths based on input data. For example, determining a student's pass or fail status involves calculating the average of four marks; if the average is below 50, it prints 'FAIL', otherwise 'PASS'. This conditional check provides a dynamic approach to handle situations differently based on specific criteria .

The algorithm to convert days into months includes declaring two variables (e.g., days and months), inputting the value of days, dividing this by 30 to estimate months, storing the result, and finally printing it. These steps are appropriate because they simplify the conversion process by assuming an average month length of 30 days, which is a common approximation used for such calculations .

To modify the algorithm for floats, the steps involve reading a float value, calculating the next integer by using the ceil function, and the previous integer using the floor function, then printing these results. This modification accommodates the continuous nature of float data and requires rounding to obtain integers .

Designing a flowchart for calculating the area of a rectangle visually represents the algorithm's logic by using symbols like ovals for start and stop, parallelograms for input and output, and rectangles for processing steps. Each step is connected with arrows indicating the sequence. The process involves inputting the width and length, computing the area by multiplying these values, and printing the result. This visual representation helps in understanding the stepwise procedure and logical flow of the algorithm .

Pseudocode is significant in bridging the gap between conceptualization and implementation by providing a language-agnostic way to describe algorithm logic. It allows developers to focus on logic without syntax concerns and serves as a communication tool to convey ideas clearly among team members with varying technical expertise. It also facilitates transition into actual code by outlining the logical framework necessary for implementation .

A flowchart displaying a conditional operation, like determining the maximum of two values, aids understanding by explicitly outlining the decision-making process. It uses diamonds to depict decision points where conditions are checked, and arrows guide the flow based on outcomes, either proceeding to actions related to the condition being met or not. This clarifies the logical steps and choices, making the algorithm more accessible and intuitive .

Relational operators play a critical role in algorithm development by allowing comparisons between values, essential for decision-making processes. They impact flowchart logic by dictating the paths taken based on the evaluated conditions, as seen in IF–THEN–ELSE structures where whether one value is greater than another determines the subsequent steps executed .

You might also like