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

LECTURE-3 Algorithm & Flowchart

Uploaded by

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

LECTURE-3 Algorithm & Flowchart

Uploaded by

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

CSE

-271
Course Title: Introduction to Computer Programming

Presented By

Saadman Sakib

Lecturer,
Department of CSE,
CUET
Algorithm

Flowchart

Slides borrowed from Instructor: Wajih Alouini


Flowchart Symbols (Mostly Used)
Symbol Name Function
Start/end An oval represents start or end point

Arrows An arrow shows the relationship between


the representative shapes
Input/Output A parallelogram represents input or
output

Process A rectangle represents a process

Decision A diamond indicates a decision

Connect A circle connect line to process


START
What is a Flowchart?
Display message
“How many
hours did you
work?”

• A flowchart is a diagram that Read Hours

depicts the “flow” of a program.


Display message
• The figure shown here is a “How much do
you get paid per
flowchart for pay-calculating hour?”

program.
Read Pay Rate

Multiply Hours
by Pay Rate.
Store result in
Gross Pay.

Display Gross
Pay

END
Rounded
Basic Flowchart Symbols START Rectangle

Display message
“How many
hours did you
work?”

• Notice there are three types Read Hours

of symbols in this Display message


“How much do
flowchart: you get paid per Parallelogra
hour?” m
– rounded rectangles
– parallelograms Read Pay Rate

– a rectangle Multiply Hours

• Each symbol represents a


by Pay Rate.
Rectangle Store result in
Gross Pay.
different type of operation.
Display Gross
Rounded Pay
Rectangle
END
START Terminal
Basic Flowchart Symbols
Display message
“How many
hours did you
work?”

• Terminals Read Hours

– represented by rounded Display message


“How much do
rectangles you get paid per
hour?”
– indicate a starting or
ending point Read Pay Rate

Multiply Hours
by Pay Rate.
START Store result in
Gross Pay.

Display Gross
Pay

END Terminal
END
Basic Flowchart Symbols START

Display message
“How many
hours did you
work?”

• Input/output Operations Read Hours

– represented by parallelograms
Display message
– indicate an input or output “How much do Input/Output
you get paid per
operation hour?” Operation

Read Pay Rate

Multiply Hours
by Pay Rate.
Display message Store result in
Gross Pay.
“How many
Read Hours
hours did you Display Gross
Pay
work?”
END
START
Basic Flowchart Symbols
Display message
“How many
hours did you
work?”

• Processes Read Hours

– represented by rectangles
Display message
– indicates a process such as a “How much do
you get paid per
mathematical computation or hour?”
variable assignment
Read Pay Rate

Multiply Hours
by Pay Rate.
Process Store result in
Multiply Hours Gross Pay.
by Pay Rate.
Store result in Display Gross
Pay
Gross Pay.
END
Stepping Through
Stepping Through the
START
Output
Operation

the Flowchart
Display message
“How many
Flowchart hours did you
work?”

Read Hours
How many
hours did
you work?
Display message
“How much do
you get paid per
hour?”

Read Pay Rate

Multiply Hours
by Pay Rate.
Store result in
Variable Contents: Gross Pay.

Hours: ? Display Gross


Pay Rate: ? Pay

Gross Pay: ? END


Stepping Through the START
Flowchart
Display message
“How many
hours did you
work?”

How many
Input Read Hours
hours did Operation
you work?
(User types Display message
40
40) “How much do
you get paid per
hour?”

Read Pay Rate

Multiply Hours
by Pay Rate.
Store result in
Variable Contents: Gross Pay.

Hours: 40
Display Gross
Pay Rate: ? Pay

Gross Pay: ? END


Stepping Through
Stepping Through the
START

the Flowchart
Display message
“How many
Flowchart hours did you
work?”

Read Hours
How much
do you get
paid per
Display message
hour?
“How much do
Output you get paid per
Operation hour?”

Read Pay Rate

Multiply Hours
by Pay Rate.
Store result in
Variable Contents: Gross Pay.

Hours: 40
Display Gross
Pay Rate: ? Pay

Gross Pay: ? END


Stepping Through
Stepping Through the
START

the Flowchart
Display message
“How many
Flowchart hours did you
work?”

Read Hours
How much
do you get
paid per
Display message
hour? 20
“How much do
you get paid per
hour?”

Input Read Pay Rate


Operation
(User types Multiply Hours
20) by Pay Rate.
Store result in
Variable Contents: Gross Pay.

Hours: 40
Display Gross
Pay Rate: 20 Pay

Gross Pay: ? END


START

Stepping Through the Display message


“How many
Flowchart hours did you
work?”

Read Hours
How much
do you ge
paid per
Display message
hour?
“How much do
you get paid per
hour?”

Read Pay Rate

Multiply Hours
Process: The by Pay Rate.
Store result in
Variable Contents: product of 40
times 20 is
Gross Pay.

Hours: 40 stored in
Gross Pay Display Gross
Pay Rate: 20 Pay

Gross Pay: 800 END


START

Stepping Through the Display message


“How many
Flowchart hours did you
work?”

