You are on page 1of 2

EEX4465 – Data Structures and Algorithms 2020/2021

Tutor Marked Assignment — #2 Deadline on 31 May 2021

Important: For all Java program or data structure designs, you must
provide results with the source code and an adequate explanation of
the methods (using pseudocodes/flowcharts).

Question 1
A prime number is a natural number greater than 1 that is not a product of two smaller natural
numbers. The first 10 prime numbers are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29

(a) Write a pseudocode algorithm to find the first 100 prime numbers.
(b) Modify the above algorithm in (a) to obtain the prime numbers up to 100 and the count of
the numbers.
(c) Implement both algorithms in (a) and (b) using Java programming language.

Question 2
Evaluation of postfix notation is flexible to process in machines while humans are more familiar in
infix notation.

(a) Consider the infix notation (in algebraic) given by


A + (((B ∗ C) − ((D/(E ∧ F )) ∗ G)) ∗ H)

[i] Write a pseudocode algorithm to convert the above expression into postfix notation.
[ii] Show the contents of the stack at every step by creating a table with the column headers:
Next symbol Postfix string Stack
.. .. ..
. . .
[iii] Implement the algorithm in [i] by Java language and verify the results with the table
obtained in [ii].
(b) Consider the infix notation (in numerical) given by
7 + (9 − (2 + 5)) ∗ (8/4 + 1) ∧ 3

[i] Convert the above into its equivalent postfix notation (no need to show steps).
[ii] Evaluate the expression obtained in [i] by showing the contents of the stack at every
step by creating the table with the column headers:
Next symbol Operand 1 Operand 2 Value Stack
.. .. .. .. ..
. . . . .

1
Question 3
Queues and Stacks are linear lists which are the types of abstract data structures. They follow the
FIFO and LIFO behaviours, respectively.

(a) Explain the difference between a stack and a queue. Provide two real-life examples for each
data structure.
(b) Consider the first 10 prime numbers to answer the following tasks:
[i] Implement the following sub-tasks by using Java language:
I) Sequentially store the above 10 numbers in a queue using an array.
II) Display the queue after removing the first two numbers.
[ii] Now consider the queue obtained in [i]—II) to answer the following sub-tasks:
I) Write a pseudocode algorithm to remove the last two numbers without removing
the other six numbers.
II) Implement the proposed algorithm in [ii]—I).
III) Now, the queue has four empty positions. Write a pseudocode algorithm to insert
the next four prime numbers {31, 37, 41, 43} sequentially for the queue obtained
in [ii]—II).
IV) Implement the proposed algorithm in [ii]—III) by modifying the implementation
in [ii]—II).
Note: The maximum size of the queue always should be kept as 10 and you can modify the
structure of the queue appropriately. For each task, you should attach both Java script and
relevant results.

— End —

You might also like