CII1SE15CI-Z17
Introduction to Programming C#
Edip ŞENYÜREK
1
Pseudo-code & Flowcharts
• The algorithms so far have been written in structural English
• This algorithm style is called pseudo-code
• Pseudo-code is usually a good way of beginning to design a
solution
• However, within a pseudo-code, the program flow is not always very
clear
• In order to express the flow of control in a much more lucid fashion,
flowcharts are used
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
Parallelograms show Rectangles show
Diamonds indicate
input and output steps processing steps
points of decision
Arrows indicate Start End
Circles are flow of control
Start/End
connectors of the algorithm 3
Flowchart for Making a Sandwich
Start
Get bread, peanut butter, jelly, knife and plate
Place 2 slices of bread on the table
Spread butter on one slice
Spread jelly on the other slice
Slap two slices together, sticky side in
Eat the sandwich
End
4
Flowchart for Example2:
Conversion from Fahrenheit-to-Celsius
Start
Prompt the user and get the
fahrenheit temperature to convert
celsius = (fahrenheit-32)/1.8
Print the fahrenheit
and celsius degrees
End
5
Flowchart for Example3:
Computing sum, product and average
Start
Prompt the user and get
number1 and number2
sum = number1 + number2
product = number1 * number2
average =sum/2
Print sum, product and average
End
Flowchart for Example4: Computing
circumference and area of a circle
Start
Prompt the user and
get radius of the circle
circumference = 2*3.14*radius
Area = 3.14*radius*radius;
Print circumference and
area of the circle
End 7
Flowchart for Example5:
Computing min and max of 2 numbers
Start
Prompt the user and get number1 and number2
yes
number1 < number2 ?
no
min = number1
min = number2
max = number2
max = number1
Print min and max
8
End
Flowchart for Example6:
Computing min of 3 numbers
Start
Prompt the user and
get number1, number2 and number3
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
Prompt the user and
get number1, number2 and number3
min = number1
yes
number2 < min?
no min = number2
yes
number3 < min?
no min = number3
Print min 10
End
Flowchart Example 8
• A car starts to move, accelerates during 10 minutes and its
speed becomes 60 km/hr. Then, it continues with this
speed during 15 minutes and finally slows down during 10
minutes and stops. What is the speed of the car at time T?
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
• Determine whether a triangle is equilateral, isosceles, or
multilateral when the lengths of its sides are given.
• Inputs
• A: Length of the first side
• B: Length of the second side
• C: Length of the third side
• Output
• The type of the triangle
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
“Equilateral” “Isosceles” “Multilateral”
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
S18 = S19 = S20 = X = 0
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
• Calculate the factorial of N.
• Input:
• N: The number whose factorial to be calculated
• 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
Output F Counter = Counter + 1
End
26
CII1SE15CI-Z17
Introduction to Programming C#
Edip ŞENYÜREK
Thank you!!!
27