0% found this document useful (0 votes)
48 views34 pages

Week 0 - Lectures

The document discusses algorithm representation using pseudocode. It describes pseudocode as a mix of natural language, programming language instructions, and keywords to define basic structures. It then outlines the common operations and control structures used in pseudocode, including assignments, comparison/logic/arithmetic operators, selection structures like if/else statements, and iteration structures like while and for loops. Finally, it provides an example of using procedures to modularize algorithms and examples of pseudocode for various algorithms and calculations.

Uploaded by

vaibhav.1991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views34 pages

Week 0 - Lectures

The document discusses algorithm representation using pseudocode. It describes pseudocode as a mix of natural language, programming language instructions, and keywords to define basic structures. It then outlines the common operations and control structures used in pseudocode, including assignments, comparison/logic/arithmetic operators, selection structures like if/else statements, and iteration structures like while and for loops. Finally, it provides an example of using procedures to modularize algorithms and examples of pseudocode for various algorithms and calculations.

Uploaded by

vaibhav.1991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

0 .

1 BINARY NUMERATION SYSTEM


Jean-Pierre Deschamps
University Rovira i Virgili, Tarragona, Spain

0 .1
1. Computer information representation
A computer receives, stores, processes, transmits data.
Data types: numbers, characters, sounds (audio), pictures
(video), etc.
Data encoding: strings of zeroes and ones.

Computer technology is based on electronic circuits able to process vectors of 0s and 1s (the
so-called digital electronic circuits). For that reason all data are encoded by strings of 0s and 1s.
This type of information encoding is called binary encoding system.
2

0 .1
2. Numeration systems
Most used systems:

Decimal system
Binary system
Hexadecimal system
Conversion methods will be presented..

0 .1
2.1 Decimal system

Uses ten digits:


0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Positional system : a weight is associated to every digit position so that position is


relevant.
Example:

653
(weights)

102

101

100

653 = 6102 + 5101 + 3100


6 hundreds, 5 tens, 3 units
4

0 .1
2.2 Binary system

Uses two digits (binary digits, bits): 0, 1

Positional system.
Example:
(weights)

23

22

21

20

To compute the decimal representation, add up the weights corresponding to the


1s of the binary representation:
(1101)2 = 123 + 122 + 021 + 120 = 8 + 4 + 0 + 1 = 13
5

0 .1
Exercise
Compute the decimal representation of the following binary number: (101001)2

0 .1
Exercise (solution)
Compute the decimal representation of the following binary number: (101001)2

weights

25

24

23

22

21

20

(101001)2 = 125 + 024 + 123 + 022 + 021 + 120 = 32 + 8 + 1 = 41

0 .1
2.2 Binary system: representation range

Pure binary system: non-negative number representation.


With n bits: 2n distinct values.
Representation range: 0 to 2n - 1.

EXAMPLE:
n = 4 bits
16 different combinations
from 0 to 15 = 24-1

Binary
0000
0001
0010
0011
0100
0101
0110
0111

Decimal
0
1
2
3
4
5
6
7

Binary
1000
1001
1010
1011
1100
1101
1110
1111

Decimal
8
9
10
11
12
13
14
15
8

0 .1
2.2 Binary system: representation range
If n = 3 : 23 = 8 representable numbers, from 0 to 7.
If n = 4 : 24 = 16 representable , from 0 to 15.
If n = 5 : 25 = 32 representable , from 0 to 31.
If n = 6 : 26 = 64 representable , from 0 to 63
Example: how many bits do we need to represent 48?
31 48 63
n=5
n=6
=> to represent decimal number 4810 we need 6 bits: 110000
9

0 .1
2.3 Hexadecimal system

Uses sixteen dgits:


0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Positional system

3
(weights)

163

A
162

9
161

F
160

To compute the decimal representation, add up the digits


multiplied by the corresponding weights:
(3A9F)16 = 3163 + 10162 + 9161 + 15160 = 1500710

BINARY

HEXA

0 .1
3. Base conversion (HEXADECIMAL to BINARY)
5. Cambios de base (BINARY to HEXADECIMAL)

HEXADECIMAL to BINARY: 1 hexadecimal digit 4 bits.


hexadecimal

binary

0011 1010 1001

BINARY to HEXADECIMAL: 4 bits 1 hexadecimal digit (starting from the four


rightmost bits)
binary
hexadecimal

100 1011 1010 0101


4

11

0 .1

Exercise
BINARY

HEXA

Compute the hexadecimal representation:


1101101010011002 = ????16

Compute the binary representation:


5F2C16 = ?????2

12

0 .1

Exercise (solution)
BINARY

HEXA

Compute the hexadecimal representation:


1101101010011002 = ?6D4C16

Compute the binary representation:


5F2C16 = ?01011111001011002

0101 1111 0010 1100

13

0 .1
4. Base conversion (DECIMAL to BINARY)
5

Divide the decimal number by 2. Divide the obtained quotient by 2. Keep dividing
the obtained quotients by 2 until the obtained quotient is equal to 1.
The base 2 number consists of the last quotient 1 and the set of previously obtained
remainders.
Example:

