Professional Documents
Culture Documents
Introduction to Programming C#
Edip ŞENYÜREK
1
Pseudo-code & Flowcharts
2
Flowcharts
• A flowchart is a structured map showing the steps of the
algorithm
• The following shapes are used within a flowchart to express
the flow of control
End
4
Flowchart for Example2:
Conversion from Fahrenheit-to-Celsius
Start
celsius = (fahrenheit-32)/1.8
End
5
Flowchart for Example3:
Computing sum, product and average
Start
average =sum/2
End
Flowchart for Example4: Computing
circumference and area of a circle
Start
circumference = 2*3.14*radius
Area = 3.14*radius*radius;
End 7
Flowchart for Example5:
Computing min and max of 2 numbers
Start
yes
number1 < number2 ?
no
min = number1
min = number2
max = number2
max = number1
yes
number1 < number2 ?
no
yes yes
number2 < number3 ? number1 < number3 ?
no no
min = number3 min = number2 min = number3 min = number1
Print min
9
End
Flowchart for Example7:
Computing min of 3 numbers
Start
min = number1
yes
number2 < min?
no min = number2
yes
number3 < min?
no min = number3
Print min 10
End
Flowchart Example 8
11
Speed Graph
12
Pseudo Code
• Variables
T: Time in minutes
V: The speed at time T, in km/h
• Algorithm
Step 1: Start
Step 2: Input T
Step 3: If T < 10 go to Step 4, otherwise go to Step 5
Step 4: V = 6T, go to Step 8
Step 5: If T < 25 go to Step 6, otherwise go to Step 7
Step 6: V = 60, go to Step 8
Step 7: V = 210 – 6T
Step 8: Output V
Step 9: End
13
Flowchart
Start
Read T
Y
V = 6T T < 10 ?
N
N
T < 25 ? V = 210 – 6T
Y
V = 60
Print V
14
End
Example 9
15
Algorithm
Step 1: Start
Step 2: Input A, B, and C values
Step 3: If A = B go to step 4, otherwise go to Step 5
Step 4: If A = C go to step 7, otherwise go to Step 8
Step 5: If A = C go to step 8, otherwise go to step 6
Step 6: If B = C go to Step 8, otherwise go to Step 9
Step 7: Print “Equilateral” and go to Step 10
Step 8: Print “Isosceles” and go to Step 10
Step 9: Print “Multilateral” and go to Step 10
Step 10: End
16
Start
Read A, B, C
Y Y N N N
A=C? A=B? A=C? B=C?
N Y Y
End 17
Example 10
• Calculate the real roots of the second order equation Ax 2
+ Bx +
C= 0.
• Inputs
• A: Coefficient of x2
• B: Coefficient of x
• C: Constant term
• Outputs
• X1: First root of the equation
• X2: Second root of the equation
• Temporary Variables:
• Δ = sqrt(B2 – 4AC)
18
Algorithm
Step 1: Start
Step 2: Input A, B and C
Step 3: Calculate Δ = B2 – 4AC
Step 4: If Δ < 0 go to Step 6, otherwise go to Step 5
Step 5: If Δ > 0 go to Step 7, otherwise go to Step 8
Step 6: Output “Complex Roots”. Go to step 13
Step 7: Output “Two Real Roots”. Go to step 9
Step 8: Output “Equal Roots”. Go to step 9
Step 9: Calculate X1 = (-b + sqrt(Δ))/(2A)
Step 10: Calculate X2=(-b - sqrt(Δ))/(2A)
Step 11: Output X1
Step 12: Output X2
Step 13: End
19
Start
Read A, B, C
Δ = B2 – 4AC
“Complex Y N N
Δ<0? Δ>0?
Roots”
Y
“Two Real “Equal
Roots” Roots”
X1 = (-b+√Δ)/(2A)
X2 = (-b-√Δ)/(2A)
Output X1 and X2
20
End
Example 11
• In a classroom of 10 students the ages of the students varies
between 18 and 20. Calculate the number of students at the ages
18, 19, and 20.
• Inputs:
• Ages of the 10 students
• Outputs:
• S18: Number of 18 year old students
• S19: Number of 19 year old students
• S20: Number of 20 year old students
• Temporary Variables:
• X: Student counter
• A: Age of a student
21
Algorithm
Step 1: Start
Step 2: Initialize S18, S19, S20, and X to 0.
Step 3: If X ≥ 10 go to step 13 otherwise continue
Step 4: Input a student age, A
Step 5: If A = 18 go to step 9
Step 6: If A = 19 go to step 10
Step 7: If A = 20 go to step 11
Step 8: Output “age error” and go to step 4
Step 9: Calculate S18 = S18 + 1, go to step 12
Step 10: Calculate S19 = S19 + 1, go to step 12
Step 11: Calculate S20 = S20 + 1
Step 12: X = X + 1, and go to step 3
Step 13: Output S18, S19 and S20
Step 14: End
22
Start
Output Y
S18, S19, X = 10 ?
and S20
N
Input A
End
Y
A = 18 ? S18 = S18 + 1
N
Y
A = 19 ? S19 = S19 + 1 X=X+1
N
“Age N Y
A = 20 ? S20 = S20 + 1 23
Error”
Example 12
• Output:
• F: Factorial of N
• Temporary Variable:
• Counter: Numbers from 1 to N
24
Algorithm
Step 1: Start
Step 2: Input N
Step 3: Initialize Counter and F to 1
Step 4: If Counter > N go to Step 7
Step 5: Calculate F = F × Counter
Step 6: Increment Counter and go to Step 4
Step 7: Output F
Step 8: End
25
Start
Input N
Counter = 1
F=1
N
Counter > N ? F = F × Counter
End
26
CII1SE15CI-Z17
Introduction to Programming C#
Edip ŞENYÜREK
Thank you!!!
27