You are on page 1of 155

CSE 113 Introduction to Computer Science and

Engineering
Introduction to Algorithms
Part 1

Dr. Denizhan DEMİRKOL (Ph.D)


Learning Objectives

 Understand what is meant by an algorithm.


 Understand what is meant by a variable.
 Understand the six basic statements in pseudocode.
Humans & Computer Algorithms
 An algorithm is a step-by-step method for solving a particular problem or doing a
specific task.
• Has input data, and is expected to produce output data.
• Each step can be carried out in a finite amount of time in a deterministic way.
Humans & Computer Algorithms
 An algorithm is a step-by-step method for solving a particular problem or doing a
specific task.
• Has input data, and is expected to produce output data.
• Each step can be carried out in a finite amount of time in a deterministic way.
 Do humans use algorithms in their daily life?
• Driving a car.
• Cooking.
• Passing a course in an engineering faculty.
• Many more.
Humans & Computer Algorithms
 An algorithm is a step-by-step method for solving a particular problem or doing a
specific task.
• Has input data, and is expected to produce output data.
• Each step can be carried out in a finite amount of time in a deterministic way.
 Do humans use algorithms in their daily life?
• Driving a car.
• Cooking.
• Passing a course in an engineering faculty.
• Many more.
 Do computers use algorithms in their mechanism?
• Operating system: Windows, Linux, Android, etc.
• Deep blue (PC) which overcame Kasparov in chess board.
• Many more.
 Deep Blue was a chess-playing expert system run on a unique
purpose-built IBM supercomputer. It was the first computer to
win a game, and the first to win a match, against a reigning
world champion under regular time controls.
 It first played world champion Garry Kasparov in a six-game
match in 1996, where it lost four games to two. It was
upgraded in 1997 and in a six-game re-match, it defeated
Kasparov by winning three games and drawing one.
 Deep Blue's victory is considered a milestone in the history
of artificial intelligence and has been the subject of several
books and films.
Examples

 Different types of problems require different types of algorithmic


techniques to be solved in the most optimized manner:
 Brute Force algorithm
 Recursive algorithm
 Greedy algorithm
 Backtracking algorithm
 Divide & Conquer algorithm
 Dynamic programming algorithm
 Randomized algorithm
 Sorting algorithm
 Searching algorithm
 Hashing algorithm
Humans & Computer Algorithms

 Humans can run algorithms using the five senses, brain, and memory.
Humans & Computer Algorithms

 Humans can run algorithms using the five senses, brain, and memory.

 Computers can also run algorithms using the input and output devices, CPU, and
storage device if they were written in a formal programming language such as C,
C++, C #, Pascal, Java, Matlab, Android, etc.
Humans & Computer Algorithms

 Humans can run algorithms using the five senses, brain, and memory.

 Computers can also run algorithms using the input and output devices, CPU, and
storage device if they were written in a formal programming language such as C,
C++, C #, Pascal, Java, Matlab, Android, etc.

 Before we learn how to write algorithms using a formal programming language,


we need to learn how to write algorithms in simpler forms such as flowcharts
and pseudocode.
Humans & Computer Algorithms

 Humans can run algorithms using the five senses, brain, and memory.

 Computers can also run algorithms using the input and output devices, CPU, and
storage device if they were written in a formal programming language such as C,
C++, C #, Pascal, Java, Matlab, Android, etc.

 Before we learn how to write algorithms using a formal programming language,


we need to learn how to write algorithms in simpler forms such as flowcharts
and pseudocode.

 Why?
Variables
 When humans run an algorithm, data are stored in their memory, humans can
save and retrieve the data automatically without specifying a location and a
name for each piece of data.
Variables
 When humans run an algorithm, data are stored in their memory, humans can
save and retrieve the data automatically without specifying a location and a
name for each piece of data.
 Computers do not have such abilities, and each piece of data should have a
specific name and unique location (cell or set of cells).
Variables
 When humans run an algorithm, data are stored in their memory, humans can
save and retrieve the data automatically without specifying a location and a
name for each piece of data.
 Computers do not have such abilities, and each piece of data should have a
specific name and unique location (cell or set of cells).
 Don't worry much, you need only to provide a name to a piece of data, and
the computer will allocate a unique location for it in the computer memory
(RAM).
Variables
 When humans run an algorithm, data are stored in their memory, humans can
save and retrieve the data automatically without specifying a location and a
name for each piece of data.
 Computers do not have such abilities, and each piece of data should have a
specific name and unique location (cell or set of cells).
 Don't worry much, you need only to provide a name to a piece of data, and the
computer will allocate a unique location for it in the computer memory (RAM).
 In terminology of algorithms, the word variable means a place in the computer
memory that can hold a piece of data, this place has a specific name and unique
physical address.
Variables
 When humans run an algorithm, data are stored in their memory, humans can
save and retrieve the data automatically without specifying a location and a
name for each piece of data.
 Computers do not have such abilities, and each piece of data should have a
specific name and unique location (cell or set of cells).
 Don't worry much, you need only to provide a name to a piece of data, and the
computer will allocate a unique location for it in the computer memory (RAM).
 In terminology of algorithms, the word variable means a place in the computer
memory that can hold a piece of data, this place has a specific name and unique
physical address.
 A variable can be also seen as a container to store
certain values. While the program is running,
variables are accessed and sometimes changed, i.e.
a new value will be assigned to the variable.

http://www.python-course.eu/variables.php
Characteristics of an Algorithm

 Finiteness: An algorithm should have finite number of


steps and it should end after a finite time.
 Well-defined Input and Output: An algorithm may have
many inputs or no inputs at all. It should result at least one
output.
 Definiteness: Each step must be clear, well-defined and
precise. There should be no any ambiguity.
 Effectiveness: Each step must be simple and should take a
finite amount of time.
Characteristics of an Algorithm
Disadvantages of an Algorithm

 Developing algorithm for complex problems would be


time consuming and difficult to understand.
 Understanding complex logic through algorithms can be
very difficult.
Pseudocode
 Pseudocode means fake code, because it is not a real programming code.

 It is a type of structured English that is used to specify an algorithm.

 Natural language and pseudocode describe algorithms so that humans can


understand them.
Pseudocode
 Pseudocode means fake code, because it is not a real programming code.

 It is a type of structured English that is used to specify an algorithm.

 It cannot be compiled nor executed on computers and has no real


formatting or syntax rules.

 It enables the programmer to concentrate on the algorithms without


worrying about all the syntactic details of a particular programming
language
Pseudocode
 Pseudocode means fake code, because it is not a real programming code.

 It is a type of structured English that is used to specify an algorithm.

 It cannot be compiled nor executed on computers, and has no real formatting or


syntax rules.

 It enables the programmer to concentrate on the algorithms without worrying


about all the syntactic details of a particular programming language
 Example:
what follows is a pseudocode for a program that gets two integer numbers
from the user, and then prints out their product.
Declare X1, X2, Result
Read X1,X2
Result = X1*X2
Write Result
The Art of Problem Solving

 As evidence of the elusive, artistic nature of problem


solving, the following loosely defined problem-solving
phases presented by the mathematician G. Polya in 1945
remain the basic principles on which many attempts to
teach problem-solving skills are based today.
 Phase 1. Understand the problem.
 Phase 2. Devise a plan for solving the problem.
 Phase 3. Carry out the plan.
 Phase 4. Evaluate the solution for accuracy and for its
potential as a tool for solving other problems.
Program development

 Translated into the context of program development, these


phases become:
 Phase 1. Understand the problem.
 Phase 2. Get an idea of how an algorithmic function might
solve the problem.
 Phase 3. Formulate the algorithm and represent it as a
program.
 Phase 4. Evaluate the program for accuracy and for its
potential as a tool for solving other problems.
Basic Pseudocode Statements
 There are six basic pseudocode statements that are used for writing algorithms:

www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
 There are six basic pseudocode statements that are used for writing algorithms:

1. Reading (or getting) data from an input device (keyboard, file, etc.).

www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
 There are six basic pseudocode statements that are used for writing algorithms:

1. Reading (or getting) data from an input device (keyboard, file, etc.).

2. Writing (or displaying) data to an output device (monitor, file, printer, etc.).

www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
 There are six basic pseudocode statements that are used for writing algorithms:

1. Reading (or getting) data from an input device (keyboard, file, etc.).

2. Writing (or displaying) data to an output device (monitor, file, printer, etc.).

3. Calculating (or performing) arithmetic operation on data like +, -, *, /.

www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
 There are six basic pseudocode statements that are used for writing algorithms:

1. Reading (or getting) data from an input device (keyboard, file, etc.).

2. Writing (or displaying) data to an output device (monitor, file, printer, etc.).

3. Calculating (or performing) arithmetic operation on data like +, -, *, /.

4. Assigning a value to a variable (fill a container).

www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
 There are six basic pseudocode statements that are used for writing algorithms:

1. Reading (or getting) data from an input device (keyboard, file, etc.).

2. Writing (or displaying) data to an output device (monitor, file, printer, etc.).

3. Calculating (or performing) arithmetic operation on data like +, -, *, /.

4. Assigning a value to a variable (fill a container).

5. Selection structures such as "IF-THEN-ELSE": compares two pieces of information


and select one of two alternative statements.

www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
 There are six basic pseudocode statements that are used for writing algorithms:

1. Reading (or getting) data from an input device (keyboard, file, etc.).

2. Writing (or displaying) data to an output device (monitor, file, printer, etc.).

3. Calculating (or performing) arithmetic operation on data like +, -, *, /.

4. Assigning a value to a variable (fill a container).

5. Selection structures such as "IF-THEN-ELSE": compares two pieces of information


and select one of two alternative statements.

6. Repetition structures such as "WHILE" and "FOR": repeats a statement or a group


of statements for a certain number of times.

www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
 There are six basic pseudocode statements that are used for writing algorithms:

1. Reading (or getting) data from an input device (keyboard, file, etc.).

2. Writing (or displaying) data to an output device (monitor, file, printer, etc.).

3. Calculating (or performing) arithmetic operation on data like +, -, *, /.

4. Assigning a value to a variable (fill a container).

5. Selection structures such as "IF-THEN-ELSE": compares two pieces of information


and select one of two alternative statements.

6. Repetition structures such as "WHILE" and "FOR": repeats a statement or a group


of statements for a certain number of times.

 According to the structure theorem, any computer program can be written using only the
three basic control structures: sequence, selection and repetition.

www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
The Sequence Structure
 It is a series of actions are performed in a
specific sequence indicated by the sequence
they appear in the program (what comes
first is executed first).
Statement 1
Statement 2
. Order of execution
.
Statement n

www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
The Sequence Structure
 It is a series of actions are performed in a
specific sequence indicated by the sequence
they appear in the program (what comes
first is executed first).
Statement 1
Statement 2
. Order of execution
.
Statement n

 Example:
Declare X1, X2, Sum
Read X1,X2
Sum = X1+X2
Write Sum
www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
The Sequence Structure
 It is a series of actions are performed in a  Let us remember:
specific sequence indicated by the sequence
they appear in the program (what comes
first is executed first).
Statement 1
Statement 2 http://blogs.articulate.com/rapid-elearning/how-

. Order of execution to-get-your-learners-to-remember-more/

. A statement could be:


Statement n 1.
2.
 Example:
3.
Declare X1, X2, Sum
4.
Read X1,X2
5.
Sum = X1+X2
6.
Write Sum
www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
The Sequence Structure
 It is a series of actions are performed in a  Let us remember:
