Professional Documents
Culture Documents
Engineering
Introduction to Algorithms
Part 5
Exercise 6:
Draw a flowchart for a program that takes an integer from user and checks whether that
number is even or odd and displays the result.
Hint: divide the number on 2 and check if the remainder is equals to zero.
PSEUDOCODE
Start
Output «Please write an integer»
N=input
IF N mod 2 = 1, then
output «It is an odd number.»
Else
output «It is an even number.»
ENDIF
Exercise 4:
Draw a flowchart for a program that calculates the roots of a quadratic equation
ax 2 + bx + c = 0.
2
Hint: d = sqrt (b − 4ac ), and the roots are: X1= (–b + d)/2a and X2 = (–b – d)/2a.
Exercise 5:
Write a pseudocode for a program that calculates and prints the roots (if exist) of a
quadratic equation
Pseudocode – Exercise 7
Constats
Variables and variable names
CONSTANTS
– FOR Loop
– WHILE Loop
– DO … WHILE Loop
Basic Pseudocode Statements
The "WHILE" Repetition Structure
The "WHILE" repetition statements repeats a statement or a group of
statements for a certain number of times.
A WHILEloop is a loop that repeats while some
condition is satisfied.
The WHILE loop tests its condition at the beginning of every loop.
If the condition is false from the start, the sequence of activities contained in
the loop never runs at all.
It may have the following form.
WHILE (condition)
Statement(s)
ENDWHILE
While döngüsü: Bir koşul sağlanmaya devam ettiği
sürece işlemleri tekrarlar. İngilizce bir kelime olan
while, Türkçede ‘... iken, ... olduğu sürece’ gibi
anlamlarına gelir.
Belirli bir iş birden çok kez tekrarlanacaksa,
programda bu iş bir kez yazılır ve döngü yapıları o
deyim(ler)i istenildiği kadar tekrarlar. Bir ya da bir
grup deyimin, belli bir koşul sağlandığı sürece
tekrarlanması için kullanılan bir denetim yapısıdır.
• Açıklaması: Burada programımız diyor ki a, 10’dan küçük olduğu sürece ekrana
“Ali” yazdır. Ancak garip olan burada a=1 olduğu için a her zaman 10’dan küçük
olacak. Bu da ekrana sürekli “Ali” yazılmasına neden olacak. Buna sonsuz döngü
diyoruz. ( infinite loop).
Boolean (mantıksal deyim) true (doğru) ise, deyim ya
da blok içindeki deyimler yürütülür. Sonra program
akışı başladığı while deyimine döner ve tekrar boolean
mantıksal deyimini denetler. Mantıksal deyim doğru ise
deyim ya da blok içindeki deyimler yeniden yürütülür. Bu
döngü, mantıksal deyim false (yanlış) değerini alana kadar
yinelenir. Dolayısıyla, yinelenen deyim(ler)in, mantıksal
deyimin değerini sonlu sayıda yineleme sonunda
değiştirmesi gerekir. Aksi halde, sonsuz döngü dediğimiz
olgu ortaya çıkar.
Bu durum olunca, döngü deyim(ler)i, kesintisiz devam
eder. Program hatası olan sonsuz döngü’den sakınmak
gerekir.
Basic Pseudocode Statements
The "WHILE" Repetition Structure
The "WHILE" repetition statements repeats a statement or a group of
statements for a certain number of times.
It may have the following form.
WHILE (condition)
Statement(s)
ENDWHILE
Example 1:
Initialize Counter=0
WHILE (Counter< 5)
Prepare a new quiz.
Let the student do it.
Counter=Counter+1
ENDWHILE
Basic Pseudocode Statements
The "WHILE" Repetition Structure
The "WHILE" repetition statements repeats a statement or a group of
statements for a certain number of times.
It may have the following form.
WHILE (condition)
Statement(s)
ENDWHILE
Example 2:
Example 1:
Declare Number
Initialize Counter=0
Initialize Counter=0,Total=0
WHILE (Counter< 5) WHILE (Counter <3)
Prepare a new quiz. Read Number
Let the student do it. Total=Total+Number
Counter=Counter+1 ENDWHILE
ENDWHILE Write Total
Basic Pseudocode Statements
The "WHILE" Repetition Structure
The "WHILE" repetition statements repeats a statement or a group of
statements for a certain number of times.
It may have the following form.
How many iterations?
WHILE (condition)
Will this program ever stop?
Statement(s)
ENDWHILE
Example 2:
Example 1:
Declare Number
Initialize Counter=0
Initialize Counter=0,Total=0
WHILE (Counter< 5) WHILE (Counter <3)
Prepare a new quiz. Read Number
Let the student do it. Total=Total+Number
Counter=Counter+1 ENDWHILE
ENDWHILE Write Total
Basic Pseudocode Statements
The "WHILE" Repetition Structure
The "WHILE" repetition statements repeats a statement or a group of
statements for a certain number of times.
It may have the following form.
WHILE (condition)
Statement(s)
ENDWHILE
Example 2:
Example 1:
Declare Number
Initialize Counter=0
Initialize Counter=0,Total=0
WHILE (Counter< 5) WHILE (Counter <3)
Prepare a new quiz. Read Number
Let the student do it. Total=Total+Number
Counter=Counter+1 What is missed here?
ENDWHILE ENDWHILE
Write Total
Basic Pseudocode Statements
The "WHILE" Repetition Structure
The "WHILE" repetition statements repeats a statement or a group of
statements for a certain number of times.
It may have the following form.
WHILE (condition)
Statement(s)
ENDWHILE
Example 2:
Example 1:
Declare Number
Initialize Counter=0
Initialize Counter=0,Total=0
WHILE (Counter< 5) WHILE (Counter <3)
Prepare a new quiz. Read Number
Let the student do it. Total=Total+Number
Counter=Counter+1 Counter=Counter+1
ENDWHILE ENDWHILE
Write Total
DO-WHILE LOOP
FOR (Counter =1; Counter <=5; Counter =Counter +1) WHILE (Counter <5)
Read Grade Statement(s)
ENDFOR
Average = Total/5
Write Average
Pseudocode – Problem 7
Problem 7:
Write a pseudocode for a program that reads the grades of any five students,
finds the average of these grades, and then prints it.
FOR (Counter =1; Counter <=5; Counter =Counter +1) WHILE (Counter <5)
Read Grade Read Grade
Write Average
Pseudocode – Problem 7
Problem 7:
Write a pseudocode for a program that reads the grades of any five students,
finds the average of these grades, and then prints it.
FOR (Counter =1; Counter <=5; Counter =Counter +1) WHILE (Counter <=5)
Read Grade Read Grade
False
Repetition Structures
Problem 7:
Draw a flowchart for a program that reads the grades of five students, finds the average of these
grades, and then prints it.
START
False
Counter<MaxR
Counter=Counter+1
Average=Total/5
True
Print Average
Read Grade Total=Total+Grade
End
Repetition Structures
A tracing table of the flowchart for the problem 7, assuming that the user has
entered the following numbers, 40,30,100,70,60.
Counter <5 Grade Total Average Counter
START
Declaration & ? ? 0 0 0
Initialization:
False
Counter<MaxR
Counter=Counter+1
Average=Total/5
True
Print Average
Read Grade Total=Total+Grade
End
Repetition Structures
A tracing table of the flowchart for the problem 7, assuming that the user has
entered the following numbers, 40,30,100,70,60.
Counter <5 Grade Total Average Counter
START
Declaration & ? ? 0 0 0
Initialization:
False
Counter<MaxR
Counter=Counter+1
Average=Total/5
True
Print Average
Read Grade Total=Total+Grade
End
Repetition Structures
A tracing table of the flowchart for the problem 7, assuming that the user has
entered the following numbers, 40,30,100,70,60.
Counter <5 Grade Total Average Counter
START
Declaration & ? ? 0 0 0
Initialization:
False
Counter<MaxR
Counter=Counter+1
Average=Total/5
True
Print Average
Read Grade Total=Total+Grade
End
Repetition Structures
A tracing table of the flowchart for the problem 7, assuming that the user has
entered the following numbers, 40,30,100,70,60.
Counter <5 Grade Total Average Counter
START
Declaration & ? ? 0 0 0
Initialization:
True
Print Average
Read Grade Total=Total+Grade
End
Repetition Structures
A tracing table of the flowchart for the problem 7, assuming that the user has
entered the following numbers, 40,30,100,70,60.
Counter <5 Grade Total Average Counter
START
Declaration & ? ? 0 0 0
Initialization:
True
Print Average
Read Grade Total=Total+Grade
End
Repetition Structures
A tracing table of the flowchart for the problem 7, assuming that the user has
entered the following numbers, 40,30,100,70,60.
Counter <5 Grade Total Average Counter
START
Declaration & ? ? 0 0 0
Initialization:
True
Itr 5 of True 60 300 0 5 Print Average
repetition Read Grade Total=Total+Grade
End
Repetition Structures
A tracing table of the flowchart for the problem 7, assuming that the user has
entered the following numbers, 40,30,100,70,60.
Counter <5 Grade Total Average Counter
START
Declaration & ? ? 0 0 0
Initialization:
True
Itr 5 of True 60 300 0 5 Print Average
repetition Read Grade Total=Total+Grade
False
Counter<MaxR
Counter=Counter+1
Average=Total/5
True
Print Average
Read Grade Total=Total+Grade
End
Repetition Structures
Problem 8:
Draw a flowchart for a program that calculates and prints the factorial (n!) of n. Where n is an integer
number entered by the user. n! = n*(n-1)*(n-2)*…….*1
Store the result
Every time we are multiplying the previous result. That
means we need to save the previous result and use it.
• What are processes that could be applied on the input to get the output.
False
Think Logically - Problem 1
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B).
Questions to answer before you start:
1. What is the maximum number of
repetition?
Think Logically - Problem 1
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B).
Questions to answer before you start:
1. What is the maximum number of
repetition?
B
2. What is the initial value of the counter?
Think Logically - Problem 1
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B).
Questions to answer before you start:
1. What is the maximum number of
repetition?
B
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
Think Logically - Problem 1
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B).
Questions to answer before you start:
1. What is the maximum number of
repetition?
B
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
Counter < B
4. Shoud I increment or drcrement the
Counter after each iteration?
Think Logically - Problem 1
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B).
Questions to answer before you start:
1. What is the maximum number of
repetition?
B
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
Counter < B
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Think Logically - Problem 1
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B).
Questions to answer before you start:
1. What is the maximum number of
repetition?
B
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
Counter < B
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Multiply A with result, and increment
the Counter
Think Logically - Problem 1- Pseudocode
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B).
Questions to answer before you start: Pseudocode:
1. What is the maximum number of Declare A,B, Counter, Result=1
repetition?
Read A,B
B
FOR(Counter = 0; Counter < B ; Counter = Counter+1)
2. What is the initial value of the counter?
Result=Result*A;
Counter = 0
ENDFOR
3. What is the condition of repetition?
Write Result
Counter < B
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Multiply A with result, and increment
the Counter
Think Logically - Problem 1- Flowchart
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B). START
Pseudocode:
Declare A,B, Counter, Result=1
Read A,B
FOR (Counter = 0; Counter < B ; Counter = Counter+1)
Result=Result*A;
ENDFOR
Write Result
Think Logically - Problem 1- Flowchart
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B). START
Declare A,B
Pseudocode: Counter=0,
Declare A,B, Counter, Result=1 Result=1
Read A,B
FOR (Counter = 0; Counter < B ; Counter = Counter+1)
Result=Result*A;
ENDFOR
Write Result
Think Logically - Problem 1- Flowchart
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B). START
Declare A,B
Pseudocode: Counter=0,
Declare A,B, Counter, Result=1 Result=1
Read A,B
FOR (Counter = 0; Counter < B ; Counter = Counter+1) Read A,B
Result=Result*A;
ENDFOR
Write Result
Think Logically - Problem 1- Flowchart
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B). START
Declare A,B
Pseudocode: Counter=0,
Declare A,B, Counter, Result=1 Result=1
Read A,B
FOR (Counter = 0; Counter < B ; Counter = Counter+1) Read A,B
Result=Result*A;
ENDFOR
Write Result False Logical
Condition
True
Think Logically - Problem 1- Flowchart
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B). START
Declare A,B
Pseudocode: Counter=0,
Declare A,B, Counter, Result=1 Result=1
Read A,B
FOR (Counter = 0; Counter < B ; Counter = Counter+1) Read A,B
Result=Result*A;
ENDFOR
Write Result False
Counter<B
True
Think Logically - Problem 1- Flowchart
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B). START
Declare A,B
Pseudocode: Counter=0,
Declare A,B, Counter, Result=1 Result=1
Read A,B
FOR (Counter = 0; Counter < B ; Counter = Counter+1) Read A,B
Result=Result*A;
ENDFOR
Write Result False
Counter<B Actions that should
be repeated
True
Think Logically - Problem 1- Flowchart
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B). START
Declare A,B
Pseudocode: Counter=0,
Declare A,B, Counter, Result=1 Result=1
Read A,B
FOR (Counter = 0; Counter < B ; Counter = Counter+1) Read A,B
Result=Result*A;
ENDFOR
Write Result False
Counter<B
Counter=Counter+1
True
result= result*A
Think Logically - Problem 1- Flowchart
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers A&B from the user, then calculates and prints 𝐴𝐴𝐵𝐵 ( A raised to the power
of B). START
Declare A,B
Pseudocode: Counter=0,
Declare A,B, Counter, Result=1 Result=1
Read A,B
FOR (Counter = 0; Counter < B ; Counter = Counter+1) Read A,B
Result=Result*A;
ENDFOR
Write Result False
Counter<B
Counter=Counter+1
Print result
End True
result= result*A
Think Logically – Problem 1 - Tracing Table
Construct a tracing table for the flowchart mentioned above, assuming that the user
has entered A=3, B=4.
START
Declare A,B
Counter=0,
Result=1
Read A,B
False
Counter<B
Counter=Counter+1
Print result
End True
result= result*A
Think Logically – Problem 1 - Tracing Table
Construct a tracing table for the flowchart mentioned above, assuming that the user
has entered A=3, B=4.
Counter <B A B result counter
START
Declare A,B
Counter=0,
Result=1
Read A,B
False
Counter<B
Counter=Counter+1
Print result
End True
result= result*A
Think Logically – Problem 1 - Tracing Table
Construct a tracing table for the flowchart mentioned above, assuming that the user
has entered A=3, B=4.
Counter <B A B result counter
START
Declaration & ? ? ? 1 0
Initialization:
Declare A,B
Counter=0,
Result=1
Read A,B
False
Counter<B
Counter=Counter+1
Print result
End True
result= result*A
Think Logically – Problem 1 - Tracing Table
Construct a tracing table for the flowchart mentioned above, assuming that the user
has entered A=3, B=4.
Counter <B A B result counter
START
Declaration & ? ? ? 1 0
Initialization:
Declare A,B
Counter=0,
Input ? 3 4 1 0 Result=1
Read A,B
False
Counter<B
Counter=Counter+1
Print result
End True
result= result*A
Think Logically – Problem 1 - Tracing Table
Construct a tracing table for the flowchart mentioned above, assuming that the user
has entered A=3, B=4.
Counter <B A B result counter
START
Declaration & ? ? ? 1 0
Initialization:
Declare A,B
Counter=0,
Input ? 3 4 1 0 Result=1
Itr 1 of True 3 4 3 1
Read A,B
repetition
False
Counter<B
Counter=Counter+1
Print result
End True
result= result*A
Think Logically – Problem 1 - Tracing Table
Construct a tracing table for the flowchart mentioned above, assuming that the user
has entered A=3, B=4.
Counter <B A B result counter
START
Declaration & ? ? ? 1 0
Initialization:
Declare A,B
Counter=0,
Input ? 3 4 1 0 Result=1
Itr 1 of True 3 4 3 1
Read A,B
repetition
Itr 2 of True 3 4 9 2
repetition False
Counter<B
Counter=Counter+1
Print result
End True
result= result*A
Think Logically – Problem 1 - Tracing Table
Construct a tracing table for the flowchart mentioned above, assuming that the user
has entered A=3, B=4.
Counter <B A B result counter
START
Declaration & ? ? ? 1 0
Initialization:
Declare A,B
Counter=0,
Input ? 3 4 1 0 Result=1
Itr 1 of True 3 4 3 1
Read A,B
repetition
Itr 2 of True 3 4 9 2
repetition False
Counter<B
Counter=Counter+1
Itr 3 of True 3 4 27 3 Print result
repetition
End True
result= result*A
Think Logically – Problem 1 - Tracing Table
Construct a tracing table for the flowchart mentioned above, assuming that the user
has entered A=3, B=4.
Counter <B A B result counter
START
Declaration & ? ? ? 1 0
Initialization:
Declare A,B
Counter=0,
Input ? 3 4 1 0 Result=1
Itr 1 of True 3 4 3 1
Read A,B
repetition
Itr 2 of True 3 4 9 2
repetition False
Counter<B
Counter=Counter+1
Itr 3 of True 3 4 27 3 Print result
repetition
End True
START
Declaration & ? ? ? 1 0
Initialization:
Declare A,B
Counter=0,
Input ? 3 4 1 0 Result=1
Itr 1 of True 3 4 3 1
Read A,B
repetition
Itr 2 of True 3 4 9 2
repetition False
Counter<B
Counter=Counter+1
Itr 3 of True 3 4 27 3 Print result
repetition
End True
End of false 3 4 81 4
repetition
& output
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer
numbers N1,N2 from the user, then calculates and prints the sum of all integer
numbers that exist between them.
Construct a tracing table for the flowchart mentioned above assuming that the
user has entered N1=10 and N2=14.
Think Logically – Problem 2
If you want to solve a problem, you should ask yourself these questions:
• What are processes that could be applied on the input to get the output.
False
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start:
1. What is the maximum number of
repetition?
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start:
1. What is the maximum number of
repetition?
N2-N1-1
2. What is the initial value of the counter?
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start:
1. What is the maximum number of
repetition?
N2-N1-1
2. What is the initial value of the counter?
Counter = N1+1
3. What is the condition of repetition?
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start:
1. What is the maximum number of
repetition?
N2-N1-1
2. What is the initial value of the counter?
Counter = N1+1
3. What is the condition of repetition?
Counter < N2
4. Shoud I increment or drcrement the
Counter after each iteration?
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start:
1. What is the maximum number of
repetition?
N2-N1-1
2. What is the initial value of the counter?
Counter = N1+1
3. What is the condition of repetition?
Counter < N2
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start:
1. What is the maximum number of
repetition?
N2-N1-1
2. What is the initial value of the counter?
Counter = N1+1
3. What is the condition of repetition?
Counter < N2
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Add counter to Result, and increment
the Counter
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start: Questions to answer before you start:
1. What is the maximum number of 1. What is the maximum number of
repetition? repetition?
N2-N1-1
2. What is the initial value of the counter?
Counter = N1+1
3. What is the condition of repetition?
Counter < N2
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Add counter to Result, and increment
the Counter
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start: Questions to answer before you start:
1. What is the maximum number of 1. What is the maximum number of
repetition? repetition?
N2-N1-1 N1-N2-1
2. What is the initial value of the counter? 2. What is the initial value of the counter?
Counter = N1+1
3. What is the condition of repetition?
Counter < N2
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Add counter to Result, and increment
the Counter
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start: Questions to answer before you start:
1. What is the maximum number of 1. What is the maximum number of
repetition? repetition?
N2-N1-1 N1-N2-1
2. What is the initial value of the counter? 2. What is the initial value of the counter?
Counter = N1+1 Counter = N2+1
3. What is the condition of repetition? 3. What is the condition of repetition?
Counter < N2
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Add counter to Result, and increment
the Counter
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start: Questions to answer before you start:
1. What is the maximum number of 1. What is the maximum number of
repetition? repetition?
N2-N1-1 N1-N2-1
2. What is the initial value of the counter? 2. What is the initial value of the counter?
Counter = N1+1 Counter = N2+1
3. What is the condition of repetition? 3. What is the condition of repetition?
Counter < N2 Counter < N1
4. Shoud I increment or drcrement the 4. Shoud I increment or drcrement the
Counter after each iteration? Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Add counter to Result, and increment
the Counter
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start: Questions to answer before you start:
1. What is the maximum number of 1. What is the maximum number of
repetition? repetition?
N2-N1-1 N1-N2-1
2. What is the initial value of the counter? 2. What is the initial value of the counter?
Counter = N1+1 Counter = N2+1
3. What is the condition of repetition? 3. What is the condition of repetition?
Counter < N2 Counter < N1
4. Shoud I increment or drcrement the 4. Shoud I increment or drcrement the
Counter after each iteration? Counter after each iteration?
Counter = Counter+1 Counter = Counter+1
5. What are the actions that should be 5. What are the actions that should be
repeated? repeated?
Add counter to Result, and increment
the Counter
Think Logically – Problem 2
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Case 1: N1 <N2 Case 2: N1>=N2
Questions to answer before you start: Questions to answer before you start:
1. What is the maximum number of 1. What is the maximum number of
repetition? repetition?
N2-N1-1 N1-N2-1
2. What is the initial value of the counter? 2. What is the initial value of the counter?
Counter = N1+1 Counter = N2+1
3. What is the condition of repetition? 3. What is the condition of repetition?
Counter < N2 Counter < N1
4. Shoud I increment or drcrement the 4. Shoud I increment or drcrement the
Counter after each iteration? Counter after each iteration?
Counter = Counter+1 Counter = Counter+1
5. What are the actions that should be 5. What are the actions that should be
repeated? repeated?
Add counter to Result, and increment Add counter to Result, and increment
the Counter the Counter
Think Logically - Problem 2 - Pseudocode
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Pseudocode:
Declare Counter, Result=0
Read N1,N2
Think Logically - Problem 2 - Pseudocode
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Pseudocode:
Declare Counter, Result=0
Read N1,N2
IF (N1<N2)
THEN
ELSE
ENDIF
Think Logically - Problem 2 - Pseudocode
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Pseudocode:
Declare Counter, Result=0
Read N1,N2
IF (N1<N2)
THEN
FOR(Counter=N1+1; Counter<N2; Counter=Counter+1)
Result=Result+Counter
ENDFOR
ELSE
ENDIF
Think Logically - Problem 2 - Pseudocode
Write a pseudocode and draw a flowchart for a program that reads two integer numbers
N1,N2 from the user, then calculates and prints the sum of all integer numbers that exist
between them.
Pseudocode:
Declare Counter, Result=0
Read N1,N2
IF (N1<N2)
THEN
FOR(Counter=N1+1; Counter<N2; Counter=Counter+1)
Result=Result+Counter
ENDFOR
ELSE
FOR(Counter=N2+1; Counter<N1; Counter=Counter+1)
Result=Result+Counter
ENDFOR
ENDIF
Think Logically - Problem 2 - Flowchart
Declare Counter, Result=0 START
Result=Result+Counter
ENDFOR
ELSE
Result=Result+Counter
ENDFOR
ENDIF
Write Result
Think Logically - Problem 2 - Flowchart
Declare Counter, Result=0 START
ENDFOR
ELSE
Result=Result+Counter
ENDFOR
ENDIF
Write Result
Think Logically - Problem 2 - Flowchart
Declare Counter, Result=0 START
ELSE Counter<N2
FOR(Counter=N2+1; Counter<N1; Counter=Counter+1) True
Result=Result+Counter Result=Result+Counter
ENDFOR
Counter=Counter+1
ENDIF
Write Result
Think Logically - Problem 2 - Flowchart
Declare Counter, Result=0 START
ENDFOR
Counter=Counter+1 Counter=Counter+1
ENDIF
Write Result
Think Logically - Problem 2 - Flowchart
Declare Counter, Result=0 START
ENDFOR
Counter=Counter+1 Counter=Counter+1
ENDIF
Write Result Print result
END
Think Logically - Problem 2 - Tracing Table
START
Construct a tracing table for the flowchart mentioned above
assuming that the user has entered N1=10 and N2=14. Declare N1,N2
Counter,Result=0
Read N1,N2
N1 N2 N1<N2 Counter <N2 Result Counter
True False
N1<N2
Counter=N1+1 Counter=N2+1
False False
Counter<N2 Counter<N1
True True
Result=Result+Counter Result=Result+Counter
Counter=Counter+1 Counter=Counter+1
Print result
END
Think Logically - Problem 2 - Tracing Table
START
Construct a tracing table for the flowchart mentioned above
assuming that the user has entered N1=10 and N2=14. Declare N1,N2
Counter,Result=0
Read N1,N2
N1 N2 N1<N2 Counter <N2 Result Counter
True False
N1<N2
Declaration ? ? ? ? 0 ?
Counter=N1+1 Counter=N2+1
False False
Counter<N2 Counter<N1
True True
Result=Result+Counter Result=Result+Counter
Counter=Counter+1 Counter=Counter+1
Print result
END
Think Logically - Problem 2 - Tracing Table
START
Construct a tracing table for the flowchart mentioned above
assuming that the user has entered N1=10 and N2=14. Declare N1,N2
Counter,Result=0
Read N1,N2
N1 N2 N1<N2 Counter <N2 Result Counter
True False
N1<N2
Declaration ? ? ? ? 0 ?
Counter=N1+1 Counter=N2+1
Input 10 14 ? ? 0 ?
False False
Counter<N2 Counter<N1
True True
Result=Result+Counter Result=Result+Counter
Counter=Counter+1 Counter=Counter+1
Print result
END
Think Logically - Problem 2 - Tracing Table
START
Construct a tracing table for the flowchart mentioned above
assuming that the user has entered N1=10 and N2=14. Declare N1,N2
Counter,Result=0
Read N1,N2
N1 N2 N1<N2 Counter <N2 Result Counter
True False
N1<N2
Declaration ? ? ? ? 0 ?
Counter=N1+1 Counter=N2+1
Input 10 14 ? ? 0 ?
False False
Counter<N2 Counter<N1
If 10 14 True ? 0 11
Structure True True
Result=Result+Counter Result=Result+Counter
Counter=Counter+1 Counter=Counter+1
Print result
END
Homework 7- Problem 2 Tracing Table
START
Construct a tracing table for the flowchart mentioned above
assuming that the user has entered N1=10 and N2=14. Declare N1,N2
Counter,Result=0
Read N1,N2
N1 N2 N1<N2 Counter <N2 Result Counter
True False
N1<N2
Declaration ? ? ? ? 0 ?
Counter=N1+1 Counter=N2+1
Input 10 14 ? ? 0 ?
False False
Counter<N2 Counter<N1
If 10 14 True ? 0 11
Structure True True
Result=Result+Counter Result=Result+Counter
Itr 1 of 10 14 True True 11 12
repetition
Counter=Counter+1 Counter=Counter+1
Print result
END
Think Logically - Problem 2 - Tracing Table
START
Construct a tracing table for the flowchart mentioned above
assuming that the user has entered N1=10 and N2=14. Declare N1,N2
Counter,Result=0
Read N1,N2
N1 N2 N1<N2 Counter <N2 Result Counter
True False
N1<N2
Declaration ? ? ? ? 0 ?
Counter=N1+1 Counter=N2+1
Input 10 14 ? ? 0 ?
False False
Counter<N2 Counter<N1
If 10 14 True ? 0 11
Structure True True
Result=Result+Counter Result=Result+Counter
Itr 1 of 10 14 True True 11 12
repetition
Itr 2 of 10 14 True True 23 13 Counter=Counter+1 Counter=Counter+1
repetition
Print result
END
Think Logically - Problem 2 - Tracing Table
START
Construct a tracing table for the flowchart mentioned above
assuming that the user has entered N1=10 and N2=14. Declare N1,N2
Counter,Result=0
Read N1,N2
N1 N2 N1<N2 Counter <N2 Result Counter
True False
N1<N2
Declaration ? ? ? ? 0 ?
Counter=N1+1 Counter=N2+1
Input 10 14 ? ? 0 ?
False False
Counter<N2 Counter<N1
If 10 14 True ? 0 11
Structure True True
Result=Result+Counter Result=Result+Counter
Itr 1 of 10 14 True True 11 12
repetition
Itr 2 of 10 14 True True 23 13 Counter=Counter+1 Counter=Counter+1
repetition
Itr 3 of 10 14 True True 36 14 Print result
repetition
END
Think Logically - Problem 2 - Tracing Table
START
Construct a tracing table for the flowchart mentioned above
assuming that the user has entered N1=10 and N2=14. Declare N1,N2
Counter,Result=0
Read N1,N2
N1 N2 N1<N2 Counter <N2 Result Counter
True False
N1<N2
Declaration ? ? ? ? 0 ?
Counter=N1+1 Counter=N2+1
Input 10 14 ? ? 0 ?
False False
Counter<N2 Counter<N1
If 10 14 True ? 0 11
Structure True True
Result=Result+Counter Result=Result+Counter
Itr 1 of 10 14 True True 11 12
repetition
Itr 2 of 10 14 True True 23 13 Counter=Counter+1 Counter=Counter+1
repetition
Itr 3 of 10 14 True True 36 14 Print result
repetition
End of 10 14 True False 36 14 END
repetition
& output
Think Logically – ASSIGNMENT 2
Write a pseudocode and draw a flowchart for a program that reads an integer
numbers x from the user, then calculates and prints y= 𝑒𝑒 𝑥𝑥 +2𝑥𝑥 3 +1.
References
http://www.gcflearnfree.org/
www.cse.scu.edu/~dlewis/book1/2.ppt
https://www.site.uottawa.ca/~ivan/02.hdwr_soft.ppt
Introduction to Computer Engineering (İngilizce)
Bilgisayar Mühendisliğine Giriş - ISBN: 978-605-4220-66-3,
İngilizce, Editor: Rifat ÇÖLKESEN (Ph.D), Mart 2013, 272
sayfa,(16,5x24 cm2)
Shelly GB and VermaatME, Discovering computers 2011:
living in a digital world, cengagelearning, BOUSTON, USA.
Glenn Brookshear, Dennis Brylow, “Computer Science: An
Overview” 12th Edition, Pearson, 2015.
Based on Mohamed KURDI’s Lecture Notes.
171
http://humorhub.net/which-step-have-you-reached-today/