CS 240 Computer Programming 1
description
Transcript of CS 240 Computer Programming 1
![Page 1: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/1.jpg)
1
CS 240COMPUTER
PROGRAMMING 1
Flowcharts
![Page 2: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/2.jpg)
2
ALGORITHMAn informal definition of an algorithm is:
a step-by-step method for solving a problem or doing a task.
![Page 3: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/3.jpg)
3
ALGORITHMA 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.
![Page 4: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/4.jpg)
4
HOW TO UNDERSTAND THE PROBLEM?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)
![Page 5: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/5.jpg)
5
TOOLS
Flowcharts
There are two commonly used tools to help to document program logic (the algorithm)
Pseudo code
![Page 6: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/6.jpg)
6
FLOWCHART
The production flowchart is a visual representation of the sequence of the program. It shows what comes first, second, third, etc
Definition
A flowchart indicates:The steps to be taken in order to solve a
problem.The order or the sequence of these steps.
![Page 7: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/7.jpg)
7
FLOWCHART RULES1. 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.
![Page 8: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/8.jpg)
8
SYMBOLSSymbol 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
![Page 9: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/9.jpg)
9
1.SIMPLE SEQUENTIAL FLOWCHART
Construct a flow chart that prints "Hello, World"?
Example 1
![Page 10: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/10.jpg)
10
1.SIMPLE SEQUENTIAL FLOWCHART
Step 1- Start
Algorithm
Step 2- Print "Hello, World"Step 3- Stop
![Page 11: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/11.jpg)
11
1.SIMPLE SEQUENTIAL FLOWCHART
Start
Print“Hello, World”
Stop
Flowchart
![Page 12: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/12.jpg)
12
1.SIMPLE SEQUENTIAL FLOWCHART
Construct a flow chart that finds the sum of two numbers.
Example 2
![Page 13: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/13.jpg)
13
1.SIMPLE SEQUENTIAL FLOWCHART
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+BStep 5- Print CStep 6- Stop
![Page 14: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/14.jpg)
14
1.SIMPLE SEQUENTIAL FLOWCHART Flowchart Start
Read A
Read B
C= A+B
Print C
Stop
![Page 15: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/15.jpg)
15
1.SIMPLE SEQUENTIAL FLOWCHART
Construct a flow chart that finds the sum, average and product of three numbers.
Example 3
![Page 16: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/16.jpg)
16
1.SIMPLE SEQUENTIAL FLOWCHART
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, PStep 7- Stop
S: Sum (X+Y+Z)
A: Average (S/3)
P: Product (X*Y*Z)
Step 5- Calculate P = X*Y*Z
![Page 17: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/17.jpg)
17
1.SIMPLE SEQUENTIAL FLOWCHART Flowchart Start
Read X,Y,Z
S= X+Y+ZA=S/3
P=X*Y*Z
Print S,A,P
Stop
![Page 18: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/18.jpg)
18
1.SIMPLE SEQUENTIAL FLOWCHART
Construct a flow chart that finds the difference and the division of two numbers and display the
result
Example 4
![Page 19: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/19.jpg)
19
1.SIMPLE SEQUENTIAL FLOWCHART
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: DivisionStep 5- Print D,V
![Page 20: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/20.jpg)
20
1.SIMPLE SEQUENTIAL FLOWCHART Flowchart
Start
Read N1, N2
D= N1 –N2V=N1/N2
Print D,V
Stop
![Page 21: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/21.jpg)
21
1.SIMPLE SEQUENTIAL FLOWCHART
Construct a flow chart that finds the circle area and circumference of a circle where R (radius) is given
Example 5 Exercise
![Page 22: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/22.jpg)
22
1.SIMPLE SEQUENTIAL FLOWCHART
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
![Page 23: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/23.jpg)
23
2. BRANCHED FLOWCHARTS
Construct a flow chart for the following function
Example 1
F(x) = { XX>=0-X X<0
![Page 24: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/24.jpg)
24
2. BRANCHED FLOWCHARTS
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
![Page 25: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/25.jpg)
25
2. BRANCHED FLOWCHARTS
FlowchartStart
Read X
Print F
Stop
F=-X F=XX>=
0YESNO
![Page 26: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/26.jpg)
26
2. BRANCHED FLOWCHARTS
Trace the following flowchart and write the output of it.
Example 2
1. When X = 20
2. When X = -10
![Page 27: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/27.jpg)
27
2. BRANCHED FLOWCHARTS
FlowchartStart
Read X
Print X,W
Stop
W=2*X-1 W=X+1X? >00>
=0W=SIN(X)
+5
![Page 28: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/28.jpg)
28
2. BRANCHED FLOWCHARTS
Result
X= 20W= 21
When X=20
X= -10W= -21
When X=-10
![Page 29: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/29.jpg)
29
2. BRANCHED FLOWCHARTS
Draw a flowchart that shows the traffic light processing
Example 3 Exercise
![Page 30: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/30.jpg)
30
2. BRANCHED FLOWCHARTS
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- StopStep 6- if C is GREEN then Print PASS
Step 3- make a Decision (what is c)
![Page 31: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/31.jpg)
31
3. LOOP FLOWCHARTS
Trace the following flowchart and write the output of it.
Example 1
![Page 32: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/32.jpg)
32
3. LOOP FLOWCHARTSFlowchart Start
StopN=N+3
While N<=7
F
T
N=1
Print N
![Page 33: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/33.jpg)
33
3. LOOP FLOWCHARTS
Result
N Loop1 14 27 3
![Page 34: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/34.jpg)
34
3. LOOP FLOWCHARTS
Trace the following flowchart and write the output of it.
Example 2
![Page 35: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/35.jpg)
35
3. LOOP FLOWCHARTSFlowchart Start
Print avg
StopSum= X + Sum
Increment i
avg=Sum/10
While i<10
F
T
i=0Sum=0
Read X
![Page 36: CS 240 Computer Programming 1](https://reader035.fdocuments.net/reader035/viewer/2022062310/568164de550346895dd73b56/html5/thumbnails/36.jpg)
36
3. LOOP FLOWCHARTS
ResultLoop Read X Sum i1 3 3 12 4 7 23 1 8 3
4 10 18 45 7 25 56 5 30 67 3 33 78 8 41 89 4 45 910 5 50 10
Avg =50/10 =5