You are on page 1of 172

CSE 113 Introduction to Computer Science and

Engineering
Introduction to Algorithms
Part 5

Dr. Denizhan DEMİRKOL (Ph.D)


Exercise 3:
Write a pseudocode for a program that takes an integer from user and checks whether that
number is even or odd and displays the result.

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

 Write pseudo code that performs the following: Ask a user


to enter a number.
 If the number is between 0 and 10, write the word blue.
 If the number is between 10 and 20, write the word red.
 If the number is between 20 and 30, write the word green.
 If it is any other number, write that it is not a correct color
option.
DATA TYPES

 Although some contemporary languages allow


programmers to invent his own data types, and define
their related operations, there are a number of traditional
data types found in most languages:
INTEGER

 Integers are numeric data items, which are either positive


or negative including zero, i.e. 1, 488, -22, 0, 456. Some
programming languages put restrictions on the magnitude
of integers which may be used in program instructions.
 These restrictions are usually dependent on the size of the
memory location of the computer in which the language
may run
REAL NUMBERS
 There are two types of real numbers, Fixed-Point and
Floating Point.
 Fixed point data items are numbers which have embedded
decimal point i.e. 1.5, 458.4589, -0.569.
 Floating point data items are numbers, which are, held as
binary fractions by a computer. The numbers are
expressed in a form where you have a mantissa and an
exponent, for example
FLOATING POINT

En temel ifade ile, 10 ile çarpılıp bölündüğünde, basitçe işlem yapılabilen