Read Hours
Your gross
pay is 800
Display message
“How much do
you get paid per
hour?”

Read Pay Rate

Multiply Hours
by Pay Rate.
Store result in
Variable Contents: Gross Pay.

Hours: 40
Output Display Gross
Pay Rate: 20 Operation Pay

Gross Pay: 800 END


Four Flowchart Structures

• Sequence
• Decision
• Repetition
• Case
Sequence Structure

• A series of actions are performed in sequence


• The pay-calculating example was a sequence flowchart.
Decision Structure

• A new symbol, the diamond, indicates a yes/no question. If the


answer to the question is yes, the flow follows one path. If the
answer is no, the flow follows another path

NO YES
Decision Structure

• The flowchart segment below shows how a decision structure


is expressed in C++ as an if/else statement.

Flowchart
C++ Code
NO YES
if (x < y)
x < y?
a = x * 2;
else
Calculate a Calculate a
as x plus y. as x times 2. a = x + y;
Decision Structure

• The flowchart segment below shows a decision structure with


only one action to perform. It is expressed as an if statement in
C++ code.
Flowchart
C++ Code
NO YES
if (x < y)
x < y?
a = x * 2;

Calculate a
as x times 2.
Repetition Structure

• A repetition structure represents part of the program that


repeats. This type of structure is commonly known as a loop.
Repetition Structure

• The flowchart segment below shows a repetition structure


expressed in C++ as a while loop.

Flowchart C++ Code

while (x < y)

YES x++;
x < y? Add 1 to x
Controlling a Repetition Structure

• The action performed by a repetition structure must eventually


cause the loop to terminate. Otherwise, an infinite loop is
created.
• In this flowchart segment, x is never changed. Once the loop
starts, it will never end.
• QUESTION: How can this
flowchart be modified so
it is no longer an infinite
YES
loop? x < y? Display x
Controlling a Repetition Structure

• ANSWER: By adding an action within the repetition that


changes the value of x.

YES
x < y? Display x Add 1 to x
Case Structure

If years_employed = 2, If years_employed = 3,
bonus is set to 200 bonus is set to 400
If years_employed = 1, If years_employed is
CASE
bonus is set to 100 years_employed any other value, bonus
is set to 800

1 2 3 Other

bonus = 100 bonus = 200 bonus = 400 bonus = 800


The biggest of three inputted numbers

Flowchart START

Read A, B, C

Yes Yes No Yes


A>C? A>B? B>C?

No No

PrintA Print C Print B

END
The biggest of three inputted numbers
Algorithm
Step 1: Input A, B, C
Step 2: if (A>B) then
if (A>C) then
MAX A [A>B,A>C]
else
MAX C [C>A>B]
endif
else
if (B>C) then
MAX B [B>A, B>C]
else
MAX C [C>B>A]
endif
endif
Step 3: Print “The largest number is”, MAX
The biggest of three inputted numbers
#include <stdio.h>
int main( )
{
float A,B,C,MAX;
SOURCE CODE printf("Enter the values of a,b and c:\n");
scanf("%f%f%f",&A,&B,&C);
if(A>B)
{
if(A>C)
MAX=A;
else
MAX=C; }
else
{
if(B>C)
MAX=B;
else
MAX=C; }
printf("Biggest Number=%f\n",MAX);
return 0; }
Leap Year

Start
procedure leap_year()
Step 1 → Take integer variable year
Step 2 → Assign value to the variable IF year%4 = 0 AND
Step 3 → Check if year is divisible by 4 but year%100 != 0 OR
not 100, DISPLAY "leap year“ year%400 = 0
Step 4 → Check if year is divisible by 400, PRINT year is leap
DISPLAY "leap year" ELSE
Step 5 → Otherwise, DISPLAY "not leap PRINT year is not leap
year" STOP END IF
End
end procedure

Algorithm Pseudocode
https://www.tutorialspoint.com/learn_c_by_examples/leap_year_program_in_c.htm
Flowchart

https://www.tutorialspoint.com/learn_c_by_examples/leap_year_program_in_c.htm
Source Code
#include <stdio.h>
int main() {
int year;
while(scanf(“%d”,&year)!=EOF)
{
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
}
return 0;
}
Advantages and Disadvantages of Flow Chart

Advantages
1. Communication: Flow Charts are better way of communicating the logic of a system to all
concerned.
2. Effective Analysis: With the help of flow chart, problem can be analyzed in more effective way.
3. Proper Documentation: Program flow harts serve as a good program documentation, which is
needed for various purposes.
4. Efficient Coding: The flow charts act as a guide or blueprint during the systems analysis and
program development phase.
5. Proper Debugging: The flow chart helps in debugging process.
6. Efficient Program Maintenance: The maintenance of operating program becomes easy
• with the help of flow chart. It helps the programmer to put efforts more efficiently on that part.

Disadvantages
1. Complex logic: Sometimes, the program logic is quite complicated. In that case, flow chart
becomes complex and clumsy.
2. Alterations and Modifications: If alterations are required the flow chart may require re-
drawing completely.
3. Reproduction: As the flow chart symbols cannot be typed, reproduction of flow chart
becomes a problem.

You might also like