chapter 3.4 - data representation, structure and manipulation (cambridge al 9691)

27
Chapter 3.4 – Data Representation, Structure and Manipulation Binary Numbers 1. Convert denary number 117 to the corresponding binary number using an 8-bit register. 2 117 2 58 1 2 29 0 2 14 1 2 7 0 2 3 1 1 1 Answer: 01110101 2. Convert binary number 10110110 to the corresponding denary number. Dena ry 128 64 32 16 8 4 2 1 Bina ry 1 0 1 1 0 1 1 0 Answer: 128 + 32 + 16 + 4 + 2 = 182 3. Convert denary number 117 to the corresponding octal number. 8 117 8 14 5 2 1 6 Answer: 165

Upload: lyceumfans

Post on 10-Apr-2015

421 views

Category:

Documents


7 download

DESCRIPTION

Chapter 3.4 – Data Representation, Structure and ManipulationBinary Numbers1. Convert denary number 117 to the corresponding binary number using an 8-bit register. 2 117 2 58 2 29 2 14 2 7 2 3 1 1 0 1 0 1 1Answer: 011101012. Convert binary number 10110110 to the corresponding denary number. Denary Binary 128 1 64 0 32 1 16 1 8 0 4 1 2 1 1 0Answer: 128 + 32 + 16 + 4 + 2 = 1823. Convert denary number 117 to the corresponding octal number. 8 117 8 14 2 1 Answer: 165 5 64. Convert the oc

TRANSCRIPT

Page 1: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

Chapter 3.4 – Data Representation, Structure and Manipulation

Binary Numbers

1. Convert denary number 117 to the corresponding binary number using an 8-bit register.

2 1172 58 12 29 02 14 12 7 02 3 1

1 1

Answer: 01110101

2. Convert binary number 10110110 to the corresponding denary number.

Denary 128 64 32 16 8 4 2 1Binary 1 0 1 1 0 1 1 0

Answer: 128 + 32 + 16 + 4 + 2 = 182

3. Convert denary number 117 to the corresponding octal number.

8 1178 14 52 1 6

Answer: 165

4. Convert the octal number 2036 to the corresponding denary number. Denary 2097152 262144 32768 4096 512 64 8 1Octal 0 0 0 0 2 0 3 6

Answer: (512 * 2) + (64 * 0) + (8 * 3) + (1 * 6) = 1024 + 0 + 24 + 6 = 1054

5. Convert the denary number 2877 to the corresponding hexadecimal number.

16 287716 179 13

Page 2: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

11 3

Answer: B3D

6. Convert the hexadecimal number 1E0A to the corresponding denary number.

Denary 268435456 16777216 1048576 65536 4096 256 16 1Hexadecimal 0 0 0 0 1 E 0 A

Answer: (4096 * 1) + (256 * E) + (16 * 0) + (1 * A) = 4096 + (256 * 14) + (16 * 0) + (1 * 10) = 7690

Binary Coded Decimal (BCD)

Decimal BCD0 00001 00012 00103 00114 01005 01016 01107 01118 10009 1001

7. Convert the denary number 438 to the corresponding BCD number.

Answer: 010000111000

8. Convert the BCD number 010100100111 to the corresponding denary number

Answer: 527

Two’s Complement

To convert a binary number to its two’s complement the following method is used:

1. The bits are written unchanged up to the first 1-bit.

2

Page 3: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

2. Beyond the first 1-bit the 0-bits are changed to 1-bits and 1-bits are changed to 0-bits.

9. Convert the binary number +119 to the corresponding two’s complement

Binary number equivalent of +119 is 01110111

Then write down the above number from right, up to and including the first digit that is a 1.

Binary Number 0 1 1 1 0 1 1 12’s Complement 1 0 0 0 1 0 0 1

Therefore the two’s complement of 01110111 is 10001001

10. Find the denary number represented by the 2’s complement 11011001.

2’s Complement 1 1 0 1 1 0 0 12’s Complement 0 0 1 0 0 1 1 1Positive denary 0 0 32 0 0 4 2 1

Therefore the positive denary number = 39Answer = -39

Addition of Binary Numbers

11. Add the binary numbers 01011011 and 00010010.

First Number 0 1 0 1 1 0 1 1Second Number 0 0 0 1 0 0 1 0Answer 0 1 1 0 1 1 0 1Remainder 1 1

Subtraction of Binary Numbers

12. Subtract the binary number 00010010 from the binary number 01011011.

