ENGR112 - Lecture 9
ENGR112 - Lecture 9
FlowChart
Problem solving
n Implementation (coding)
n Testing
Slide 2
Algorithm
n Algorithm
n A procedure determining the
n What? are the actions to be executed
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:
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
Slide 12
Z=X+Y
Slide 13
Flowcharts of Control Structures
n control structures:
n selection structure
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:
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_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
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