You are on page 1of 53

Chapter 1. Introduction to programming.

Algorithmic thinking, reasoning


Flowcharts, pseudo-codes.
Lecture 1

Instructor: Etibar Vazirov


Algorithmic thinking
Computers help us and solve the problem in two stages:

1. We are thinking about the steps needed to solve the problem


2. We use our technical skills to make the computer work on the problem

Computers can't think, they just carry out commands, and if they think, it's us.

When thinking about calculations, we use the following concepts to get into the
problem:

❖ Logical judgment: prediction, analysis, clarification


❖ Algorithms: set steps and rules
❖ Decomposition: Divide the problem into parts
❖ Abstract: ability to manage complexity (sometimes throwing away unnecessary
parts)
Logical reasoning
Logical reasoning is the use of one or more considerations in order to obtain a valid
conclusion.

1. If you are reading this, then you have opened this slide.
2. You are reading this.
3. So you've opened this slide. (Result)

Let's say one of your friends printed this slide on a piece of paper and gave it to you to
read. In this case, the logical argument is still valid because its form is correct, but the
result may be questionable because the first reasoning is incorrect.

This shows what logic is and what is not. Logic is not a magic that always gives you
the right answer, it is simply a formal system that turns many considerations into valid
results within the system.
Some deficiencies in formal logic
Confirming the result:

1. If you read this, then you can read.


2. You can read.
3. So you are reading this. (Result)

Consideration 1 consists of 2 parts: the beginning and the end. The result does not conform to the rules
of logic because here the IF-THEN relationship is reversed.

Negating what comes before:

4. If someone reads it, then they can read.


5. They don't read it.
6. So they can't read. (Result)

The first consideration is directly related to its outcome, but the opposite is never intended and therefore
cannot be accepted.
What is an algorithm?
We call an algorithm a systematic logical approach that allows a computer to solve
a problem, consisting of well-defined, step-by-step procedures.

Of course, this is not the only and perfect definition of the algorithm. However, if
we look at it in terms of a logical approach, it would be more appropriate to define
the algorithm as above.

The word algorithm is named after Al-Khwarizmi, a prominent 9th-century Persian


mathematician. (Al-khowarizmi - Algorismus (in Latin) - Algorithm)

If the solution of any problem can be expressed in the form of a set of executable
instructions , it is called an algorithmic problem.

In this regard, the algorithm can be defined more strongly:


Characteristics of the algorithm

An algorithm is a calculation procedure that consists of a set of instructions that


takes a certain value (or set of values) as an INPUT and then sends the required
value (or set of values) to the OUTPUT.

The algorithm must have the following important properties:

❖ Each step must be precise, useful and definite.


❖ It must stop after a finite number of steps and produce results.
❖ It must be effective, that is, it must give the correct answer.
❖ It must be general, that is, it must work correctly for each instance of the
same type of problem.
Characteristics of the algorithm
An Algorithm is implemented in some programming language.

program = Algorithm + Data Structures.

Data Structures refer to the types of data used and how the data are organized in
the program.

An algorithm is usually presented in the form of some pseudo-code, which is a


mixture of English statement,some mathematical notations,and selected keywords
from a programming language.
Problem Solving

Consider the problem below:

You are required to design a complete system which will enable the sum of two values
to be calculated.

As the computer is also a device similar to the way in which the human brain functions,
the process of calculating the sum of two values can also be easily performed by the
computer.
Problem Solving
Problem Solving
Problem Solving
Problem Solving
As shown previously, the example values (5 and 10) have been specified explicitly. As the brain is
flexible enough in calculating a wide range of numbers, the two input values have to be
generalised.

Notice that instead of using specific numbers, variables are used to represent these values.
What Are Variables?
Variables are memory locations within the computer which allows pieces of data to
be stored. The word variable comes from the word vary, which means that whatever
you place within a variable can be changed. A variable can be viewed as a container
used to store things.
Problem Solving
Now that we have an exact idea about how the problem is solved, let us represent
this in a clearer manner, using the defining diagram.