First Number 0 1 0 1 1 0 1 1Second Number 0 0 0 1 0 0 1 02’s complement of the second number

1 1 1 0 1 1 1 0

Answer 0 1 0 0 1 0 0 1

Answer: 64 + 0 + 0 + 8 + 0 + 0 + 1 = 73 Decimal Fractions and their Binary Representations

Fraction Decimal Fraction Binary Fraction1/2 0.5 0.1

3

Page 4: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

1/4 0.25 0.011/8 0.125 0.0011/16 0.0625 0.00011/32 0.03125 0.000011/64 0.015625 0.0000011/128 0.0078125 0.00000011/256 0.00390625 0.000000011/512 0.001953125 0.0000000011/1024 0.0009765625 0.0000000001

Normalized Floating Point Representation

13. Using 8 bits for the mantissa and 8 bits for the exponent express 123 in normalized floating point representation

Binary representation of 123 in 8 bits = 01111011 Normalized floating point representation of 123 = 0.1111011 x 27

Answer = 01111011 00000111

14. Using 8 bits for the mantissa and 8 bits for the exponent express -123 in normalized floating point representation

Binary representation of 123 in 8 bits = 01111011 Two’s complement of 123 in 8 bits = 10000101 Normalized floating point representation of -123 = 1.0000101 x 27

Answer = 10000101 00000111

15. Using 8 bits for the mantissa and 8 bits for the exponent express 0.1875 in normalized floating point representation

0.1875 = 0.125 + 0.0625 Binary representation of 0.1875 = 0.0011 Normalized floating point representation of 0.1875 = 0.11 x 2-2

Binary representation of 2 in 8 bits = 00000010 Therefore the Two’s complement of -2 in 8 bits = 11111110 Answer = 01100000 11111110

16. Using 8 bits for the mantissa and 8 bits for the exponent express -0.1875 in normalized floating point representation

Binary representation of 0.1875 in 8 bits = 0.0011000 Two’s complement of 0.1875 in 8 bits = 1.1101000

4

Page 5: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

Normalized floating point representation of -0.1875 = 1.0100000 x 2-2

Binary representation of 2 in 8 bits = 00000010 Therefore the Two’s complement of -2 in 8 bits = 11111110 Answer = 10100000 11111110

17. Using 8 bits for the mantissa and 8 bits for the exponent express 123.1875 in normalized floating point representation

Binary representation of 123 = 01111011 Binary representation of 0.1875 = 0.0011 Binary representation of 123.1875 = 01111011.0011 = 0.111101100110000 x 27 = 0.1111011 x 27 = 0.1111011 x 200000111 Answer = 01111011 00000111

18. Using 8 bits for the mantissa and 8 bits for the exponent express -123.1875 in normalized floating point representation

Binary representation of 123.1875 = 01111011.0011 Two’s complement of 123.1875 = 10000100.1101 Normalized floating point binary = 1.00001001101 x 27 Answer = 10000100 00000111

How the range and the accuracy of a number vary according to the number of bits in the mantissa and the exponent?

When the number of bits in the mantissa increases the accuracy increases but the range decreases

Example: Suppose that a number is held in a 1-byte register. When the mantissa is 8 bits it can hold the number 4.2378244 and the exponent can hold the maximum of 01111111 Then the maximum number will be 4.2378244 x 2127

When the mantissa is 10 bits it can hold the number 4.237824458 and the exponent can hold the maximum of 011111 Then the maximum number will be 4.237824458 x 231

When the number of bits in the exponent increases the accuracy decreases but the range increases

19. Convert the binary floating point representation 01101000 00000110 to its denary equivalent.

Binary floating point 01101000 00000110 = 0.1101000 x 200000110 = 0.1101000 x 26

5

Page 6: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

= 0110100.0 Denary equivalent of 01101000 00000110 = 52

Why it is not possible to represent zero as a normalized floating point number?

A normalized number must have the first two bits of the mantissa different. Therefore one of those numbers must be a 1. Then the number will represent either -1 or 0.5 but not zero.

20. Represent the denary number 2 ½ in normalized floating point binary format using 5 bits for the mantissa and 3 bits for the exponent.

Binary representation of 2 ½ = 10.1 = 0.101 x 22 = 0.101 x 2010 Answer = 01010 010

21. Represent the denary number 4 ¾ in normalized floating point binary format using 5 bits for the mantissa and 3 bits for the exponent.

Binary representation of 4 ¾ = 100.11 = 0.10011 x 23 = 0.1001 x 2011 Answer = 01001 011