18(10 = ? 10010(2
(10

18 =
9=
4=
2=

2+

92 + 0
42 + 1
22 + 0
12 + 0

18(10 = 10010(2 10010(2


14

0 .1
Exercise
43(10 = binary number?

15

0 .1
Exercise (solution)
43(10 = binary number?
43 =
21 =
10 =
5=
2=

212 + 1
102 + 1
5 2 + 0
22+1
12+0

43(10 = 101011

16

0 .1
6. Sum and difference of binary numbers
Sum of 2 bits:
0+0=0
0+1=1
1+0=1
1 + 1 = 10 (current step bit: 0,
carry to the next step: 1)

Sum example:
1 1 1

Difference of 2 bits:
0-0=0
1-0=1
1-1=0
0 - 1 = 11 (current step bit: 1
borrow to the next step: 1)

Difference example:
carry

10100101 =A
+ 1010111 =B
11111100

10100101 =A
- 1010111 =B
1 0 1 1 1 1

borrow

01001110
17

0 .1
Exercise
10011011 =A
+
1010011 =B

10011001 =A
1010011 =B

18

0 .1
Exercise (solution)
0 0 1 0 0 1 1

carry

10011011 =A
+
1010011 =B
11101110

10011001 =A
1010011 =B
1

0 0 1 1

borrow

01000110

19

0 .1
SUMMARY
Computer information representation.
Numeration systems (decimal, binary, hexadecimal).
Pure binary system and representation range.
Base conversion.
Sum and difference of binary numbers.

20

0 .2

ALGORITHM REPRESENTATION IN
PSEUDOCODE
Jean-Pierre Deschamps
University Rovira i Virgili, Tarragona, Spain

0 .2
1. Algorithm representation: pseudocode
Algorithm
Sequence of operations whose objective is the solution of some problem such as:
complex computation, control of some process, etc.
The result of the algorithm execution must be independent of the chosen type of
representation.
Some common representation methods are: natural language, pseudocode, flow
diagrams and programming languages.
Pseudocode
Similar to programming language but more informal. It uses a mix of
natural language sentences,
programming language instructions,
key words that define basic structures.
22

0 .2
2. Operations and control structures
ASSIGNMENTS
OPERATORS
Comparison
Logic operations
Arithmetic operations
SELECTION STRUCTURES (DECISIONS)
Simple, Double, Multiple, Case
ITERATION STRUCTURES (CYCLES)
While, For
PROCEDURES
23

0 .2
2. 1 Assignments and types of operators
ASSIGNMENTS
Assignment instruction <= : allows to store a value within a variable. Examples:
x <= 15, x <= 2x + y + z, etc.
variable <= expression;

where x, y and z are variables.


Operator

COMPARISON OPERATORS

Meaning

<

Smaller than

>

Greater than

(2) Sometimes we can use >=

Equal to

(3) Sometimes we can use /=

(1)

Smaller than or equal to

(2)

Greater than or equal to

(3)

Different from

(1) Sometimes we can use <=

24

0 .2
2. 1 Assignments and types of operators
LOGIC OPERATORS
Operator
and

Meaning
Logic product

or

Logic sum

not

Negation

ARITHMETIC OPERATORS
Operator

Meaning

Sum

Difference

Product

Division

25

0 .2
3. Control structures
SELECTION STRUCTURES
Simple

condition

If condition then action(s);


end if;

no

yes
action;

Double
If condition then action1/s;
else action2/s;
end if;

no
action2;

condition

yes
action1;

26

0 .2
3. Control structures
SELECTION STRUCTURES
Multiple

yes

If condition1 then action1/s;


elsif condition2 then action2/s;
end if;
end if;

condition1

yes

action1;

no
condition2

no

action2;

Case
case x is
when value1" => action1;
when value2" => action2;
when value3" => action3;
when value4" => action4;
end case;

action1;

action2;

action3;

action4;

27

0 .2
3. Control structures (example 1)
y = X/2 , rounded down
Different cases:
When X is even, the result of the division is exact (whether X is positive or negative)
When X is odd, rounding must be calculated differently :
- if X is negative: (X+1)/2
- if X is positive (X-1)/2
If (X is even) then y<= X/2;
elsif (X<0) then y <= (X+1)/2;
else y <= (X-1)/2;
end if;
end if;

28

0 .2
3. Control structures (example 2)
X is a decimal digit: we calculate the binary representation

case x is
when 0" => y <= 0000;
when 1" => y <= 0001;
..
when 9" => y <= 1001;
end case;

29

0 .2
3. Control structures
ITERATION STRUCTURES
yes

While
While condition loop
action/s;
end loop;

condition

no

action;

For
for variable in min to max loop
action/s;
end loop;

30

0 .2
3. Control structures (example 3)
Given two vectors of 8 positions:
a(0), a(1), a(7)
x(0), x(1), x(7)
We want to do the following calculation:
y = a(0) x(0) + a(1) x(1) + a(2) x(2) + ..+ a(7) x(7)

acc <= 0;
for i in 0 to 7 loop
acc <= acc+ a(i) x(i);
end loop;
31

0 .2
4. Procedures
Procedure call name(parameters);
Procedure (subroutine): sequence of instructions (operations and control structures) that
execute some task (algorithm).
A name and a set of parameters are associated to every procedure.
A procedure can be called one or several times within a program. When called, values are given
to its parameters.
Algorithm

----------------call procedure

----------------call procedure

PROCEDURE
--------------------------------32

0 .2
4. Procedures (example 1)
z = a x + b y
Procedure square_root(x, y, n) is
------end procedure

Algorithm
call square_root (x, u, 16);
u <= a * u;
call square_root (y, r, 16);
r <= b * r;
z <= u + r;

ALGORITHM

Begin
call square_root (x,u,16);
u <= a * u;
call square_root (y,r,16);
r <= b * r;
z <= u + r;
end

PROCEDURE
square_root
(x,y,n)

----------------end procedure

33

0 .2
SUMMARY
Algorithm representation
Pseudocode
Operations and control structures
Procedures

34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy