CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart)...

12
CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science 1

Transcript of CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart)...

Page 1: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

CS-101

Lecture 10

Algorithm and Flow Chart Developing logic (Examples on

algorithm and flowchart) Department of Computer Science

College of Arts and Science

1

Page 2: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

Relational Operator

2

Operator Description

> Greater than

< Smaller than

≤ Smaller than or Equal

≥ Greater than or Equal

= Equal

≠ Not Equal

Page 3: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

If else structure

3

• The structure is as followsIf condition then

true alternative else

false alternativeendif

Page 4: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

If else structure

4

isA>B

Print BPrint A

Y N

Print A Print B

Page 5: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

Example (1)

5

Step 1: Start.

Step 2: Input M1,M2,M3,M4

Step 3: GRADE (M1+M2+M3+M4)/4

Step 4: if (GRADE < 60) then

Print “FAIL”

else

Print “PASS”

endif

Page 6: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

Example (2)

Write a algorithm to find out number is odd or even?• step 1 : start• step 2 : input number• step 3 : rem=number mod 2• step 4 : if rem=0 then• print "number even"• else• print "number odd"• endif• step 5 : stop

Mr.Mohammed Rahmath

Page 7: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

Chapter 2- Visual Basic Schneider 7

What is a flowchart?

• Diagram that visually represents the steps that the program performs to arrive at a solution.

• A popular logic tool used for showing an algorithm in graphical form.

Page 8: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

Chapter 2- Visual Basic Schneider 8

Continue flowchart• Programmer draws flowchart before coding.• Most common flowchart symbols are:

– Flowline: indicates flow of logic. (arrow )

– Terminal: represents beginning or end of task (oval )

– Input/Output: represents input or output operations (parallelogram )

– Processing: data manipulation operations (rectangle )

– Decision: used for logic or comparison operation (diamond )

Page 9: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

Chapter 2- Visual Basic Schneider 9

Example of Flowchart:Start

Initialize counterand sum to 0

Are there more data?

Get next grade

Increment counter

Add grade to sum

Yes

No

Average=sum/counter

Finish

This is a program to insert grades and calculate the average

Print the average

Page 10: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

Cont..

10

PRINT“PASS”

START

InputM1,M2,M3,M4

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

GRADE<60

PRINT“FAIL”

STOP

YN

Print “Fail”Print “Pass”

Page 11: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

Example (2)

11

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

Page 12: CS-101 Lecture 10 Algorithm and Flow Chart Developing logic (Examples on algorithm and flowchart) Department of Computer Science College of Arts and Science.

Thank you for your attention

12

Lecturer : Aasd Abd Elrashid