22. Represent the denary number -5/16 in normalized floating point binary format using 5 bits for the mantissa and 3 bits for the exponent.

5/16 = 1/4 + 1/16

Binary representation of 5/16 = 0.0101 Two’s complement of 5/16 = 1.1011 = 1.011 x 2-1 = 1.011 x 211 Answer = 10110 111

23. Represent the denary number -5 13/16 in normalized floating point binary format using 5 bits for the mantissa and 3 bits for the exponent.

13/16 = 1/2 + 1/4 + 1/16

6

Page 7: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

Binary representation of 13/16 = 0.1 + 0.01 + 0.0001 = 0.1101

Binary representation of 5 = 0101 Binary representation of 5 13/16 = 0101.1101 Two’a complement of 5 13/16 = 1010.0011

Floating point binary representation of -5 13/16 = 1.0100011 x 23 = 1.0100 x 2011 = 1.0100 x 2011 Answer = 10100 011

24. Using 5 bits for the mantissa and 3 bits for the exponent express -15/32 in normalized floating point representation

+15/32 = 1/4 + 1/8 + 1/16 + 1/32 Therefore binary representation of +15/32 in 8 bits = 0.01 + 0.001 + 0.0001 + 0.00001 = 0.01111 Two’s complement of 15/32 in 8 bits = 1.10001 Normalized floating point representation of 15/32 = 1.0001 x 2-1

= 1.0001 x 2111 Answer = 10001 111

25. Using 8 bits for the mantissa and 8 bits for the exponent express 10 1/2 in normalized floating point representation

Binary representation of 10 1/2 = 1010.1 Normalized floating point representation of 10 ½ = 0.10101 x 24 = 0.10101 x 2100

Answer = 01010100 00000100

26. Using 8 bits for the mantissa and 8 bits for the exponent express -10 1/2 in normalized floating point representation

Binary representation of 10 1/2 = 1010.1 Two’s complement of 10 1/2 = 10101.1 Normalized floating point representation of -10 ½ = 1.01011 x 24 = 1.01011 x 2100

Answer = 10101100 00000100

Number Description Floating Point Binary Representation

Denary value

Largest magnitude

Should have the largest positive mantissa and

01111111 01111111 (1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + 1/128) x 2127

7

Page 8: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

positive number

the largest positive exponent = 127/128 x 2127

Smallest magnitude positive number

Should have the smallest positive mantissa and the largest negative exponent.

01000000 10000000 1/ 2 x 2-128

Largest magnitude negative number

Should have the largest magnitude negative mantissa and the largest positive exponent occurs.

10000000 01111111 -1 x 2127

Smallest magnitude negative number

Should have the smallest magnitude negative mantissa and the largest negative exponent occurs.

10111111 10000000 -(0 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + 1/128) x 2-128

= - (1 – 63/128) x 2-128

= -65/128 x 2-128

Static data structures

This is a data structure that does not change in size while the program is running. An example of a static data structure

Dynamic data structure

8

Page 9: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

This is a data structure that can increase or decrease in size while a program is running. An example of a dynamic data structure is a linked list.

Data structure

Examples Advantages Disadvantages

Static Array 1. Easy to program 1. Programmer has to estimate the maximum amount of space that is going to be needed

2. An array allows for random Access

2. Can waste a lot of memory space

Dynamic Linked ListsStackQueueBinary tree

1. Only uses the memory space needed at that particular time

1. Difficult to program

2. Does not produce overflow 2. A linked list only allows serial access and does not allow random access

3. Makes efficient use of Memory 3. Can be slow to implement searches

4. It is easier to insert and delete elements from a linked list rather than an array because it involves only the deleting and creating of few pointers

5. Storage no longer required can be returned to the system for other uses

Algorithms involving Linked Lists

Algorithm 1: Insertion of a data item to a sorted linked list

Before the data was inserted

9

Page 10: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

After the data was inserted

IF free list is empty THEN OUTPUT error messageELSE store the new data in the node pointed to by the NEXTFREE pointer READ data from the linked list following the pointers IF new data = < current data IF current data <> first data change the pointer of the previous data to point to the new data change the pointer of the new data to point to the current data ELSEIF current data = first data set the pointer of the new data to point to the current data set START pointer to point to the new data ENDIF ELSEIF End Of List has reached and new data > = current data set the pointer of current data to new data set the pointer of new data to NULL ENDIF set NEXTFREE pointer to point to the next free locationENDIF

Algorithm 2: Deleting an item from a linked list