(nokta'nın kaydırılması suretiyle) sayılar olarak tanımlanmaktadır.
CHARACTER
 Character data, sometimes referred to as “string” data,
may consist of any digits, letters of the alphabet or
symbols which, the internal coding system of the
computer is capable of representing. Many programming
languages require character data to be enclosed by
quotation marks when used in program instructions, for
example PRINT “HAPPY NEW YEAR”.
BOOLEAN
 Boolean data items are used as status indicators and may
contain only one of two possible values: True or False.
DATA ITEM
 There are two basic methods of using data items in a
program:

 Constats
 Variables and variable names
CONSTANTS

 Data items are sometimes required to keep their values


throughout the program, hence the term constant. A
constant is a specific value or character string used
explicitly in an operation. Consider the constant values
47.5, 1, and 13 in the example below.

 Multiply … by 47.5 Add 1 to … If … = 13


Variables and Variables Names
 A variable is a symbolic name assigned to a data item by
the programmer. At any particular time, a variable will
stand for one particular data, called the value of a variable,
which may change from time to time during a computing
process. The value of a variable may change many times
during the execution of a program. A variable is usually
given a name by the programmer.
LOOPS

 A loop is a sequence of instructions that is


continually repeated until a certain condition is
reached.

 Loops allow for the same statement to be


executed a number of times in succession.
LOOPS
• There are three types which are common to most
programming languages:
– Condition Tested Loops
– Counted Loops
– Endless Loops
CONDITION TESTED LOOPS

• A condition tested loop is one which repeats a set of


instructions until a certain condition is reached.
• The test can be performed at the start of the loop
(before any of the instructions are executed), during
the loop, or at the end of the loop.
• Usually, the condition will be testing the result of
executing the statements that are inside the loop.
COUNTED LOOPS

• A counted loop is one which allows the programmer to


instruct the computer to perform a set of
instructions x times, where x is usually an integer
value, but some programming languages offer other
data types.
• One could argue that the counted loop is just a
condition tested loop which updates a counter and
exits once a given value is reached.
• The only time to use a count loop is when the
program can determine ahead of time how many
times the loop will repeat.
• There are generally two ways that the number of
repetitions of a loop will be know ahead of time:
 The loop always repeats the same number of times.
 The program calculates the number of repetitions
based upon user input.
ENDLESS LOOPS

• An endless loop goes round and round until one of


three things happens:
– The computer is turned off (or the application
stopped, forcefully)
– The computer encounters an EXIT (or similar)
statement
– An error forces the application to 'crash'
• Some endless loops serve a purpose, in message
loops, for example, where it is necessary to
continually monitor for incoming messages from the
operating system.
EXAMPLES OF LOOP STATEMENT

• These are examples loop statement in programming


language

– 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

• Like a while loop, a do-while loop is a loop that


repeats while some condition is satisfied.
• Unlike a while loop, a do-while loop tests its condition at
the end of the loop.
• This means that its sequence of activities always runs at
least once.
Basic Pseudocode Statements
The "FOR" Repetition Structure
 The "FOR" repetition statements repeats a statement or a group of statements
for a certain number of times.
 A FOR loop is a loop that repeats a specified number of times.
 The loop uses a counter to tell it how many times to run the same sequence of
activities.
 It may have the following form.
FOR (a certain number of iterations)
Statement(s)
ENDFOR
• The counter has the following three numeric values:
– Initial counter value
– Increment (the amount to add to the counter each
time the loop runs)
– Final counter value
• The loop ends when the counter reaches the final
counter value, or, if there is an associated test
condition, when the test condition is true.
• Komut ya da komutların istenilen sayıda
tekrarlanmasını sağlar. Sayaç mantığıyla çalışır.
Döngüyü istediğimiz sayıdan başlatır istediğimiz sayıda
sonlandırabiliriz. Döngünün değişme değerini her
işlemde istediğimiz miktarda artırabilir ya da
azaltabiliriz.
• Örneğin döngümüz için başlangıç değerini 1, bitiş
değerini 100 olarak belirleyebilir. Artış miktarını ise 2
olarak ayarlayabiliriz. Bu durumda döngü içindeki
komutlar 50 defa tekrar tekrar çalıştırılacaktır.
Basic Pseudocode Statements
The "FOR" Repetition Structure
 The "FOR" repetition statements repeats a statement or a group of statements
for a certain number of times.
 It may have the following form.
FOR (a certain number of iterations)
Statement(s)
ENDFOR
Example 1:
FOR (Counter =0; Counter <10; Counter =Counter +1)
Prepare a new quiz
Let the students do it
ENDFOR
Basic Pseudocode Statements
The "FOR" Repetition Structure
 The "FOR" repetition statements repeats a statement or a group of statements
for a certain number of times.
 It may have the following form.
FOR (a certain number of iterations)
Statement(s)
ENDFOR
Example 1:
FOR (Counter =0; Counter <10; Counter =Counter +1)
Prepare a new quiz
Let the students do it
ENDFOR
Or
FOR (Counter =1:10)
Prepare a new quiz
Let the students do it
ENDFOR
Basic Pseudocode Statements
The "FOR" Repetition Structure
 The "FOR" repetition statements repeats a statement or a group of statements
for a certain number of times.
 It may have the following form.
FOR (a certain number of iterations)
Statement(s)
ENDFOR
Example 1: Example 2:
FOR (Counter =0; Counter <10; Counter =Counter +1) FOR (counter=1:16)
Prepare a new quiz
Come to the class
Let the students do it
ENDFOR ENDFOR
Or
FOR (Counter =1:10)
Prepare a new quiz
Let the students do it
ENDFOR
Basic Pseudocode Statements
The "FOR" Repetition Structure
 The "FOR" repetition statements repeats a statement or a group of statements
for a certain number of times.
 It may have the following form.
FOR (a certain number of iterations)
Statement(s)
ENDFOR
Example 1: Example 2:
FOR (Counter =0; Counter <10; Counter =Counter +1) FOR (counter=1:16)
Prepare a new quiz
Come to the class
Let the students do it
ENDFOR ENDFOR
Or Example 3:
FOR (Counter =1:10) FOR (counter=1:1)
Prepare a new quiz
Violate the class rules
Let the students do it
ENDFOR ENDFOR
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.

Pseudocode ( using FOR):


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.

Pseudocode ( using FOR):


Declare Counter, Grade,Average
Initialize Total=0
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.

Pseudocode ( using FOR):


Declare Counter, Grade,Average
Initialize Total=0
FOR (specific number of iterations)
Statement(s)
ENDFOR
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.

Pseudocode ( using FOR):


Declare Counter, Grade,Average
Initialize Total=0
FOR (5 iterations)
Statement(s)
ENDFOR
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.

Pseudocode ( using FOR):


Declare Counter, Grade,Average
Initialize Total=0
FOR (Counter =1; Counter <=5; Counter =Counter +1)
Statement(s)
ENDFOR
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.

Pseudocode ( using FOR):


Declare Counter, Grade,Average
Initialize Total=0
FOR (Counter =1; Counter <=5; Counter =Counter +1)
Read Grade
Total=Total +Grade
ENDFOR
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.

Pseudocode ( using FOR):


Declare Counter, Grade,Average
Initialize Total=0
FOR (Counter =1; Counter <=5; Counter =Counter +1)
Read Grade
Total=Total +Grade
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.

Pseudocode ( using FOR): Pseudocode ( using WHILE):

Declare Counter, Grade,Average


Initialize Total=0
FOR (Counter =1; Counter <=5; Counter =Counter +1)
Read Grade
Total=Total +Grade
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.

Pseudocode ( using FOR): Pseudocode ( using WHILE):

Declare Counter, Grade,Average Declare Grade,Average

Initialize Total=0 Initialize Total=0, Counter=0

FOR (Counter =1; Counter <=5; Counter =Counter +1)


Read Grade
Total=Total +Grade
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.

Pseudocode ( using FOR): Pseudocode ( using WHILE):

Declare Counter, Grade,Average Declare Grade,Average

Initialize Total=0 Initialize Total=0, Counter=0

FOR (Counter =1; Counter <=5; Counter =Counter +1) WHILE (Counter <5)
Read Grade Statement(s)

Total=Total +Grade ENDFOR

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.

Pseudocode ( using FOR): Pseudocode ( using WHILE):

Declare Counter, Grade,Average Declare Grade,Average

Initialize Total=0 Initialize Total=0, Counter=0

FOR (Counter =1; Counter <=5; Counter =Counter +1) WHILE (Counter <5)
Read Grade Read Grade

Total=Total +Grade Total=Total +Grade

ENDFOR Counter= Counter +1

Average = Total/5 ENDFOR

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.

Pseudocode ( using FOR): Pseudocode ( using WHILE):

Declare Counter, Grade,Average Declare Grade,Average

Initialize Total=0 Initialize Total=0, Counter=1

FOR (Counter =1; Counter <=5; Counter =Counter +1) WHILE (Counter <=5)
Read Grade Read Grade

Total=Total +Grade Total=Total +Grade

ENDFOR Counter= Counter +1

Average = Total/5 ENDFOR

Write Average Average = Total/5


Write Average
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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
1!=1
2!=2*1
3!=3*2*1
4!=4*3*2*1
5!=5*4*3*2*1
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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 What are the input and output?
1!=1 How many iterations?
2!=2*1 What should we do in each iterations?
What are the variables that I may need?
3!=3*2*1
4!=4*3*2*1
5!=5*4*3*2*1
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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 What are the input and output?
How many iterations?
Pseudocode (using FOR): What should we do in each iterations?
What are the variables that I may need?
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR):


