Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240 C OMPUTER P ROGRAMMING 1 Flowcharts 1. A LGORITHM An informal definition of an algorithm is: 2 a step-by-step method for solving a problem or doing.

Similar presentations


Presentation on theme: "CS 240 C OMPUTER P ROGRAMMING 1 Flowcharts 1. A LGORITHM An informal definition of an algorithm is: 2 a step-by-step method for solving a problem or doing."— Presentation transcript:

1 CS 240 C OMPUTER P ROGRAMMING 1 Flowcharts 1

2 A LGORITHM An informal definition of an algorithm is: 2 a step-by-step method for solving a problem or doing a task.

3 A LGORITHM 3 A step-by-step problem-solving procedure An algorithm is a sequence of unambiguous instructions for solving a problem. The number of steps of an algorithm will be countable and finite. It is a sequence of instructions (or set of instructions) to make a program more readable; a process used to answer a question.

4 H OW TO UNDERSTAND THE PROBLEM ? 4 Define the problem Analyze the problem Develop an algorithm/method of solution Write a computer program corresponding to the algorithm Test and debug the program Document the program (how it works and how to use it)

5 T OOLS 5 Flowcharts There are two commonly used tools to help to document program logic (the algorithm) Pseudo code

6 F LOWCHART The production flowchart is a visual representation of the sequence of the program. It shows what comes first, second, third, etc 6 Definitio n A flowchart indicates: The steps to be taken in order to solve a problem. The order or the sequence of these steps.

7 F LOWCHART R ULES 7 1. Use only one start and one stop per flowchart, --that is, one way in and one way out of the flowchart. 2. The logic flow of the solution is displayed from top to bottom and from left to right. 3. Use the appropriate symbol for each type of operation. 4. Use arrows when moving to another part of the flowchart rather than lines. 5. Do not leave dead-ends--that is, a part of a question unanswered.

8 S YMBOLS 8 Symbol Description TERMINAL - To start or end a flowchart INPUT / OUTPUT - Used with Read, Input, Print and other I/O commands. PROCESSING - Used for operations done inside the computer. Such as calculations, storing and moving of data. DECISION - Used to ask a question in programming. Questions are Yes/No format (Used with the If Statement). DIRECTION FLOW - Used to connect symbols and to represent the direction of flow. Lines should not cross each other. Arrowheads should be placed at the end close to the symbol. Connector - or joining of two parts of program

9 1.S IMPLE SEQUENTIAL F LOWCHART 9 Construct a flow chart that prints "Hello, World"? Example 1

10 1.S IMPLE SEQUENTIAL F LOWCHART 10 Step 1- Start Algorithm Step 2- Print "Hello, World" Step 3- Stop

11 1.S IMPLE SEQUENTIAL F LOWCHART 11 Start Print “Hello, World” Stop Flowchart

12 1.S IMPLE SEQUENTIAL F LOWCHART 12 Construct a flow chart that finds the sum of two numbers. Example 2

13 1.S IMPLE SEQUENTIAL F LOWCHART 13 C: Sum (A+B) Variables B: Second Number A: First Number Step 3- Read B Algorithm Step 2- Read A Step 1- Start Step 4- Calculate C = A+B Step 5- Print C Step 6- Stop

14 1.S IMPLE SEQUENTIAL F LOWCHART 14 Flowchart Start Read A Read B C= A+B Print C Stop

15 1.S IMPLE SEQUENTIAL F LOWCHART 15 Construct a flow chart that finds the sum, average and product of three numbers. Example 3

16 1.S IMPLE SEQUENTIAL F LOWCHART 16 Z: Third Number Variables Y: Second Number X: First Number Step 3- Calculate S = X+Y+Z Algorithm Step 2- Read X, Y, Z Step 1- Start Step 4- Calculate A = S/3 Step 6- Print S, A, P Step 7- Stop S: Sum (X+Y+Z) A: Average (S/3) P: Product (X*Y*Z) Step 5- Calculate P = X*Y*Z