REPEATREAD data from the linked list by following the pointersIF current data = data to be deleted THEN

10

START Adam Bob David Edward

NEXTFREE Colin

START Adam Bob David Edward

NEXTFREE

LINKED LIST

FREE LIST

LINKED LIST

FREE LIST

Current dataPrevious data

New data

First data Next data

Page 11: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

set the pointer of the previous data to the next data add the current data to the free list and ENDIF

Algorithm 3: Searching for an item in a linked list

REPEATREAD data from the linked list by following the pointersIF current = data searched THEN DISPLAY message “A match is found” STOPENDIFUNTIL end of the listIF current data <> data searched THEN DISPLAY message “A match is not found” STOPENDIF

Search Algorithms

Serial search is carried out by continually comparing the desired key with each key in the list until a match is found or the end of the list is reached.

Binary search is carried out by continually comparing the desired key with the middle key of the list and hence repeatedly splitting the list into two equal halves until the desired key equals to the middle key or until the list is empty.

Serial search is useful if the list is not sorted and if the number of records in the list is small.

Binary search is faster if the list is sorted and if the number of records in the list is large

Algorithm 4: Searching for an item in a list using serial search

Serial search is carried out on a list of data by comparing each key with the one being searched, until a match is found or the end of the file is reached.

BEGINREPEAT

11

Page 12: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

READ the key of the current record IF the current key = desired key THEN DISPLAY message “Item found” STOP ENDIFUNTIL end of listIF current key <> the desired key THEN DISPLAY message “Item not found” STOPENDIFEND

Algorithm 5: Searching for an item in a sorted list using binary search

BEGINREPEAT READ the key of the middle record of the list IF the key of the middle record = desired key THEN DISPLAY message “Item found” STOP ELSE Split the list into two halves IF the key of the middle record < desired key THEN Consider the second half of the list ELSEIF Consider the first half of the list ENDIF ENDIFUNTIL the number of items in the list = 1IF the key of the record left = desired key THEN DISPLAY message “Item found” STOP ELSE DISPLAY message “Item not found”ENDIF

StackStack is a LIFO (last-in first-out) data structure.An item is both pushed-on and popped-off from the top of the stack.

QueueQueue is a FIFO (first-in first-out) data structure.

12

Page 13: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

An item is inserted to the queue from the rear and deleted from the front.

Algorithms involving Stacks and Queues

A problem that can occur when a program tries to insert a new value to a stack or a queue which is represented as an array is the situation where the array is full. The stack pointer helps to recognize this situation by pointing to the outside of the array.

A problem that can occur when a program tries to read a value from a stack or a queue which is represented as an array is the situation where the array is empty. The stack pointer helps to recognize this situation by pointing to the null value.

Algorithm 6: Insertion of an item to a stack (when the stack is represented by an array)

IF stack is full DISPLAY message “Stack is full.” STOPELSE Increment stack pointer by 1 Store the new data at the location pointed to by the stack pointer STOPENDIF

Algorithm 7: Deletion of an item from a stack (when the stack is represented by an array)

13

Stack Pointer (points to the top of the stack)

Memory addresses increase in this direction

Data

Data

Data

Data

Page 14: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

IF stack is empty DISPLAY message “Stack is empty” STOPELSE Set data in the cell pointed to by the stack pointer to the variable item_deleted Decrement the stack pointer by 1 STOPENDIF

Algorithm 8: Insertion of an item to a queue (when the queue is represented by an array)

If the Head pointer points to the front of the queue and the tail pointer points to the back of the queue:

IF queue is full DISPLAY message “Queue is full.” STOPELSE

14

Tail Pointer (points to the back of the queue)

Head Pointer (points to the front of the queue)

Memory addresses increase in this direction

Data

Data

Data

Data

Stack Pointer (points to the top of the stack)

Memory addresses increase in this direction

Data

Data

Data

Data

Page 15: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

Increment tail pointer by 1 Store the new data at the location pointed to by the tail pointer STOPENDIF

Algorithm 9: Deletion of an item from a queue (when the queue is represented by an array)

If the Head pointer points to the front of the queue and the tail pointer points to the back of the queue:

IF queue is empty DISPLAY message “Queue is empty” STOPELSE Set data in the cell pointed to by the head pointer to the variable item_deleted Increment the head pointer by 1 STOPENDIF

Algorithms involving Binary Trees

Algorithm 10: Inserting an item (X) to a binary tree