Input Processing Output

Value1
? Sum
Value2
Problem Solving
The next step is to identify the actual processing steps required to convert the input to
become the output.

Input Processing Output

Value1 1)Read Value1,Value2


2)Calculate Sum Sum
Value2 3)Display Sum

Once the defining diagram has been developed, the next logical step is to develop the
algorithm (which is much more detailed).
Algebraic operators
The basic mathematical operators used in algorithms are as follows:

+ addition

- subtraction

* multiplication

/ division

% modulus (remainder by division)

= assignment

( ) brackets for grouping calculations


Algorithm Development
Example of an algorithm (using pseudocodes) which can be used to carry out the
tasks outlined in the defining diagram is as follows:

1) Read Value1, Value2


2) Calculate
Sum = Value1 + Value2
3) Display Sum
Pseudocoding
A Pseudocode language is semi-formal, English-like language with a limited
vocabulary that can be used to design and describe algorithms.

Pseudocode is a generic way of describing an algorithm without use of any


specific programming language syntax. It is, as the name suggests, pseudo code —it
cannot be executed on a real computer, but it models and resembles real
programming code, and is written at roughly the same level of detail.

Computer science textbooks often use pseudocode in their examples so that all
programmers can understand them, even if they do not all know the same
programming languages.

Pseudocode, by nature, exists in various forms, although most borrow syntax from
popular programming languages (like C, Lisp, or FORTRAN)
Control Structures
The key to better algorithm design and thus to programming lies in limiting the control
structure to only three constructs. These are illustrated below:

❏ Sequence Structure
❏ Selection (or Decision) Structure
❏ Repetition (or Iteration) Structure
Sequence Control Structure
The Sequence Control Structure:

The sequence control structure is a series of steps or statements that are executed in the order
in which they are written in an algorithm.

For Example:

For example, suppose you are required to design an algorithm for finding the average of six
numbers, and the sum of the numbers is given. The pseudocode will be as follows:

1. Start
2. Get the sum
3. Average = sum / 6
4. Output the average
5. Stop
The Selection Control Structure:
The selection control structure defines two courses of action, depending on the
outcome of a condition. A condition is an expression that, when evaluated, computes
to either true or false.

Syntax is:
Decision Making
Being able to mimic the way the human brain works, the computer also has the ability
to make decisions.

Decision making can be represented in pseudo-codes using the IF...THEN construct.

The expression is a comparison between two values which evaluates to either true or
false.

Statements are placed here.


Decision Making

Example:

We are looking for a job which pays more than 4000 $.


Decision Making
Commonly used relational operators in expressions:-

> Greater Than

< Less Than

= Equals To

< > Not Equals To

>= Greater Than or Equals To

<= Less Than or Equals To

( ) Brackets used for prioritising certain calculations


Decision Making
Since all expressions works out to be either true or false, what the IF..THEN
statement represents is a two-state condition.

For example,

A potential employer is waiting for you to give a reply (on the spot) about the job offer
with a salary of RM2000. Your decision would be to only take a job worth more than
4000 $. What would you say?
Decision Making with compound expressions

Certain conditions may give rise to more than one expression being evaluated. These
are known as compound expressions.

Example:

You are interested in taking up a job which pays more than 4000 $ and that the
company must also provide a credit card.
Decision Making

Compound expressions can be represented using the following operators:

● AND Every expression must evaluate to be true in order for the whole
expression to be true.
● OR As long as any one of the expression can be true, the entire IF
statement will be true.
● NOT The inverse (opposite) of the entire expression.

IF statements can be nested, that is, placed within another IF statement.


This is used in situations when the expression is more complex than the
simple decisions.
Decision Making

Above statement can be represented like this.........

....... whereby more possibilities can be represented.


Nested decision making

For good practice...........

....... ensure that statements are properly indented to indicate block of statements
which belong together.
Repetition Control Structure
The repetition control structure specifies a block of one or more statements that are
repeatedly executed until a condition is satisfied.