(Temp = temporary variable)
Declare N, Factorial=1, Temp, Counter
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR):


Declare N, Factorial=1, Temp, Counter
Read N
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR):


Declare N, Factorial=1, Temp, Counter
Read N
Temp=N
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR):


Declare N, Factorial=1, Temp, Counter
Read N
Temp=N
FOR (specific number of iterations)
Statement(s)
ENDFOR
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR):


Declare N, Factorial=1, Temp, Counter
Read N
Temp=N
FOR (N iterations)
Statement(s)
ENDFOR
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR):


Declare N, Factorial=1, Temp, Counter
Read N
Temp=N
FOR (Counter =1; Counter <=n; Counter =Counter +1)
Statement(s)
ENDFOR
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR):


Declare N, Factorial=1, Temp, Counter
Read N
Temp=N
FOR (Counter =1; Counter <=n; Counter =Counter +1)
Factorial=Factorial*Temp
Temp=Temp-1
ENDFOR
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR):


Declare N, Factorial=1, Temp, Counter
Read N
Temp=N
FOR (Counter =1; Counter <=n; Counter =Counter +1)
Factorial=Factorial*Temp
Temp=Temp-1
ENDFOR
Write Factorial
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR): Pseudocode ( using WHILE):

Declare N, Factorial=1, Temp, Counter