BEGINREPEAT READ X READ root node WHILE node NOT EMPTY DO IF X < value at node Follow left sub-tree ELSE Follow right sub-tree ENDIF ENDWHILE INSERT X at last read nodeUNTIL end of list

15

Tail Pointer (points to the back of the queue)

Head Pointer (points to the front of the queue)

Memory addresses increase in this direction

Data

Data

Data

Data

Page 16: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

END

Example

Write the following data to a binary tree

Michael, Jack, Victor, John, Sam, Nathan, Edward, Harry, Lisa, Xavier, Mark, Pam, Ivan, Frank, Walter, Izack, Colin, Kevin, Edwin, Zev, Jessie, Irene, Gary, Miranda

Michael

Jack Victor

Edward John Sam Xavier

Colin Harry Jessie Lisa Nathan Walter Zev

Frank Ivan Kevin Mark Miranda Pam

Edwin Gary Irene Izack

Algorithm 11: Reading a binary tree data structure in alphabetic order

BEGINTraverse left sub-treeREAD the node Traverse right sub-treeREAD the nodeEND

16

Page 17: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

Example:

Read the following binary tree in alphabetical order

Michael (16)

Jack (10) Victor (21)

Edward (2) John (12) Sam (20) Xavier (23)

Colin (1) Harry (5) Jessie (11) Lisa (14) Nathan (18) Walter (22) Zev (24)

Frank (4) Ivan (8) Kevin (13) Mark (15) Miranda (17) Pam (19)

Edwin (3) Gary (6) Irene (7) Izack (9)

Algorithms involving Sorting of Lists

The three sorting algorithms are:

Bubble Sort algorithmInsertion Sort algorithmQuick Sort algorithm

Quick Sort algorithm is the fastest in sorting a list and Insertion Sort algorithm is faster than the Bubble Sort algorithm.

Algorithm 12: Sorting a list in ascending order using the bubble sort algorithm

Bubble sort algorithm sorts a given unsorted list of items in ascending order.

DOFOR pass IN RANGE OF 1 to (N – 1) DO FOR c IN RANGE OF 1 TO (N – pass) DO IF Data (c) > Data (c + 1) THEN SET Temp TO Data (c) SET Data (c) to Data (c + 1) SET Data (c + 1) TO Temp ENDIF

17

Page 18: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

ENDFORENDFOR

Example:

Explain how the bubble sort algorithm is used to sort the list 12, 34, 23, 5, 78, 10 in ascending order:

pass Sequence1 12 34 23 5 78 10

12 34 23 5 78 1012 23 34 5 78 1012 23 5 34 78 1012 34 5 34 78 10

2 12 34 5 23 10 7812 34 5 23 10 7812 5 34 23 10 7812 5 23 34 10 7812 5 23 10 34 78

3 12 5 23 10 34 785 12 23 10 34 785 12 23 10 34 785 12 10 23 34 785 12 10 23 34 78

4 5 12 10 23 34 785 12 10 23 34 785 10 12 23 34 785 10 12 23 34 785 10 12 23 34 78

Algorithm 13: Sorting a list in ascending order using the insertion sort algorithm

First value of the given unsorted list is written to the new listStarting from the second value in the given unsorted list write each value to the new list by inserting it to the correct place of the new list by moving appropriate values to the right to create spaceRepeat until the end of the given unsorted list

Algorithm 14: Sorting a list in ascending order using the quick sort algorithm

Split the list into two sublistsSet up two pointers (one at each end of the list) called left pointer and right pointerREPEAT Quick sort the list by comparing the two numbers pointed to by the pointers and swap if necessaryUNTIL the pointers coincideQuick sort each sublist until all the sublists contain just a single data in which case the entire list is sorted

18

Page 19: Chapter 3.4 - Data Representation, Structure and Manipulation (Cambridge AL 9691)

Algorithm 15: Merging of two sorted lists

Select one value from each list WHILE neither list is empty Compare the values in the lists

Copy the smaller value to the new listRead the next value in the list that has a value copied

ENDWHILEWhen one list is empty copy the remaining values of the other list to the new list

Explain how storing data in ascending order within an array would affect the management of storage and the search for a given data.

1. When a student leaves then all students higher in the alphabet have to be moved up one place in the array

2. When a new student arrives, the correct location in the array needs to be found and the remaining students moved down one place

3. A binary search may be used involving looking at the centre value in the remaining part of the array at each pass and then cutting number of items to be searched in half

4. Speeds up any searches that have to be made (because of reduced number of comparisons)

19