specific sequence indicated by the sequence
they appear in the program (what comes
first is executed first).
Statement 1
Statement 2 http://blogs.articulate.com/rapid-elearning/how-

. Order of execution to-get-your-learners-to-remember-more/

. A statement could be:


Statement n 1. Read
2. Write
 Example:
3. Calculate
Declare X1, X2, Sum
4. Assign
Read X1,X2
5. Select
Sum = X1+X2
6. Repeat
Write Sum
www.cse.unr.edu/~fredrick/class/201/lecture/Pseudocode.doc
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container):
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container).
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
An assignement is executed in two steps:
1-Evaluation of the expression found on the right side.
2-Setting the value returned from the evaluation in the memory cell corresponding to the
variable on the left side.
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container).
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
An assignement is executed in two steps:
1-Evaluation of the expression found on the right side.
2-Setting the value returned from the evaluation in the memory cell corresponding to the
variable on the left side.
Example 1: RAM
?
?
?
?
?
?
?
?
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container).
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
An assignement is executed in two steps:
1-Evaluation of the expression found on the right side.
2-Setting the value returned from the evaluation in the memory cell corresponding to the
variable on the left side.
Example 1: RAM
Declare A, B ?
?
?
?
?
?
? A
? B
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container).
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
An assignement is executed in two steps:
1-Evaluation of the expression found on the right side.
2-Setting the value returned from the evaluation in the memory cell corresponding to the
variable on the left side.
Example 1: RAM
Declare A, B ?
?
?
A=2 ?
?
?
2 A
? B
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container).
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
An assignement is executed in two steps:
1-Evaluation of the expression found on the right side.
2-Setting the value returned from the evaluation in the memory cell corresponding to the
variable on the left side.
Example 1: RAM
Declare A, B ?
?
?
A=2 ?
B=3 ?
?
2 A
3 B
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container).
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
An assignement is executed in two steps:
1-Evaluation of the expression found on the right side.
2-Setting the value returned from the evaluation in the memory cell corresponding to the
variable on the left side.
Example 1: RAM
Declare A, B ?
?
?
A=2 ?
B=3 ?
?
A=A+2 4 A
3 B
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container).
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
An assignement is executed in two steps:
1-Evaluation of the expression found on the right side.
2-Setting the value returned from the evaluation in the memory cell corresponding to the
variable on the left side.
Example 1: RAM
Declare A, B ?
?
?
A=2 ?
B=3 ?
?
A=A+2 4 A
B=B+A 7 B
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container).
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
An assignement is executed in two steps:
1-Evaluation of the expression found on the right side.
2-Setting the value returned from the evaluation in the memory cell corresponding to the
variable on the left side.
Example 1: RAM
Declare A, B ? Notes:
? • In computer science, the instruction A=A+2
? means the new value of A is equal to the old
A=2 one plus two.
?
B=3 ?
?
A=A+2 4 A
B=B+A 7 B
Basic Pseudocode Statements
Assigning a value
 Assigning a value to a variable (fill a container).
On the left side of =, we put the name of a variable and on the right side we put a value
or an expression.
An assignement is executed in two steps:
1-Evaluation of the expression found on the right side.
2-Setting the value returned from the evaluation in the memory cell corresponding to the
variable on the left side.
Example 1: RAM
Declare A, B ? Notes:
? • In computer science, the instruction A=A+2
? means the new value of A is equal to the old
A=2 one plus two.
?
B=3 ? • The instruction A+2=A has no meaning in
? computer science (the left side must be a
A variable).
A=A+2 4
B=B+A 7 B
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?

? X

? Y

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
? X

? Y

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
2 X

3 Y

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
2 X
X=X+1

3 Y

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
3 X
X=X+1

3 Y

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
3 X
X=X+1
X=X+1 3 Y

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
4 X
X=X+1
X=X+1 3 Y

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
4 X
X=X+1
X=X+1 3 Y
X=X-Y
?

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
1 X
X=X+1
X=X+1 3 Y
X=X-Y
?

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
1 X
X=X+1
X=X+1 3 Y
X=X-Y
?
Y=X+1
?

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
1 X
X=X+1
X=X+1 2 Y
X=X-Y
?
Y=X+1
?

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
1 X
X=X+1
X=X+1 2 Y
X=X-Y
?
Y=X+1
X=Y*X ?

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
2 X
X=X+1
X=X+1 2 Y
X=X-Y
?
Y=X+1
X=Y*X ?

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
2 X
X=X+1
X=X+1 2 Y
X=X-Y
?
Y=X+1
X=Y*X ?

Declare Z=100 ?

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
2 X
X=X+1
X=X+1 2 Y
X=X-Y
100 Z
Y=X+1
X=Y*X ?

Declare Z=100 ?

?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
2 X
X=X+1
X=X+1 2 Y
X=X-Y
100 Z
Y=X+1
X=Y*X ?

Declare Z=100 ?
Initialize Conuter=0
?
Basic Pseudocode Statements
Assigning a value
Example 2:
RAM
Declare X, Y ?
X=2
?
Y=3
2 X
X=X+1
X=X+1 2 Y
X=X-Y
100 Z
Y=X+1
X=Y*X 0 Counter

Declare Z=100 ?
Initialize Counter=0
?
References

 https://www.site.uottawa.ca/~ivan/07.alg_1.ppt

 Prog 0101 fundamentals of programming- chapter 3


algorithms-FTMS Global College- Kuala Lumpur,
Malaysia.
CSE 113 Introduction to Computer Science and
Engineering
Introduction to Algorithms
Part 2

Dr. Denizhan Demirkol (Ph.D.)


Learning Objectives

 Understand the six basic statements in pseudocode.


 Using and applying pseudocode for writing algorithms.
 Understand the three basic types of control structures: sequence, decision
and repetition.
How to Think? Think Logically
If you want to solve a problem, you should ask yourself these questions:

• What is the input of the problem?

• What is the output of the problem?

• What are processes that should be applied on the input to get the output?

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

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

o Variable(s) for storing the input.

o Variable(s) for storing the output.

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

o Intermediate variable(s) for the simplification of calculations.


Pseudocode – Problem 1
Problem 1:
Write a pseudocode for a program that gets two integer
numbers from the user, and then prints out the sum of those
numbers.
Pseudocode – Problem 1
Problem 1:
Write a pseudocode for a program that gets two integer
numbers from the user, and then prints out the sum of those
numbers.

Pseudocode: X1 ?

Declare X1, X2, Sum X2 ?

Sum ?
Pseudocode – Problem 1
Problem 1:
Write a pseudocode for a program that gets two integer
numbers from the user, and then prints out the sum of those
numbers.

Pseudocode: X1 Value 1

Declare X1, X2, Sum X2 Value 2

Read X1,X2 http://www.danskebank.co.uk/en-gb/Business/Small- Sum ?


business/Online-services/Pages/business-ebanking.aspx
Pseudocode – Problem 1
Problem 1:
Write a pseudocode for a program that gets two integer
numbers from the user, and then prints out the sum of those
numbers.

Pseudocode: X1 Value 1

Declare X1, X2, Sum X2 Value 2

Read X1,X2
Sum Value 1 + Value 2

Sum = X1+X2
Pseudocode – Problem 1
Problem 1:
Write a pseudocode for a program that gets two integer
numbers from the user, and then prints out the sum of those
numbers.

Pseudocode: X1 Value 1

Declare X1, X2, Sum X2 Value 2

Read X1,X2
Sum Value 1 + Value 2

Sum = X1+X2
Write Sum http://goo.gl/ArNUmV
Pseudocode – Problem 2
Problem 2:
Write a pseudocode for a program that computes and prints
the average of any three numbers.
Pseudocode:
Pseudocode – Problem 2
Problem 2:
Write a pseudocode for a program that computes and prints
the average of any three numbers.
Pseudocode:
Declare X1, X2, X3, Sum, Average
Pseudocode – Problem 2
Problem 2:
Write a pseudocode for a program that computes and prints
the average of any three numbers.
Pseudocode:
Declare X1, X2, X3, Sum, Average
Read X1,X2,X3
Pseudocode – Problem 2
Problem 2:
Write a pseudocode for a program that computes and prints
the average of any three numbers.
Pseudocode:
Declare X1, X2, X3, Sum, Average
Read X1,X2,X3
Sum = X1+X2+X3
Pseudocode – Problem 2
Problem 2:
Write a pseudocode for a program that computes and prints
the average of any three numbers.
Pseudocode:
Declare X1, X2, X3, Sum, Average
Read X1,X2,X3
Sum = X1+X2+X3
Average=Sum/3
Pseudocode – Problem 2
Problem 2:
Write a pseudocode for a program that computes and prints
the average of any three numbers.
Pseudocode:
Declare X1, X2, X3, Sum, Average
Read X1,X2,X3
Sum = X1+X2+X3
Average=Sum/3
Write Average
Pseudocode – Problem 3
Problem 3:
Write a pseudocode for a program that calcultes the area of
an rectangle.
Pseudocode:
Pseudocode – Problem 3
Problem 3:
Write a pseudocode for a program that calcultes the area of
an rectangle.
Pseudocode:
Declare Length, Width, Area
Read Length, Width
Area= Length * Width
Write Area
Pseudocode – Problem 4
Problem 4:
Write a pseudocode for a program that calculates and
displays the square (second power) and cube (third power)
of an integer number.
Pseudocode:
Pseudocode – Problem 4
Problem 4:
Write a pseudocode for a program that calculates and
displays the square (second power) and cube (third power)
of an integer number.
Pseudocode:
Declare X, Square, Cube
Read X
Square = X* X
Cube= Square *X
Write Square, Cube
Basic Pseudocode Statements
Relational Operators
 A relational operator is a binary operator that compares two values.

http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Relational Operators
 A relational operator is a binary operator that compares two values.
 The following symbols are almost used by all programmers:
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to

http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Relational Operators
 A relational operator is a binary operator that compares two values.
 The following symbols are almost used by all programmers:
 A condition (or relational expression) is < less than

an expression that contains at least one > greater than


relational operators. <= less than or equal to
 The result of a relational expression is >= greater than or equal to
always == equal to
boolean value, TRUE or FALSE. != not equal to

http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Relational Operators
 A relational operator is a binary operator that compares two values.
 The following symbols are almost used by all programmers:
 A condition (or relational expression) is < less than

an expression that contains at least one > greater than


relational operators. <= less than or equal to
 The result of a relational expression is >= greater than or equal to
always == equal to
boolean value, TRUE or FALSE. != not equal to
 Examples:
• X1=12,X2=10, (X1>X2) TRUE.
• X1=12,X2=10, (X1<X2) FALSE.
• X1=12,X2=12, (X1!=X2) FALSE.
• A1=12,A2=10, (A1>=A2) .
• B2=0,C1=10, (B2==C1) .
http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Relational Operators
 A relational operator is a binary operator that compares two values.
 The following symbols are almost used by all programmers:
 A condition (or relational expression) is < less than

an expression that contains at least one > greater than


relational operators. <= less than or equal to
 The result of a relational expression is >= greater than or equal to
always == equal to
boolean value, TRUE or FALSE. != not equal to
 Examples:
• X1=12,X2=10, (X1>X2) TRUE. • (100>2) TRUE.
• X1=12,X2=10, (X1<X2) FALSE. • ((100>2) ==FALSE) FALSE.
• X1=12,X2=12, (X1!=X2) FALSE. • (-10<-100) FALSE.
• A1=12,A2=10, (A1>=A2) . • (2==(3-1)) .
• B2=0,C1=10, (B2==C1) . • B2=0,(B2==(10-1)) .
http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Logical Operators
 The three logical operators of programming are AND, OR, and NOT.

http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Logical Operators
 The three logical operators of programming are AND, OR, and NOT.
 The AND operator
requires both operands
(values) to be true for the
result to be true.
TRUE AND TRUE = TRUE
TRUE AND FALSE = FALSE
FALSE AND TRUE = FALSE
FALSE AND FALSE = FALSE

http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Logical Operators
 The three logical operators of programming are AND, OR, and NOT.
 The AND operator  The OR operator
requires both operands requires one operand
(values) to be true for the (values) to be true for the
result to be true. result to be true.
TRUE AND TRUE = TRUE TRUE OR TRUE = TRUE
TRUE AND FALSE = FALSE TRUE OR FALSE = TRUE
FALSE AND TRUE = FALSE FALSE OR TRUE = TRUE
FALSE AND FALSE = FALSE FALSE OR FALSE = FALSE

http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Logical Operators
 The three logical operators of programming are AND, OR, and NOT.
 The AND operator  The OR operator  The NOT operator is
requires both operands requires one operand a unary operator that
(values) to be true for the (values) to be true for the changes a boolean
result to be true. result to be true. value to its opposite.
TRUE AND TRUE = TRUE TRUE OR TRUE = TRUE
NOT TRUE = FALSE
TRUE AND FALSE = FALSE TRUE OR FALSE = TRUE
FALSE AND TRUE = FALSE FALSE OR TRUE = TRUE
NOT FALSE = TRUE
FALSE AND FALSE = FALSE FALSE OR FALSE = FALSE

http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Logical Operators
 The three logical operators of programming are AND, OR, and NOT.
 The AND operator  The OR operator  The NOT operator is
requires both operands requires one operand a unary operator that
(values) to be true for the (values) to be true for the changes a boolean
result to be true. result to be true. value to its opposite.
TRUE AND TRUE = TRUE TRUE OR TRUE = TRUE
NOT TRUE = FALSE
TRUE AND FALSE = FALSE TRUE OR FALSE = TRUE
FALSE AND TRUE = FALSE FALSE OR TRUE = TRUE
NOT FALSE = TRUE
FALSE AND FALSE = FALSE FALSE OR FALSE = FALSE

 Examples:
• ( (2+3 < 10) AND (21 > 19) )  T.
• ( (15< 10) AND (21 > 19) )  F.
• ( (5==10) AND (21 <= 19) )  F.
• ( (TRUE) OR (2 > 19) )  T.
http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
Basic Pseudocode Statements
Logical Operators
 The three logical operators of programming are AND, OR, and NOT.
 The AND operator  The OR operator  The NOT operator is
requires both operands requires one operand a unary operator that
(values) to be true for the (values) to be true for the changes a boolean
result to be true. result to be true. value to its opposite.
TRUE AND TRUE = TRUE TRUE OR TRUE = TRUE
NOT TRUE = FALSE
TRUE AND FALSE = FALSE TRUE OR FALSE = TRUE
FALSE AND TRUE = FALSE FALSE OR TRUE = TRUE
NOT FALSE = TRUE
FALSE AND FALSE = FALSE FALSE OR FALSE = FALSE

 Examples:
• ( (2+3 < 10) AND (21 > 19) )  T. • (NOT (100>2)) F.
• ( (15< 10) AND (21 > 19) )  F. • ((NOT(10!=100) AND (10<=12 ))F.
• ( (5==10) AND (21 <= 19) )  F. • ((NOT(3<1) ) OR (1>19)) .
• ( (TRUE) OR (2 > 19) )  T. • ((9==(10-1) OR (NOT(20>2))) .
http://web.fuhsd.org/debbie_frazier/Lessons/L06if-else/L06if-else.html
References

 https://www.site.uottawa.ca/~ivan/07.alg_1.ppt

 Prog 0101 fundamentals of programming- chapter 3


algorithms-FTMS Global College- Kuala Lumpur,
Malaysia.
CSE 113 Introduction to Computer Science and
Engineering
Introduction to Algorithms
Part 3

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


Flowcharts
 A Flowchart is a graphical representation composed of shapes, lines, and text
that graphically illustrates the steps of an algorithm.

 Flowcharts are used to put forward the algorithm by explaning the steps that
need to be done in a visual way, it shows what should be done to fix the
problem from beginning to end by using the symbols that consist of geometric
shapes. Each symbol in general indicates a command or task to do.

 Flowchart begins with Start icon and ends with Stop icon.
Flowchart Building Units

Oval
Start/End

The start/end symbol :represents the start or the end of an algorithm.


Flowchart Building Units

Arrow

The sequence symbol: represents (flow of control) the sequence of


steps in an algorithms.

Connections between symbols are shown with arrows, direction of


the arrow indicates the direction of the flow process.
Flowchart Building Units
1. Read
Rectangle 2. Write
3. Calculate
=,+,/,*,- 4. Assign
5. Select
6. Repeat

The process symbol represents:


 Calculating (or performing) arithmetic operation on data like +, -, *, /.
 Or, Assigning a value to a variable (fill a container).
.
Flowchart Building Units
1. Read
2. Write
Parallelograms
3. Calculate
Input,Output 4. Assign
5. Select
6. Repeat

The input (read)/output (write) symbol represents:


 Reading (or getting) data from an input device (keyboard, file, etc.).
 Writing (or displaying) data to an output device (monitor, file,
printer, etc.).
Basic Flowchart Structures

There three basic types of control structures in flowcharts:


1. The sequence structure.
2. The selection structure.
3. The repetition structure.
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.
Problem1:
Draw a flowchart for a program that gets
two integer numbers from the user, and
then print out the sum of those numbers.

Pseudocode:
Declare X1, X2, Sum
Read X1,X2
Sum = X1+X2
Write Sum
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.
Problem1:
Draw a flowchart for a program that gets
two integer numbers from the user, and
then print out the sum of those numbers.
Start

Pseudocode: Declare
X1,X2,Sum
Declare X1, X2, Sum
Read X1,X2 Read X1,X2
Sum = X1+X2
Write Sum
Sum=X1+X2

Write Sum

End
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.
Problem1:
Draw a flowchart for a program that gets
two integer numbers from the user, and
then print out the sum of those numbers.
Start

Pseudocode: Declare
X1,X2,Sum
Declare X1, X2, Sum
Read X1,X2 Read X1,X2
Sum = X1+X2
Write Sum
Sum=X1+X2

Write Sum

End
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.
Problem 2:
Draw a flowchart for a program that
computes and prints the average of any
three numbers.
Pseudocode:
Declare X1, X2, X3
Sum, Average
Read X1,X2,X3
Sum = X1+X2+X3
Average=Sum/3
Write Average
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.
Problem 2:
Draw a flowchart for a program that
computes and prints the average of any
three numbers. Start

Pseudocode:
Declare
X1,X2,Sum,Average
Declare X1, X2, X3
Sum, Average
Read X1,X2,X3
Read X1,X2,X3
Sum = X1+X2+X3
Sum=X1+X2,X3
Average=Sum/3
Write Average Average=Sum/3

Write Average

End
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.

Problem 3:
Draw a flowchart for a program that calculates the area of an rectangle.

Pseudocode:
Declare Length, Width, Area
Read Length, Width
Area= Length * Width
Write Area
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.

Problem 3:
Draw a flowchart for a program that calculates the area of an rectangle.

Start

Pseudocode:
Declare Length,
Width, Area
Declare Length, Width, Area
Read Length, Width
Area= Length * Width Read Length, Width
Write Area
Area= Length * Width

Write Area

End
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.
Problem 4:
Draw a flowchart for a program that
calculates and displays the square
(second power) and cube (third
power) of an integer number.
Pseudocode:
Declare X, Square, Cube
Read X
Square = X* X
Cube= Square *X
Write Square, Cube
Basic Flowchart Structures

1-The sequence structure is a series of actions are performed in a specific sequence


indicated by the arrows symbols.
Problem 4:
Draw a flowchart for a program that
calculates and displays the square
(second power) and cube (third Start
power) of an integer number.
Pseudocode: Declare X, Square,
Cube

Declare X, Square, Cube


Read X Read X
Square = X* X Square = X* X
Cube= Square *X
Write Square, Cube Cube= Square *X

Write Square, Cube

End
Flowcharts – Exercise 1
Exercise 1:
Draw a flowchart for a program that calculates the area and
circumference of a circle.
Hints: area = PI * radius * radius; circumference = 2 * PI * radius.
Flowcharts – Exercise 2
Exercise 2:
Draw a flowchart for a program that reads a temperature given in
Fahrenheit, converts it into Celsius degree, and prints it.
Hint: use this formula c=(f-32)*5/9 to convert Fahrenheit into Celsius.
References

 https://www.site.uottawa.ca/~ivan/07.alg_1.ppt

 Prog 0101 fundamentals of programming- chapter 3


algorithms-FTMS Global College- Kuala Lumpur,
Malaysia.
CSE 113 Introduction to Computer Science and
Engineering
Introduction to Algorithms
Part 4

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


Basic Pseudocode Statements
The "IF-THEN-ELSE" Selection Structure
 The decesion statement " If-Then-Else" Compares two pieces of information
and select one of two alternative statements.
 The If-Then-Else may have this form:
IF (condition)
THEN
Statement(s)
ELSE
Alternative Statement (s)
ENDIF
Basic Pseudocode Statements
The "IF-THEN-ELSE" Selection Structure
 The decesion statement " If-Then-Else" Compares two pieces of information
and select one of two alternative statements.
 The If-Then-Else may have this form:
IF (condition) Example 1:
IF (Total-Mark >49)
THEN
THEN
Statement(s) Write Pass
ELSE ELSE
Alternative Statement (s) Write Fail
ENDIF ENDIF
Basic Pseudocode Statements
The "IF-THEN-ELSE" Selection Structure
 The decesion statement " If-Then-Else" Compares two pieces of information
and select one of two alternative statements.
 The If-Then-Else may have this form:
IF (condition) Example 1:
IF (Total-Mark >49)
THEN
THEN
Statement(s) Write Pass
ELSE ELSE
Alternative Statement (s) Write Fail
ENDIF ENDIF
Example 2:
IF (Absence-Hours >12)
THEN
Write Fail
ELSE
Write Pass
ENDIF
Basic Pseudocode Statements
The "IF-THEN-ELSE" Selection Structure
 The decesion statement " If-Then-Else" Compares two pieces of information
and select one of two alternative statements.
 The If-Then-Else may have this form:
IF (condition) Example 1:
IF (Total-Mark >49)
THEN
THEN
Statement(s) Write Pass
ELSE ELSE
Alternative Statement (s) Write Fail
ENDIF ENDIF
Example 2: Example 3:
IF (Absence-Hours >12) IF (The class is finished)
THEN THEN
Write Fail Write Turn on your mobile
ELSE ELSE
Write Pass Write Turn off your mobile
ENDIF ENDIF
Basic Pseudocode Statements
The "IF-THEN-ELSE" Selection Structure
 The decesion statement " If-Then-Else" Compares two pieces of information
and select one of two alternative statements.
 The If-Then-Else may have this form:
IF (condition) Adding Example 1:
Indentation to IF (Total-Mark >49)
THEN
show the THEN
Statement(s) dependency is Write Pass
ELSE essential. ELSE
Alternative Statement (s) Write Fail
ENDIF ENDIF
Example 2: Example 3:
IF (Absence-Hours >12) IF (The class is finished)
THEN THEN
Write Fail Write Turn on your mobile
ELSE ELSE
Write Pass Write Turn off your mobile
ENDIF ENDIF
Basic Pseudocode Statements
The "IF-THEN" Selection Structure
 We may need to do nothing if the condition is evaluated to FALSE, in this case
we can omit the ELSE part, and The If-Then will have this form:
IF (condition)
THEN
Statement(s)
ENDIF
Basic Pseudocode Statements
The "IF-THEN" Selection Structure
 We may need to do nothing if the condition is evaluated to FALSE, in this case
we can omit the ELSE part, and The If-Then will have this form:
IF (condition)
THEN
Statement(s)
ENDIF

Example 1:
IF (a student is absent)
THEN
Incease his absence hours
EENDIF
Basic Pseudocode Statements
The "IF-THEN" Selection Structure
 We may need to do nothing if the condition is evaluated to FALSE, in this case
we can omit the ELSE part, and The If-Then will have this form:
IF (condition) Example 2:
THEN IF (student grade > 49)
THEN
Statement(s)
Add his name to the list
ENDIF ENDIF

Example 1:
IF (a student is absent)
THEN
Incease his absence hours
EENDIF
Basic Pseudocode Statements
The "IF-THEN" Selection Structure
 We may need to do nothing if the condition is evaluated to FALSE, in this case
we can omit the ELSE part, and The If-Then will have this form:
IF (condition) Example 2:
THEN IF (student grade > 49)
THEN
Statement(s)
Add his name to the list
ENDIF ENDIF

Example 1: Example 3:
IF (a student is absent) IF (a student has an active paticipation record)
THEN THEN
Incease his absence hours He may get a bonus from his teacher
EENDIF ENDIF
Pseudocode – Problem 5
Problem 5:
Write a pseudocode for a program that reads a student’s grade, and
then check if it is bigger than 49 it will print PASS otherwise it will
print FAIL.
Pseudocode:
Pseudocode – Problem 5
Problem 5:
Write a pseudocode for a program that reads a student’s grade, and
then check if it is bigger than 49 it will print PASS otherwise it will
print FAIL.
Pseudocode:
Declare grade
Read grade
IF (grade > 49)
THEN
Write Pass
ELSE
Write Fail
ENDIF
Pseudocode – Problem 6
Problem 6:
Write a pseudocode for a program that gets from the user 3 integer numbers, and then
returns the maximum one between them.
Pseudocode:
Pseudocode – Problem 6
Problem 6:
Write a pseudocode for a program that gets from the user 3 integer numbers, and then
returns the maximum one between them.
Pseudocode:
Declare X1, X2, X3, Max
Pseudocode – Problem 6
Problem 6:
Write a pseudocode for a program that gets from the user 3 integer numbers, and then
returns the maximum one between them.
Pseudocode:
Declare X1, X2, X3, Max
Read X1,X2,X3
Pseudocode – Problem 6
Problem 6:
Write a pseudocode for a program that gets from the user 3 integer numbers, and then
returns the maximum one between them.
Pseudocode:
Declare X1, X2, X3, Max
Read X1,X2,X3
IF (X1> X2 & X1>X3)
THEN

ELSE

ENDIF
Pseudocode – Problem 6
Problem 6:
Write a pseudocode for a program that gets from the user 3 integer numbers, and then
returns the maximum one between them.
Pseudocode:
Declare X1, X2, X3, Max
Read X1,X2,X3
IF (X1> X2 & X1>X3)
THEN
Max=X1
ELSE

ENDIF
Pseudocode – Problem 6
Problem 6:
Write a pseudocode for a program that gets from the user 3 integer numbers, and then
returns the maximum one between them.
Pseudocode:
Declare X1, X2, X3, Max
Read X1,X2,X3
IF (X1> X2 & X1>X3)
THEN
Max=X1
ELSE
IF (X2>X1 & X2>X3)
THEN

ELSE

ENDIF
ENDIF
Pseudocode – Problem 6
Problem 6:
Write a pseudocode for a program that gets from the user 3 integer numbers, and then
returns the maximum one between them.
Pseudocode:
Declare X1, X2, X3, Max
Read X1,X2,X3
IF (X1> X2 & X1>X3)
THEN
Max=X1
ELSE
IF (X2>X1 & X2>X3)
THEN
Max=X2
ELSE

ENDIF
ENDIF
Pseudocode – Problem 6
Problem 6:
Write a pseudocode for a program that gets from the user 3 integer numbers, and then
returns the maximum one between them.
Pseudocode:
Declare X1, X2, X3, Max
Read X1,X2,X3
IF (X1> X2 & X1>X3)
THEN
Max=X1
ELSE
IF (X2>X1 & X2>X3)
THEN
Max=X2
ELSE
Max=X3
ENDIF
ENDIF
Pseudocode – Problem 6
Problem 6:
Write a pseudocode for a program that gets from the user 3 integer numbers, and then
returns the maximum one between them.
Pseudocode:
Declare X1, X2, X3, Max
Read X1,X2,X3
IF (X1> X2 & X1>X3)
THEN
Max=X1
ELSE
IF (X2>X1 & X2>X3)
THEN
Max=X2
ELSE
Max=X3
ENDIF
ENDIF
Write MAX
Basic Flowchart Structures

2-The decision (or conditional) structure is equivalent to the if/then/else statement in


pseudocode and the if function in Excel.

False (or No) True (or Yes)


Logical
Test
Basic Flowchart Structures

2-The decision (or conditional) structure is equivalent to the if/then/else statement in


pseudocode and the if function in Excel.
Problem 5:
Draw a flowchart for a program
that reads a student’s grade, tests
it if PASS (more than 49) or
FAIL, and prints the result.

Pseudocode: False True


Declare Grade Logical
Read Grade Test
IF (Grade > 49)
THEN
Write Pass
ELSE
Write Fail
ENDIF
Basic Flowchart Structures

2-The decision (or conditional) structure is equivalent to the if/then/else statement in


pseudocode and the if function in Excel.
Start
Problem 5:
Draw a flowchart for a program
that reads a student’s grade, tests
it if PASS (more than 49) or
FAIL, and prints the result.

Pseudocode: False True


Declare Grade Logical
Read Grade Test
IF (Grade > 49)
THEN
Write Pass
ELSE
Write Fail
ENDIF
Basic Flowchart Structures

2-The decision (or conditional) structure is equivalent to the if/then/else statement in


pseudocode and the if function in Excel.
Start
Problem 5:
Draw a flowchart for a program Declare Grade
that reads a student’s grade, tests
it if PASS (more than 49) or
FAIL, and prints the result.

Pseudocode: False True


Declare Grade Logical
Read Grade Test
IF (Grade > 49)
THEN
Write Pass
ELSE
Write Fail
ENDIF
Basic Flowchart Structures

2-The decision (or conditional) structure is equivalent to the if/then/else statement in


pseudocode and the if function in Excel.
Start
Problem 5:
Draw a flowchart for a program Declare Grade
that reads a student’s grade, tests
it if PASS (more than 49) or Read Grade
FAIL, and prints the result.

Pseudocode: False True


Declare Grade Logical
Read Grade Test
IF (Grade > 49)
THEN
Write Pass
ELSE
Write Fail
ENDIF
Basic Flowchart Structures

2-The decision (or conditional) structure is equivalent to the if/then/else statement in


pseudocode and the if function in Excel.
Start
Problem 5:
Draw a flowchart for a program Declare Grade
that reads a student’s grade, tests
it if PASS (more than 49) or Read Grade
FAIL, and prints the result.

Pseudocode: False True


Declare Grade Grade>49
Read Grade
IF (Grade > 49)
THEN
Write Pass
ELSE
Write Fail
ENDIF
Basic Flowchart Structures

2-The decision (or conditional) structure is equivalent to the if/then/else statement in


pseudocode and the if function in Excel.
Start
Problem 5:
Draw a flowchart for a program Declare Grade
that reads a student’s grade, tests
it if PASS (more than 49) or Read Grade
FAIL, and prints the result.

Pseudocode: False True


Declare Grade Grade>49
Read Grade
IF (Grade > 49)
THEN Write Fail Write Pass
Write Pass
ELSE
Write Fail
ENDIF
Basic Flowchart Structures

2-The decision (or conditional) structure is equivalent to the if/then/else statement in


pseudocode and the if function in Excel.
Start
Problem 5:
Draw a flowchart for a program Declare Grade
that reads a student’s grade, tests
it if PASS (more than 49) or Read Grade
FAIL, and prints the result.

Pseudocode: False True


Declare Grade Grade>49
Read Grade
IF (Grade > 49)
THEN Write Fail Write Pass
Write Pass
ELSE
Write Fail
ENDIF End
Basic Flowchart Structures
Problem 5:
Draw a flowchart for a program that reads a student’s grade, tests it if PASS (more than 49)
or FAIL, and prints the result.
Start

Declare Grade

Read Grade

False True
Grade>49

Write Fail Write Pass

End
Basic Flowchart Structures
2-The decision (or conditional) structure is equivalent to the if/then/else statement in pseudocode
and the if function in excel.
Problem 6 :
Draw a flowchart for a program that gets from the user 3 integer numbers, and then returns the maximum one
between them.
Pseudocode:
Declare X1, X2, X3, Max
Read X1,X2,X3
IF (X1> X2)
THEN
IF (X1>X3)
THEN
Max=X1
ELSE
Max=X3
END
ELSE
IF (X2>X3)
THEN
Max=X2
ELSE
Max=X3
ENDIF
ENDIF
Write MAX
Basic Flowchart Structures
2-The decision (or conditional) structure is equivalent to the if/then/else statement in pseudocode
and the if function in excel.
Problem 6 :
Draw a flowchart for a program that gets from the user 3 integer numbers, and then returns the maximum one
between them.
Pseudocode:
Declare X1, X2, X3, Max START
Read X1,X2,X3
IF (X1> X2) Declare X1, X2, X3, Max
THEN
IF (X1>X3) Read X1,X2,X3
THEN
Max=X1
ELSE Yes No
X1>X2
Max=X3
END
Yes
ELSE Yes No No
X1>X3 X2>X3
IF (X2>X3)
THEN Max=X3 Max=X2
Max=X1
Max=X2
ELSE Print the maximum number is: Max
Max=X3
ENDIF
ENDIF END
Write MAX
Basic Flowchart Structures
Problem 6 :
Draw a flowchart for a program that gets from the user 3 integer numbers, and then returns the maximum one
between them.

START

Declare X1, X2, X3, Max

Read X1,X2,X3

Yes No
X1>X2

Yes
Yes No No
X1>X3 X2>X3

Max=X3 Max=X2
Max=X1

Print the maximum number is: Max

END
Pseudocode – Exercise 3
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.
Hint: divide the number on 2 and check if the remainder is equals to zero.
Pseudocode:
Pseudocode – Exercise 4
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.
Pseudocode – Exercise 5
Exercise 5:
Write a pseudocode for a program that calculates and prints the roots (if exist) 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.
Pseudocode:
Pseudocode – Exercise 6
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 – 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.
References

 https://www.site.uottawa.ca/~ivan/07.alg_1.ppt

 Prog 0101 fundamentals of programming- chapter 3


algorithms-FTMS Global College- Kuala Lumpur,
Malaysia.

You might also like