Read N
Temp=N
FOR (Counter =1; Counter <=n; Counter =Counter +1)
Factorial=Factorial*Temp
Temp=Temp-1
ENDFOR
Write Factorial
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR): Pseudocode ( using WHILE):

Declare N, Factorial=1, Temp, Counter Declare N, Factorial=1, Counter=1


Read N Read N
Temp=N Temp=N
FOR (Counter =1; Counter <=n; Counter =Counter +1)
Factorial=Factorial*Temp
Temp=Temp-1
ENDFOR
Write Factorial
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR): Pseudocode ( using WHILE):

Declare N, Factorial=1, Temp, Counter Declare N, Factorial=1, Counter=1


Read N Read N
Temp=N Temp=N
FOR (Counter =1; Counter <=n; Counter =Counter +1) WHILE (Counter <=N)
Factorial=Factorial*Temp Statement(s)
Temp=Temp-1 ENDFOR
ENDFOR
Write Factorial
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR): Pseudocode ( using WHILE):

Declare N, Factorial=1, Temp, Counter Declare N, Factorial=1, Counter=1


Read N Read N
Temp=N Temp=N
FOR (Counter =1; Counter <=n; Counter =Counter +1) WHILE (Counter <=N)
Factorial=Factorial*Temp Factorial=Factorial*Temp
Temp=Temp-1 Temp=Temp-1
ENDFOR Counter= Counter +1
Write Factorial ENDFOR
Pseudocode – Problem 8
Problem 8:
Write a pseudocode 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

Pseudocode (using FOR): Pseudocode ( using WHILE):

Declare N, Factorial=1, Temp, Counter Declare N, Factorial=1, Counter=1


Read N Read N
Temp=N Temp=N
FOR (Counter =1; Counter <=N; Counter =Counter +1) WHILE (Counter <=N)
Factorial=Factorial*Temp Factorial=Factorial*Temp
Temp=Temp-1 Temp=Temp-1
ENDFOR Counter= Counter +1
Write Factorial ENDFOR
Write Factorial
 While döngüsünün for döngüsünden en önemli farkı, kaç
kez çalışacağının kesin olmadığı durumlarda kullanılır.
Ancak for döngüsü ile yapılabilen her şey, bir değişken
yardımı ile while döngüsü kullanılarak da yapılabilir.
CSE 113 Introduction to Computer Science and
Engineering
Introduction to Algorithms
Part 6

Dr. Denizhan DEMİRKOL (Ph.D.)


Repetition Structures
A repetition structure represents a part of an algorithm that repeats. This type of structure
is also known as a loop and has 3 main forms: FOR, WHILE, and DO WHILE
structures.
Repetition Structures
When you want to construct a FOR structure, you should find the answers for these 1. Read
questions:
2. Write
1. What is the maximum number of repetition? Let us keep it in a vaiable named
MaxR. 3. Calculate
2. What is the initial value of the counter? Let us keep it in a vaiable named Counter. 4. Assign
3. What is the logical condition (the condition of repetition)? 5. Select
4. Shoud I increment or decrement the Counter after each iteration? (we have MaxR 6. Repeat
iterations)
5. What are the actions that should be repeated?

Counter = initial value

True Counter = Counter - 1


Logical test
(repetition condition) Actions to be or
repeated Counter = Counter +1

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.

Questions to answer before you start:


1. What is the maximum number of
repetition?
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.

Questions to answer before you start:


1. What is the maximum number of
repetition?
MaxR= 5
2. What is the initial value of the counter?
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.

Questions to answer before you start:


1. What is the maximum number of
repetition?
MaxR= 5
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
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.

Questions to answer before you start:


1. What is the maximum number of
repetition?
MaxR= 5
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
Counter < MaxR
4. Shoud I increment or drcrement the
Counter after each iteration?
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.

Questions to answer before you start:


1. What is the maximum number of
repetition?
MaxR= 5
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
Counter < MaxR
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
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.

Questions to answer before you start:


1. What is the maximum number of
repetition?
MaxR= 5
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
Counter < MaxR
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Read a grade, add a grade, and
increment the Counter
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
Questions to answer before you start:
1. What is the maximum number of
repetition?
MaxR= 5
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
Counter < MaxR
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Read a grade, add a grade, and
increment the Counter
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
Questions to answer before you start:
1. What is the maximum number of
repetition? Declare Average,Grade,
MaxR= 5 Counter=0, MaxR=5,
Total=0
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
Counter < MaxR
4. Shoud I increment or drcrement the
Counter after each iteration?
Counter = Counter+1
5. What are the actions that should be
repeated?
Read a grade, add a grade, and
increment the Counter
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
Questions to answer before you start:
1. What is the maximum number of
repetition? Declare Average,Grade,
MaxR= 5 Counter=0, MaxR=5,
Total=0
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
False Logical
Counter < MaxR Condition
4. Shoud I increment or drcrement the
Counter after each iteration?
True
Counter = Counter+1
5. What are the actions that should be
repeated?
Read a grade, add a grade, and
increment the Counter
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
Questions to answer before you start:
1. What is the maximum number of
repetition? Declare Average,Grade,
MaxR= 5 Counter=0, MaxR=5,
Total=0
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
False
Counter < MaxR Counter<MaxR
4. Shoud I increment or drcrement the
Counter after each iteration?
True
Counter = Counter+1
5. What are the actions that should be
repeated?
Read a grade, add a grade, and
increment the Counter
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
Questions to answer before you start:
1. What is the maximum number of
repetition? Declare Average,Grade,
MaxR= 5 Counter=0, MaxR=5,
Total=0
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
False
Counter < MaxR Counter<MaxR Actions that should
be repeated
4. Shoud I increment or drcrement the
Counter after each iteration?
True
Counter = Counter+1
5. What are the actions that should be
repeated?
Read a grade, add a grade, and
increment the Counter
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
Questions to answer before you start:
1. What is the maximum number of
repetition? Declare Average,Grade,
MaxR= 5 Counter=0, MaxR=5,
Total=0
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
False
Counter < MaxR Counter<MaxR
Counter=Counter+1
4. Shoud I increment or drcrement the
Counter after each iteration?
True
Counter = Counter+1
Read Grade Total=Total+Grade
5. What are the actions that should be
repeated?
Read a grade, add a grade, and
increment the Counter
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
Questions to answer before you start:
1. What is the maximum number of
repetition? Declare Average,Grade,
MaxR= 5 Counter=0, MaxR=5,
Total=0
2. What is the initial value of the counter?
Counter = 0
3. What is the condition of repetition?
False
Counter < MaxR Counter<MaxR
Counter=Counter+1
4. Shoud I increment or drcrement the Average=Total/5
Counter after each iteration?
True
Counter = Counter+1 Print Average
Read Grade Total=Total+Grade
5. What are the actions that should be
repeated? End