17 1.S IMPLE SEQUENTIAL F LOWCHART 17 Flowchart Start Read X,Y,Z S= X+Y+Z A=S/3 P=X*Y*Z Print S,A,P Stop

18 1.S IMPLE SEQUENTIAL F LOWCHART 18 Construct a flow chart that finds the difference and the division of two numbers and display the result Example 4

19 1.S IMPLE SEQUENTIAL F LOWCHART 19 D: Difference Variables N2 : Second Number N1 : First Number Step 3- Calculate D = N1-N2 Algorithm Step 2- Read N1, N2 Step 1- Start Step 4- Calculate V = N1/N2 Step 6- Stop V: Division Step 5- Print D,V

20 1.S IMPLE SEQUENTIAL F LOWCHART 20 Flowchart Start Read N1, N2 D= N1 –N2 V=N1/N2 Print D,V Stop

21 1.S IMPLE SEQUENTIAL F LOWCHART 21 Construct a flow chart that finds the circle area and circumference of a circle where R (radius) is given Example 5 Exercise

22 1.S IMPLE SEQUENTIAL F LOWCHART 22 A: Area Variables PI: PI = 3.14 R : Radius Step 3- Calculate A = PI*(R) 2 Algorithm Step 2- Read R Step 1- Start Step 4- Calculate C = 2*PI*R Step 6- Stop C: Circumference Step 5- Print R, A, C

23 2. B RANCHED F LOWCHARTS 23 Construct a flow chart for the following function Example 1 F(x) = { XX>=0 -X X<0

24 2. B RANCHED F LOWCHARTS 24 Variables X : Number Step 3- if X >=0 then F =X Algorithm Step 2- Read X Step 1- Start Step 4- if X <0 then F =-X Step 6- Stop Step 5- Print F F: function of X

25 2. B RANCHED F LOWCHARTS 25 Flowchart Start Read X Print F Stop F=-X F=X X>=0 YES NO

26 2. B RANCHED F LOWCHARTS 26 Trace the following flowchart and write the output of it. Example 2 1. When X = When X = -10

27 2. B RANCHED F LOWCHARTS 27 Flowchart Start Read X Print X,W Stop W=2*X-1 W=X+1 X? >0 0> =0 W=SIN(X)+5

28 2. B RANCHED F LOWCHARTS 28 Result X= 20 W= 21 When X=20 X= -10 W= -21 When X=-10

29 2. B RANCHED F LOWCHARTS 29 Draw a flowchart that shows the traffic light processing Example 3 Exercise

30 2. B RANCHED F LOWCHARTS 30 Variables C : Traffic light color Step 4- if C is RED then Print STOP Algorithm Step 2- Read C Step 1- Start Step 5- if C is YELLOW then Print WAIT Step 7- Stop Step 6- if C is GREEN then Print PASS Step 3- make a Decision (what is c)

31 3. L OOP F LOWCHARTS 31 Trace the following flowchart and write the output of it. Example 1

32 3. L OOP F LOWCHARTS 32 Flowchart Start Stop N=N+3 While N<=7 F T N=1 Print N

33 3. L OOP F LOWCHARTS 33 Result NLoop

34 3. L OOP F LOWCHARTS 34 Trace the following flowchart and write the output of it. Example 2

35 3. L OOP F LOWCHARTS 35 Flowchart Start Print avg Stop Sum= X + Sum Increment i avg=Sum/10 While i<10 F T i=0 Sum=0 Read X

36 3. L OOP F LOWCHARTS 36 Result LoopRead XSumi Avg =50/10 =5


Download ppt "CS 240 C OMPUTER P ROGRAMMING 1 Flowcharts 1. A LGORITHM An informal definition of an algorithm is: 2 a step-by-step method for solving a problem or doing."

Similar presentations


Ads by Google