cs-1301 lecture 2 - faculty.psau.edu.sa filecs-1301 lecture 2 algorithms and flowcharts department...

22
CS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid [email protected] 1

Upload: others

Post on 02-Nov-2019

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

CS-1301

Lecture 2

Algorithms and flowcharts Department of Computer Science

College of Arts and Science

Lecturer : Mr. Asad Abd Elrashid

[email protected]

1

Page 2: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Content

2

Introduction.

Pseudocode.

Algorithm.

The Flowchart.

Control structure.

If structure.

Page 3: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Introduction

3

Computer programming can be divided into two

phases:

Problem solving phase

–Make an ordered sequence of steps that

solves a problem

– these sequence of steps is called an

algorithm

Implementation phase :

– implement using a programming language

Page 4: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Pseudocode

4

Artificial, informal language that helps us

develop algorithms

Similar to everyday English

Not actually executed on computers

Helps us “think out” a program before writing

it.

Page 5: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Algorithm

5

Step 1: input x,y

Step 2: sum x+y.

Step 3: print sum value.

Page 6: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

The Flowchart

6

Graphical representation of an algorithm.

Drawn using certain special-purpose symbols connected by arrows called flow lines.

–A flowchart must have a start and stop

–A steps in a flowchart must connect. Can’t leave a step “hanging” with no connection.

Page 7: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Flow chart symbols

7

Page 8: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Example 1

8

Input two numbers.

Multiply your numbers together.

Output Multiplication result.

Page 9: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Cont..

9

Step 1: Input x,y.

Step 2 : z = x*y.

step 3 : print z.

Page 10: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Flow chart

10

Start

z = x*y

Enter x,y

Print z

End

Page 11: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Control Structures

11

Sequential execution :

Statements executed one after the other in the

order written.

All programs written in terms of three control

structures

Sequence structures: Built into C. Programs

executed sequentially by default.

Page 12: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Cont..

12

Selection structures:

C has three types: if, if…else, and switch.

Repetition structures:

C has three types: while, do…while and for.

Page 13: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Sequence structures

13

Start

z = a+b

Enter a,b

Print z

End

Page 14: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Selection structures:

14

Used to choose among alternative options of

actions.

Step 1: start

Step 2: if(a=0) then

Step 3: print a.

Step 4: stop

Page 15: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

If structure

15

if

A=0

Print A

Y N

Print A

End

Page 16: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

If else structure

16

Specifies an action to be performed both when

the condition is true and when it is false.

Challenge :

Think about any example of if else structure.

Page 17: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Example (1)

17

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 18: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Cont..

18

PRINT

“PASS”

START

Input

M1,M2,M3,M4

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

GRADE<60

PRINT

“FAIL”

STOP

YN

Print “Fail”Print “Pass”

Page 19: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Example

19

Input two numbers.

Check which number is greater.

Output greater number.

Page 20: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Cont..

20

Step 1 : start.

Step 2 : input A and B.

Step 3 : if A is greater than B.

Step 4 : if “true” print A.

Step 5 : else print B.

Step 6 : stop.

Page 21: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Challenge

21

Change previous algorithm to flow chart?

Page 22: CS-1301 Lecture 2 - faculty.psau.edu.sa fileCS-1301 Lecture 2 Algorithms and flowcharts Department of Computer Science College of Arts and Science Lecturer : Mr. Asad Abd Elrashid

Thank you for your attention

22

Lecturer : Aasd Abd Elrashid