2ALGORITMS 
& 
1 1 FLOWCHARTS
2 
ALGORITHMS 
 Algorithm: Is an ordered set of unambiguous, 
executable steps defining a terminating 
process 
 Unambiguous:during the execution of an 
algorithm, the information in the state of the 
process must be sufficient to determine 
uniquely and completely the action required 
by each step
ALGORITHMS 
Or 
 The execution of each step in an 
algorithm does not require creative 
skills. Rather, it requires only the ability 
to follow directions. 
3
4 
Control Structures 
 The key to clear algorithm design lies in 
limiting the control structure to only 
three structures: 
Sequence, Selection, & Repetition 
 The following are the basic C and C++ 
control structures illustrated in flowchart 
and statement form:
5 
1) Sequence : 
Example: 
C=A+B
6 
2) Selection: if & if/else 
if/else Structure(Double 
Selection) 
T 
F 
if Structure (Single 
Selection) 
F T
7 
2) Selection: switch 
Break 
Break 
T 
F 
T 
F 
Switch Structure(Multiple Selection)
3) Repetition: while & do/while 
T 
8 
F T 
while Structure 
F 
do/while structure
9 
3) Repetition: for loop 
T 
F 
for Structure
10 
Pseudocode: 
 There are several ways to represent 
algorithms. Such as pseudocode, 
flowcharts, … 
 Pseudocode: Individual operations 
are described briefly and precisely in 
English standard statements consists 
of nouns and verbs
Pseudocode: 
For example, to evaluate the formula for 
converting temperature reading from Celsius 
to Fahrenheit: 
11 
F= (9 /5) C + 32 
Solution: Algorithm in pseudocode: 
Get the value of C 
Calculate F 
Print F
While the flowchart used is shown bellow 
(the used control structure is Sequence ): 
12 
Start 
Read C 
F=(9/5)C+32 
Print F 
End
Example 1 : Write an algorithm and draw a flowchart to 
read the grades of five students , find the average of 
these grades and print it 
13 
Algorithm: 
Initialize counter to 1 
Initialize total to zero 
Initialize average to zero 
While counter is less than or equal 5 
Input the grade 
Add grade into total 
average=total/5 
Print average 
Counter=1 
total=0,average=0 
If 
Counter<5 
Read Grade Total=Total+grade 
Average=Total/5 
End 
START 
T 
F 
Print Average 
Counter=Counter+1
Example2: A student took final exams of 10 courses. 
Write an algorithm and draw a flowchart to read the 
grades and find their sum and print it. 
14 
Algorithm: 
Initialize Count to 1 
Initialize Total to Zero 
For Count=1 to 10 
Read a Grade 
Add Grade to Total 
Print Total 
START 
Count=1 
Total=0 
If 
Count<10 
Read Grade Print Total 
Total=Total+Grade 
END 
Count=Count+1 
T 
F
Example 3 : Write an algorithm and draw a flowchart to 
read a grade, test it if PASS(more than or equal 50) or 
FAIL, and print the result . 
15 
Algorithm : 
Read a Grade 
If Grade more than or equal 50 
Print PASS 
Else Print FAIL 
START 
Read Grade 
If 
F T 
Grade>50 
Print”FAIL” Print”PASS” 
END

algo

  • 1.
    2ALGORITMS & 11 FLOWCHARTS
  • 2.
    2 ALGORITHMS Algorithm: Is an ordered set of unambiguous, executable steps defining a terminating process  Unambiguous:during the execution of an algorithm, the information in the state of the process must be sufficient to determine uniquely and completely the action required by each step
  • 3.
    ALGORITHMS Or The execution of each step in an algorithm does not require creative skills. Rather, it requires only the ability to follow directions. 3
  • 4.
    4 Control Structures  The key to clear algorithm design lies in limiting the control structure to only three structures: Sequence, Selection, & Repetition  The following are the basic C and C++ control structures illustrated in flowchart and statement form:
  • 5.
    5 1) Sequence: Example: C=A+B
  • 6.
    6 2) Selection:if & if/else if/else Structure(Double Selection) T F if Structure (Single Selection) F T
  • 7.
    7 2) Selection:switch Break Break T F T F Switch Structure(Multiple Selection)
  • 8.
    3) Repetition: while& do/while T 8 F T while Structure F do/while structure
  • 9.
    9 3) Repetition:for loop T F for Structure
  • 10.
    10 Pseudocode: There are several ways to represent algorithms. Such as pseudocode, flowcharts, …  Pseudocode: Individual operations are described briefly and precisely in English standard statements consists of nouns and verbs
  • 11.
    Pseudocode: For example,to evaluate the formula for converting temperature reading from Celsius to Fahrenheit: 11 F= (9 /5) C + 32 Solution: Algorithm in pseudocode: Get the value of C Calculate F Print F
  • 12.
    While the flowchartused is shown bellow (the used control structure is Sequence ): 12 Start Read C F=(9/5)C+32 Print F End
  • 13.
    Example 1 :Write an algorithm and draw a flowchart to read the grades of five students , find the average of these grades and print it 13 Algorithm: Initialize counter to 1 Initialize total to zero Initialize average to zero While counter is less than or equal 5 Input the grade Add grade into total average=total/5 Print average Counter=1 total=0,average=0 If Counter<5 Read Grade Total=Total+grade Average=Total/5 End START T F Print Average Counter=Counter+1
  • 14.
    Example2: A studenttook final exams of 10 courses. Write an algorithm and draw a flowchart to read the grades and find their sum and print it. 14 Algorithm: Initialize Count to 1 Initialize Total to Zero For Count=1 to 10 Read a Grade Add Grade to Total Print Total START Count=1 Total=0 If Count<10 Read Grade Print Total Total=Total+Grade END Count=Count+1 T F
  • 15.
    Example 3 :Write an algorithm and draw a flowchart to read a grade, test it if PASS(more than or equal 50) or FAIL, and print the result . 15 Algorithm : Read a Grade If Grade more than or equal 50 Print PASS Else Print FAIL START Read Grade If F T Grade>50 Print”FAIL” Print”PASS” END