Syntax is:

Looping constructs (also known as repetition or iteration constructs) are a kind of


construct found in pseudocodes which allows statements (or a group of statements)
to be repeated. The main reason why looping constructs are provided is because
most of the problems which we encounter everyday requires some degree of
repetition.
Looping Constructs
The looping constructs available in pseudocodes are as follows:

The format of the DOWHILE...ENDDO

construct is shown below:


Looping Constructs

The format of the FOR...NEXT construct is shown below:

ENDFOR
Looping Constructs

The format of the REPEAT...UNTIL construct is shown below:


Looping Constructs
Take a look at the following example:

You are required to develop a complete system which will allow the
total payroll to be calculated.

The system is required to read in the amount to be paid for each


employee.

The moment the system receives an input value of -99, the system
is required to stop and display the total payroll.
Looping Constructs
Looping Constructs

Example 1: An algorithm segment to print out each character typed at


a keyboard until the character ‘q’ is entered.

WHILE letter <> ‘q’

READ letter

DISPLAY “The character you typed is”, letter

ENDWHILE
Looping Constructs

Example 2: Write an algorithm that will output the square of any number input until the number input is zero.

DISPLAY “Type in a number or zero to stop”

READ number

WHILE number <> 0

Square = number * number

DISPLAY “The square of the number is”, square

DISPLAY “Type in a number or zero to stop”

READ number

ENDWHILE
Looping Constructs

FOR LOOP - the second type of iteration, which we shall use


when the number of iterations is known in advance, is a
for loop. This, in its simplest form, uses an initialisation of
the variable as a starting point, a stop condition depending
on the value of the variable. The variable is incremented on
each iteration until it reaches the required value.
Looping Constructs
Example: Design an algorithm for finding the sum of n numbers.

Sum = 0

Display “Input value n”

READ n

FOR (I = 1, I < n , I = I + 1)

READ value

Sum = Sum + value

ENDFOR

Output sum
Looping Constructs

REPEAT...UNTIL - it runs the statements inside loop until we get the condition true. So while it is false, the
repetition will be continued.

Examle: Write an algorithm which displays all the positive even integer numbers until 100.

even = 2

REPEAT

DISPLAY even

even = even + 2

OUTPUT even

UNTIL (even >= 100)


Program Flowcharts

As humans are more inclined towards understanding diagrams and pictures rather than words,
pseudocodes tends to become tedious to understand if too lengthy. Program flowcharts,
because they are represented graphically, makes understanding easier.

❖ Another technique used in designing and representing algorithms.


❖ Alternative to pseudocoding
❖ A pseudocode description is verbal, a flowchart is graphical in nature.

Definition:

A flowchart is a graph consisting of geometrical shapes that are connected by flow lines.
The following are the commonly used symbols for drawing program flowcharts.
Program Flowcharts (example)
Program Flowcharts (example)
Sequence Structure
Selection Structure 1
Selection Structure 2
Repetition Structure
Program Flowcharts (exercise 1)

Convert Temperature from Fahrenheit (℉) to Celsius (℃)

Flowchart: t
Program Flowcharts (exercise 2)
Determine Whether A Student Passed the Exam or Not: Flowchart:
Program Flowcharts (exercise 2)

Print numbers from 1 to 20.

Flowchart:
Summary of Main Teaching Points

★ An algorithm is a sequence of a finite number of steps arranged in a


specific logical order that, when executed, produce the solution for a
problem.
★ A pseudocode language is a semiformal,English-like language with a
limited vocabulary that can be used to design and describe
algorithms.
★ Any algorithm can be described in terms of three basic control
structures.They are the sequence,selection and repetition structures.
★ A Flowchart is a graphical representation of an algorithm.
Thanks for your attention!

NEXT CLASS:
Chapter 2: Introduction to Java
Brief history.
Introduction to execution of java programs.
Java compiler.
JIT, JVM, JRE, JDK.
Javac vs java commands.
.java vs .class extensions.

You might also like