Sunteți pe pagina 1din 4

Assignment 2

Write Algorithms for the following tasks


1. Given a n×n matrix M of type float, replace M with its transpose without the use of a
temporary matrix.
2. Suppose you are designing a multi-player game that has n≥1000 players, numbered 1 to n,
interacting in an enchanted forest. The winner of this game is the first player who can meet
all the other players at least once (ties are allowed). Assuming that there is a function
meet(i, j), which is called each time a player i meets a player j (with i ≠ j), describe a way
to keep track of the pairs of meeting players and who is the winner.
3. Suppose an initially empty stack S has performed a total of 25 push operations, 12 top
operations, and 10 pop operations, 3 of which generated a StackEmpty exception that was
caught and ignored. What is the current size of S?
4. Describe the output of the following series of stack operations: push(5), push(3), pop(),
push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7), push(6), pop(), pop(),
push(4), pop(), pop().
5. Give a recursive function for removing all the elements in a stack.
6. Given an array of characters formed with a’s and b’s. The string is marked with special
character X which represents the middle of the list (for example: ababa...ababXbabab
baaa). Check whether the string is palindrome.
Can we solve using stacks?
7. How do we implement two stacks using only one array? Our stack routines should not
indicate an exception unless every slot in the array is used
8. Describe a way to use recursion to compute the sum of all the elements in a n×n (two-
dimensional) array of integers.
9. Describe a recursive algorithm for finding the maximum element in an array A of n
elements.
10. Write a short recursive function that repeatedly selects and removes a random entry from
an n-element array until the array holds no more entries. Assume that you have access to a
function random(k), which returns a random integer in the range from 0 to k.
11. Give a recursive algorithm to compute the product of two positive integers, m and n, using
only addition and subtraction.
12. Describe a recursive function for converting a string of digits into the integer it represents.
For example, "13531" represents the integer 13,531.
13. Write a short recursive function that finds the minimum values in an array of int values
without using any loops.
14. Write a short recursive function that takes a character string s and outputs its reverse. For
example, the reverse of "pots&pans" would be "snap&stop".
15. Write a short recursive function that determines if a string s is a palindrome, that is, it is
equal to its reverse. For example, "racecar” and "gohangasalamiimalasagnahog" are
palindromes.
Limitation of array implementation:

 the dimension of an array is determined the moment the array is created, and
cannot be changed later on;
 the array occupies an amount of memory that is proportional to its size,
independently of the number of elements that are actually of interest;
 if we want to keep the elements of the collection ordered, and insert a new
value in its correct position, or remove it, then, for each such operation we may
need to move many elements (on the average, half of the elements of the array);
this is very inefficient

Answer all questions

Part-A (10*2=20 marks)

1. Prove formally, using the definition of asymptotic notation, that if f(n)=n and
g(n)=n 2 ,

then f(n)= 0(g(n))

2. Indicate whether each of the following statements is true or false:

a. f(n) = 5n 2 + 3n + 2 is Ω(n) b. f(n) = 5n 2 + 3n + 2 is Θ (n)

3. Find a suitable data structure for the following questions and justify with a
statement,

a. A string contains letters and digits. A program removes digits from a string and

retains the string that contains letters only.

b. Given a number n, A function that generates and prints all binary numbers with

decimal values from 1 to n.

5. In the game of "twenty questions", your task is to guess the value of a secret
number that

is one of the n integers between 0 and n−1. For simplicity, we will assume that n is
a

power of 2 and that the questions are of the form "is the number greater than or
equal to
x?” What effective search strategy you will follow? Why?

7. Trace the following infix expressions into their equivalent postfix form.

a. (A+B ^ D)/ (E-F)+G b. A* (B+D) /E –F *(G+H/K)

8. A major problem in the normal Queue data structure is that even though we
have empty

positions in the queue we cannot make use of them to insert new element Is there
any way to overcome this problem ? Define.

9. Suppose the array A contains [7, 1, 6, 4, 9]. Do the quicksort algorithm where
the first

element in the array is used as pivot. What is the strategy does this algorithm
follow?

10. Show how the singly linked lists remove method works when the item to be
removed is

in the last node.

Part-B (3*10=30 marks)

1. Consider the following function that computes the sum of the elements of an
array

iteratively:

int arraySum (int a[])

int sum = 0;

n = a.size();

for (int i = 1; i < n; i++)

sum += a[i];

return sum; }
Write a recursive function that performs the same task. Depict the Series of
Recursive

Returns for the array a[1,3,5,7,9]

2. a. A common way to evaluate an expression in postfix notation is to first convert


it to an

infix expression and then evaluate the infix expression – True/False? Justify

b. Evaluate the following postfix expressions. Show the stack as each operand and

operator is processed.

823^/23*+51*-

3. Write the algorithm specification of array Implementation for Queue Data


Structure?

S-ar putea să vă placă și