Read a grade, add a grade, and


increment the Counter
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.

START

Declare Counter=0 MaxR=5


Total=0,Average=0,grade

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:

Declare Counter=0 MaxR=5


Total=0,Average=0,grade

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:

Itr 1 of True 40 40 0 1 Declare Counter=0 MaxR=5


repetition
Total=0,Average=0,grade

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:

Itr 1 of True 40 40 0 1 Declare Counter=0 MaxR=5


repetition
Total=0,Average=0,grade
Itr 2 of True 30 70 0 2
repetition

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:

Itr 1 of True 40 40 0 1 Declare Counter=0 MaxR=5


repetition
Total=0,Average=0,grade
Itr 2 of True 30 70 0 2
repetition

Itr 3 of True 100 170 0 3


repetition
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:

Itr 1 of True 40 40 0 1 Declare Counter=0 MaxR=5


repetition
Total=0,Average=0,grade
Itr 2 of True 30 70 0 2
repetition

Itr 3 of True 100 170 0 3


repetition
False
Counter<MaxR
Itr 4 of True 70 240 0 4 Counter=Counter+1
repetition 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:

Itr 1 of True 40 40 0 1 Declare Counter=0 MaxR=5


repetition
Total=0,Average=0,grade
Itr 2 of True 30 70 0 2
repetition

Itr 3 of True 100 170 0 3


repetition
False
Counter<MaxR
Itr 4 of True 70 240 0 4 Counter=Counter+1
repetition Average=Total/5

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:

Itr 1 of True 40 40 0 1 Declare Counter=0 MaxR=5


repetition
Total=0,Average=0,grade
Itr 2 of True 30 70 0 2
repetition

Itr 3 of True 100 170 0 3


repetition
False
Counter<MaxR
Itr 4 of True 70 240 0 4 Counter=Counter+1
repetition Average=Total/5

True
Itr 5 of True 60 300 0 5 Print Average
repetition Read Grade Total=Total+Grade

End of false 60 300 60 5 End


repetition
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

Declare Counter=0 MaxR=5


Total=0,Average=0,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.

 Let’s assume result is stored in fact variable.


 What is its default value? It is 1.
 Fact = 1
 Use for loop since it is repeating and why we use for loop?
 Because we know the exact iterations.
CSE 113 Introduction to Computer Science
and Engineering
Think Logically - Examples on Loops

Dr. Denizhan DEMİRKOL (Ph.D.)


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).
 Construct a tracing table for the flowchart mentioned above, assuming that the
user has entered A=3, B=4.
Think Logically - Problem 1
If you want to solve a problem, you should ask yourself these questions:

• What is the input of the problem? A, B (2 integer numbers)

• What is the output of the problem? 𝐴𝐴𝐵𝐵

• What are processes that could be applied on the input to get the output.

Repeated multiplication : A*A*….*A (B times)

• Do I need selection & repetition structures? If yes, where and how?

We need a repetition structure.

• What are the variables that I may need to use?

o Variable(s) for storing the input: A,B.

o Variable(s) for storing the output: Result

o Variable(s) for counting (if you have a repetition): Counter

o Intermediate variable(s) for the simplification of calculations: no need


Think Logically - Problem 1
When you want to construct a FOR structure, you should find the answers for these
questions:
1. What is the maximum number of repetition? Let us keep it in a vaiable named
MaxR.
2. What is the initial value of the counter? Let us keep it in a vaiable named Counter.
3. What is the logical condition (the condition of repetition)?
4. How to update the Counter?, shoud I increment or drcrement the Counter after
each iteration? (We have MaxR iterations)
5. What are the actions that should be repeated?

Counter = initial value

True Counter = Counter - 1


Logical Test
(Repetition condition) Actions to be Or
repeated Counter = Counter +1

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

Itr 4 of True 3 4 81 4 result= result*A


