0% found this document useful (0 votes)
22 views25 pages

ENGR112 - Lecture 9

The document discusses algorithms, pseudocode, and flowcharts. It provides examples of each and explains how to represent different control structures like if/else, loops, and switch statements in pseudocode and flowcharts. Key concepts covered include understanding the problem, designing a solution, and implementing code.

Uploaded by

mmhygbwsvg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views25 pages

ENGR112 - Lecture 9

The document discusses algorithms, pseudocode, and flowcharts. It provides examples of each and explains how to represent different control structures like if/else, loops, and switch statements in pseudocode and flowcharts. Key concepts covered include understanding the problem, designing a solution, and implementing code.

Uploaded by

mmhygbwsvg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Algorithm, Pseudocode and

FlowChart
Problem solving

n Understand the problem: Analysis


n Define inputs, outputs, process
n Define your Assumptions
n Need for control statement ?
n If , if-else, nested , condition(s)
n Loop ?

n Design a Solution for your problem: How?


n Algorithm
n Pseudocode
n FlowChart

n Implementation (coding)
n Testing
Slide 2
Algorithm
n Algorithm
n A procedure determining the
n What? are the actions to be executed

n How? Order in which these actions are to be

executed
Finding the largest integer
among five integers
Defining actions in FindLargest
algorithm
FindLargest refined
Generalization of FindLargest
Algorithm: Example
§ Design an algorithm to find the perimeter and
area of a rectangle
§ The perimeter and area of the rectangle are given
by the following formulas:

perimeter = 2 * (length + width)

area = length * width

Slide 8
Algorithm: Example
§ Prompt the user to enter the length
§ Read the length of the rectangle
§ Prompt the user to enter the width
§ Read the width of the rectangle
§ Find the perimeter using the following equation:
perimeter = 2 * (length + width)
§ Find the area using the following equation:
area = length * width
Slide 9
Pseudocode
n Algorithm gives details without using words related to
C++
n Pseudocode is an artificial, informal language used to
develop algorithms
n A mixture of English and a generic programming language
n Pseudocode simplifies algorithm design by allowing you
to ignore the specific syntax of the programming
language as you work out the details of the algorithm
n If the step is obvious, use C++
n If the step is difficult to express in C++, use English

Slide 10
Example
n Write an algorithm to change a numeric grade to
a pass/no pass grade, assume the pass grade is
70 and above.
n Begin
Read (Get) the number
if (number > 70) then
Set the grade to “pass”
else
Set the grade to “nopass”
End if
n End
Flowchart
n Flowchart is a graphical representation of an
algorithm

n A flowchart is a step by step solution of a problem,


using suitable geometric diagrams connected by
arrows (called flowlines) that show the flow of a
program.

Slide 12
Z=X+Y

Slide 13
Flowcharts of Control Structures
n control structures:
n selection structure

n if, if-else, if-else-if, switch

n repetition structure
n while, for, do .. while
Selection Structure: (if)
Syntax: Flowchart: if

if (expr1)
{
C++ block true
} expr1 {
block
}
Example: false

if( grade>=60)
cout<<"Passed\n";
Selection Structure: (if-else)
Syntax: Flowchart: if-else

if (expr1)
{ block1 }
false true
else
expr1
{ block2 }

{ block2} { block1}
Example:
if( grade>=60)
cout<<"Passed";
else
cout<<"Failed";
Selection Structure: (if-else-if)
Syntax: Example:

if (expr1) if( grade>=90)


{ block1 } cout<<"Your GPA is A."<<endl;
else if (expr2) else if (grade>=80)
{ block2 } cout<<"Your GPA is B."<<endl;
else if (expr3) else if (grade>=70)
{block 3} cout<<"Your GPA is C."<<endl;
else else if (grade>=60)
{block4} cout<<"Your GPA is D."<<endl;
else
{cout<<"Your GPA is F."<<endl;
cout<<"You will cry!"<<endl;}
Nested (if … else) Flowchart .
NO YES
g > 60

failed NO YES
g > 90

mark mark&
excellent

Slide 18
Selection Structure: (switch)
Syntax:

switch (expr1)
{
case val_1: block
break;
case val_2: block2
break;

default: block
}
Selection Structure: (switch)

val_1 val_2 val_4 val_5 default


expr1

Val_3
block1 block3 block5 block6 block
break; block4 break; break;
break;
break;
Repetition Structures
(while-loop)
Syntax: Flowchart: while-loop

while (expr1)
{C++ block}
true
Example: expr1 { C++block}
int num=0;
while( num<=100)
false
{
cout<<num*num;
cout<<"\n";
num+=2;
}
Repetition Structures
(do while-loop)
Syntax: Flowchart: do-while-loop

do
{C++ block true
}while (expr1);
Example: { C++block} expr1
int num=0;
do
false
{
cout<<num*num;
cout<<"\n";
num+=2;
} while( num<=100);
while vs. do-while
while-loop
do-while-loop
true
true
expr1 { C++block} { C++block} expr1

false false

Exercise: explain the difference between the two


Repetition Structures
(for loop)
Flowchart: for-loop
Syntax:

for (expr1;expr2;expr3)
{C++ block } { C++ block}

expr3 true
expr1
Example:
int num; expr2
for (num=0; num<=100; num+=2)
{ false
cout<<num*num;
cout<<"\n";
}
Flowchart & Pseudocode
Start
Flowchart
Pseudocode
Sum=0
Count = 0 BEGIN Average Grade
sum=0
count = 0
Input DO
Sum = Sum + Grade
Count = Count + 1
Grade INPUT grade
IF grade < 0 EXIT
sum = sum + grade
Yes More count = count +1
grades? END DO
IF count > 0 THEN
No average = sum/
count
Average = Sum/Count ELSE
average = 0
END IF
Stop END Average Grade

You might also like