CLang Lect01
CLang Lect01
Introduction
Department of Information System
SoICT, HUT
1
eureka!
Algorithm
A set of instructions specifying
the steps required to
accomplish a task
Program
2
What a computer can do?
Not much… Computers understand only numbers!
• Store and retrieve numbers (fast and accurate).
• Add, subtract, multiply and divide numbers (also,
fast and accurate).
• Compare numbers.
• Follow a list of instructions and jump around in the
list
3
What else a computer can do?
• More complex calculations can be implemented
from a set of simple calculations
• Communicate with peripheral devices to
input/output data
• Input: mouse, keyboards, joystick.
• Output: graphic cards, printers
• Everything is doable with numbers
4
Von Neumann Architecture
5
What is a computer program?
• A sequence of instructions aims at solving a specific
task
• Instruction is carried out one after the other. No
instruction is carried out when the previous
instruction is not accomplished
• A program is represented by a programming
language.
6
Programming languages
• Machine language is dependent to the computer
using machine instructions. Executable programs
must be in machine language
• High level language is independent to the computer
using human algorithm instructions
7
Machine language
• is a language understandable by computer
• In our view, machine language is only a sequence of
0 and 1.
• There is no common machine language for
computer
• Each micro-processor has its own language
• Human cannot work directly with machine language
• However, computer cannot understand other
languages
8
High level language
• Assembly – machine language
encoded as documents (not
convenient)
• Interpretation language (java, perl)
• A program is translated into
machine language during its
process
• Compile language (C, pascal)
• A program is compiled into
machine language once before
process
9
The problem solving process
Rice cooking
Problem Wash rice (0,5kg)
Pour water (1liter) to a casserole
Analyze Boil the water
Describe the Put rice into the casserole
problem Turn down heat
Design Wait 15minutes, take the casserole
out
washrice(0,5);
Algorithm
pourwater (1);
Implement boilwater();
putintocasserole();
turndownheat();
Program takecasseroleout();
Translate
01001110101100101010101010010
10101010100110010101010101001
011010011101010101010010010111
Executed 010011110101010111110101010001
program 10100001101...
10
Algorithm
11
Rice cooking algorithm
• Prepare Input
• 0,5 kg rice, 1 liter of water
• Steps:
Processing
• Wash rice (0,5 kg)
• Pour (1 liter) water to a casserole
• Boil the water
• Put rice into the casserole
• Wait until the water is shallow
• Turn down heat Output
• Wait 15minutes, take the casserole out
• Result:
• A casserole that contains rice for 5 people
12
Components of an algorithm
• Variables and values
• Instructions
• Sequences
• Selections
• Iterations
• Procedures
13
Values
14
Variables
• Containers or places to store values
• Example
Variable Values
10 candies
This container
can be used to 50 g sugar
store
3 cakes
etc.
15
Type of variables
• Restricted to contain a specific type of value, e.g.,
only integer number.
• Example : kg (rice) or liter (water)
16
Instruction
• Instructions should be:
• simple
• unambiguous
• the system knows the instruction in order to implement it
17
Guide about instructions
• Instructions should be simple and unambiguous
• For example:
19
Selection
• Is an instruction that decides which of two possible
sequences is executed
• It is based on a condition (true/false)
if …
then …
else …
20
Example about rational number
input an integer input N
if (N 0)
selection condition
then
{
output 1/N
Carry out when the }
condition is true
else
{
output “infinitive"
Carry out when the
condition is false }
21
Question ?
Do these two algorithms give the same output?
Algorithm 1 Algorithm 2
input N input N
if (N 0)
if (N 0)
then
{ then
output 1/N {
} output 1/N
else }
{
output “infinitive"
} output “infinitive"
22
Iteration
• Repeat an instruction (or a group of instructions)
while (or maybe until) some true or false condition
occurs.
• Two kinds of iteration:
• Test the condition each time before repeating the
instruction
• Test the condition each time after executing the
instruction
23
Example
Print the odd numbers from 1 to 100
24
Question ?
Do these two algorithms give the same output?
Algorithm 1 Algorithm 2
num = 1 num = 1
while (num <= 100) while (num <= 100)
do do
{ {
output num num = num + 2
num = num + 2 output num
} }
25
Example: Sum of a set of integer values
26
Procedure
• Is a series of instructions with a name
• You can
• refer to it (by name)
• Procedure is used in structured programming to divide
a program into smaller parts with different names
• Procedure
• Function
• Sub-routine
27
Example
Procedure RiceCooking Procedure DinerPreparing
{ {
Wash rice (0,5 kg) RiceCooking
Pour (1 little) water to a Boiling vegetablesCalling procedure
casserole Frying meat
Boil the water
Setting the table
Put rice into the casserole
}
Wait until the water is shallow
Turn down heat
Wait 15minutes, take the
casserole out
} Declaring procedure
28
Exercises
1. Write an algorithm to solve the following equation:
a*x + b = c.
2. Write an algorithm to:
• Input values for 3 variables a,b,c
• Print out the variable that has the highest value and the
variable that has the lowest value. Print out the value for
these two variables.
3. Write an algorithm to:
• Input a value for a variable n >=1
• Find all numbers <=n that satisfy the following condition
• Divide 3 remain 2 and divide 5 remain 3
29
Summary
• The problem solving process
• Problem → Algorithm → Program
• Programming language
• High level language vs. machine language
• Components of an algorithm
• Variables and values
• Instructions:
• Sequences, selections, iterations, procedures
30
Thank you
for your
attentions!