repetition
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

Itr 4 of True 3 4 81 4 result= result*A


repetition

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 is the input of the problem? N1,N2 (two integer number)


N1<N2 N1>=N2
• What is the output of the problem? (N1+1)+(N1+2)+….(N2-1) Or (N2+1)+(N2+2)+….(N1-1)

• What are processes that could be applied on the input to get the output.

Repeated addition (N1-N2-1 Or N2-N1-1 times)

• Do I need selection & repetition structures? If yes, where and how?

We need repetition and selection (N1<N2 Or N1>=N2?) structures

• What are the variables that I may need to use?

o Variable(s) for storing the input: N1,N2.

o Variable(s) for storing the output: Result

o Variable(s) for counting (if you have a repetition): Counter

o Intermediate variable(s) for the simplification of calculations: no need


Think Logically – Problem 2
When you want to construct a FOR structure, you should find the answers for these
questions:
1. What is the maximum number of repetition? Let us keep it in a vaiable named
MaxR.
2. What is the initial value of the counter? Let us keep it in a vaiable named Counter.
3. What is the logical condition (the condition of repetition)?
4. Shoud I increment or drcrement the Counter after each iteration? (we have MaxR
iterations)
5. What are the actions that should be repeated?

Counter = initial value

True Counter = Counter - 1


Logical Test
(Repetition condition) Actions to be Or
repeated Counter = Counter +1

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

Read N1,N2 Declare N1,N2


Count,Result=0
IF (N1<N2)

THEN Read N1,N2

FOR(Counter=N1+1; Counter<N2; Counter=Counter+1)

Result=Result+Counter

ENDFOR

ELSE

FOR(Counter=N2+1; Counter<N1; Counterer=Counter+1)

Result=Result+Counter

ENDFOR

ENDIF
Write Result
Think Logically - Problem 2 - Flowchart
Declare Counter, Result=0 START

Read N1,N2 Declare N1,N2


Counter,Result=0
IF (N1<N2)

THEN Read N1,N2

FOR(Counter=N1+1; Counter<N2; Counter=Counter+1) True False


N1<N2
Result=Result+Counter

ENDFOR

ELSE

FOR(Counter=N2+1; Counter<N1; Counter=Countere+1)

Result=Result+Counter

ENDFOR

ENDIF
Write Result
Think Logically - Problem 2 - Flowchart
Declare Counter, Result=0 START

Read N1,N2 Declare N1,N2


Counter,Result=0
IF (N1<N2)

THEN Read N1,N2

FOR(Counter=N1+1; Counter<N2; Counter=Counter+1) True False


N1<N2
Result=Result+Counter
Counter=N1+1
ENDFOR

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

Read N1,N2 Declare N1,N2


Counter,Result=0
IF (N1<N2)

THEN Read N1,N2

FOR(Counter=N1+1; Counter<N2; Counter=Counter+1) True False


N1<N2
Result=Result+Counter
Counter=N1+1 Counter=N2+1
ENDFOR

ELSE Counter<N2 Counter<N1


FOR(Counter=N2+1; Counter<N1; Counter=Counter+1) True True

Result=Result+Counter Result=Result+Counter Result=Result+Counter

ENDFOR
Counter=Counter+1 Counter=Counter+1
ENDIF
Write Result
Think Logically - Problem 2 - Flowchart
Declare Counter, Result=0 START

Read N1,N2 Declare N1,N2


Counter,Result=0
IF (N1<N2)

THEN Read N1,N2

FOR(Counter=N1+1; Counter<N2; Counter=Counter+1) True False


N1<N2
Result=Result+Counter
Counter=N1+1 Counter=N2+1
ENDFOR
False False
ELSE Counter<N2 Counter<N1
FOR(Counter=N2+1; Counter<N1; Counter=Counter+1) True True

Result=Result+Counter Result=Result+Counter Result=Result+Counter

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/

You might also like