EE441 Data Structures Example Questions - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

EE441 Data Structures Example Questions

Description:

Recall the implementation of Push method of Stack class. void Stack::Push(const DataType& item) ... Stack int SA, SB, SC; int prev, current; SA.Push(1); for ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 23
Provided by: ozgurba
Category:

less

Transcript and Presenter's Notes

Title: EE441 Data Structures Example Questions


1
EE441 Data StructuresExample Questions
  • Özgür B. Akan
  • Department of Electrical Electronics
    Engineering
  • Middle East Technical University
  • akan_at_eee.metu.edu.tr
  • www.eee.metu.edu.tr/akan

2
Example Questions
  • Q1) Consider the following C program section
  • int S(int a, int b)
  • int t
  • ta
  • ab2
  • bt2
  • return t
  • void main(void)
  • int p
  • int q, A4
  • for (int i0 ilt4 i)
  • Aii2
  • pA2
  • // control point 1
  • qS(p,A0)
  • // control point 2
  • S(A0,p)
  • // control point 3
  • Show the contents of p, q and the array A at
  • Control point 1
  • Control point 2
  • Control point 3

3
Example Questions
  • A1)
  • int S(int a, int b)
  • int t
  • ta
  • ab2
  • bt2
  • return t
  • void main(void)
  • int p
  • int q, A4
  • for (int i0 ilt4 i)
  • Aii2
  • pA2
  • // control point 1
  • qS(p,A0)
  • // control point 2
  • S(A0,p)
  • // control point 3
  • At Control point 1
  • At Control point 2
  • S(4 by value, 0) t4 local a020 A0?8

4
Example Questions
  • A1)
  • int S(int a, int b)
  • int t
  • ta
  • ab2
  • bt2
  • return t
  • void main(void)
  • int p
  • int q, A4
  • for (int i0 ilt4 i)
  • Aii2
  • pA2
  • // control point 1
  • qS(p,A0)
  • // control point 2
  • S(A0,p)
  • // control point 3

(c) At Control point 3 t8 local
a428 // note that the 2nd parameter // of
function S is passed // by reference,
thus A2?8216
5
Example Questions
  • Q2)
  • (a) What is the order (time complexity in O(.)
    notation) of the following tasks in the worst
    case?
  • i. computing the sum of the first n even
    integers by using a for loop
  • ii. pushing an item to a stack of n items
  • (b) Order the following functions by growth rate
    nlog n, nloglog n, log2 n, nlog n2, 2n/2, 2n, 37,
    n2log n, 2000. Indicate the group of functions
    which grow at the same rate.
  • (c) Suppose that the implementation of a
    particular algorithm appears in C as follows
  • for (int i1 igtn i)
  • for (int j1 jgti j)
  • for (int k1 kgt10 k)
  • myfunction(i,j,k,n)
  • What is the time complexity of the algorithm,
    assuming that myfunction has a running time,
    which is O(log n)? Justify your answer.

6
Example Questions
  • A2)
  • (a) i. computing the sum of the first n even
    integers by using a for loop
  • for (i1iltni) sumsumi2
  • the work (time it takes) grows linearly with
    n, hence O(n)
  • ii. pushing an item to a stack of n items
  • Recall the implementation of Push method of
    Stack class
  • void StackPush(const DataType item)
  • if (topMaxStackSize-1)
  • cerrltlt"Stack overflow"ltltendl exit(1)
  • top
  • stacklisttop item
  • work done in pushing an item to a stack is
    independent of stack sizeHence, O(1)

7
Example Questions
  • A2)
  • (b) 37, 2000
  • log2 n
  • nloglog n
  • nlog n, nlog n2
  • n2log n
  • 2n/2, 2n
  • (c) If myfunction requires t time units, the
    innermost loop on k requires 10t time units. The
    loop of j requires 10ti time units and the
    outermost loop on i requires

for (int i1 igtn i) for (int j1 jgti
j) for (int k1 kgt10 k) myfunction(i,
j,k,n)
Since t is O(log n) then the overall execution
time is O(n2log n)
8
Example Questions
  • Q3) Determine, showing your arguments briefly,
    the O( ) complexities of
  • (a) The function (n3 7) / (n 1)
  • (b) The running time of the following C code,
    as a function of n
  • for (h1 hltn h)
  • for (i1 iltn i)
  • for (j1 jltii j)
  • count
  • (Note that 12 22 32 ... n2 n (n 1)
    (2n 1) / 6 )
  • (c) The worst case running time of the following
    algorithm, described informally, as a function of
    n
  • Search a sorted array of n elements for a
    given key
  • Start with size ? full table size.
  • Partition the table to be searched into three.
  • If the table is too small for this (i.e., if size
    lt 3) simply check its contents and exit either
    with success or with failure.
  • If key lt the last element of the lowest
    partition, from now on, consider only this lowest
    partition and go back to step 2. (In steps 3 and
    4, if the key is found at the comparison
    operation, exit the algorithm with success.)
  • If key lt the last element of the middle
    partition, from now on, consider only this middle
    partition and go back to step 2.

9
Example Questions
  • A3)
  • (a) (n3 7) / (n 1) n2-n16/(n1) ? O(n2)
  • (b) The running time of the following C code,
    as a function of n
  • for (h1 hltn h)
  • for (i1 iltn i)
  • for (j1 jltii j)
  • count
  • execution timenn (n 1) (2n 1) / 6 ) ?
    O(n4)
  • (c) n items can be partitioned into 3, at most
    ?log3n? times.. ? O(logn)

10
Example Questions
Q4) Given the following C program
void myfunction(int n) Stackltintgt SA, SB,
SC int prev, current SA.Push(1) for
(int i2 iltn i) coutltendl coutltlti
ltltiltlt previ SB.Push(prev) coutltltSB.Peek
ltlt, while !(SA.StackEmpty())
currentprev SA.Pop SB.Push(current)
coutltltSB.Peekltlt, prevcurrent
while !(SB.StackEmpty()) SC.Push(SB.Pop)
while !(SC.StackEmpty()) SA.Push(SC.Pop)
  • include ltiostream.hgt
  • template ltclass Tgt
  • Class Stack
  • private
  • T stacklistMaxStackSize
  • int top
  • public
  • Stack(void)
  • void Push(const T item)
  • T Pop(void)
  • void Clearstack(void)
  • T Peek(void) const
  • int StackEmpty(void) const
  • int StackFull(void) const
  • What will be the output if myfunction is called
    with n6, i.e., myfunction(6) ?
  • What is the complexity of the algorithm in O(.)
    notation in terms of n?

11
Example Questions
A4)
void myfunction(int n) Stackltintgt SA, SB,
SC int prev, current SA.Push(1) for
(int i2 iltn i) coutltendl coutltlti
ltltiltlt previ SB.Push(prev) coutltltSB.Peek
ltlt, while !(SA.StackEmpty())
currentprev SA.Pop SB.Push(current)
coutltltSB.Peekltlt, prevcurrent
while !(SB.StackEmpty()) SC.Push(SB.Pop)
while !(SC.StackEmpty()) SA.Push(SC.Pop)

(a) myfunction(6)
i22,3, i33,6,8, i44,12,18,21, i55,26,4
4,56,60 i66,66,122,166,192,197
(b) O(nn)
12
Example Questions
  • Q5) Give the final values of X, Y and A after the
    following C statements are executed
  • void main()
  • int X4, Y7, PXX, PY
  • float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
    PAA0
  • PYY
  • (PX)--
  • PYPX
  • PYPX
  • PY55
  • PA3.0
  • PA
  • PA6.8
  • PA2
  • PA3.3

13
Example Questions
  • A5)
  • void main()
  • int X4, Y7, PXX, PY
  • float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
    PAA0
  • PYY
  • (PX)--
  • PYPX
  • PYPX
  • PY55
  • PA3.0
  • PA
  • PA6.8
  • PA2
  • PA3.3

PA
2.3
4.5
8.9
1.0
5.5
3.5
4
PX
X
7
PY
Y
14
Example Questions
  • A5)
  • void main()
  • int X4, Y7, PXX, PY
  • float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
    PAA0
  • PYY
  • (PX)--
  • PYPX
  • PYPX
  • PY55
  • PA3.0
  • PA
  • PA6.8
  • PA2
  • PA3.3

PA
2.3
4.5
8.9
1.0
5.5
3.5
3
PX
X
10
PY
Y
15
Example Questions
  • A5)
  • void main()
  • int X4, Y7, PXX, PY
  • float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
    PAA0
  • PYY
  • (PX)--
  • PYPX
  • PYPX
  • PY55
  • PA3.0
  • PA
  • PA6.8
  • PA2
  • PA3.3

PA
5.3
4.5
8.9
1.0
5.5
3.5
55
PX
X
10
PY
Y
16
Example Questions
  • A5)
  • void main()
  • int X4, Y7, PXX, PY
  • float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
    PAA0
  • PYY
  • (PX)--
  • PYPX
  • PYPX
  • PY55
  • PA3.0
  • PA
  • PA6.8
  • PA2
  • PA3.3

5.3
4.5
PA
8.9
1.0
5.5
3.5
55
PX
X
10
PY
Y
17
Example Questions
  • A5)
  • void main()
  • int X4, Y7, PXX, PY
  • float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
    PAA0
  • PYY
  • (PX)--
  • PYPX
  • PYPX
  • PY55
  • PA3.0
  • PA
  • PA6.8
  • PA2
  • PA3.3

5.3
6.8
8.9
PA
1.0
5.5
3.5
55
PX
X
10
PY
Y
18
Example Questions
  • A5)
  • void main()
  • int X4, Y7, PXX, PY
  • float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
    PAA0
  • PYY
  • (PX)--
  • PYPX
  • PYPX
  • PY55
  • PA3.0
  • PA
  • PA6.8
  • PA2
  • PA3.3

5.3
6.8
8.9
1.0
PA
5.5
3.5
55
PX
X
10
PY
Y
19
Example Questions
  • A5)
  • void main()
  • int X4, Y7, PXX, PY
  • float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
    PAA0
  • PYY
  • (PX)--
  • PYPX
  • PYPX
  • PY55
  • PA3.0
  • PA
  • PA6.8
  • PA2
  • PA3.3

5.3
6.8
8.9
1.0
5.5
PA
3.3
55
PX
X
10
PY
Y
20
Example Questions
  • Q6) Given the partial class definition

class MyClass private float a50
public MyClass(void) ??? Fun(???
J) // single input argument
Implement the member function Fun which will
allow the user to insert floating numbers to the
desired location in the floating array a. Array
indices greater than 49 will be inserted in the
last location and negative indices will be
inserted in the 1st location in the array. Any
index between 0-49 will be inserted in the
corresponding location in the array. This member
function will take only a SINGLE input argument.
Comment on each line of your code
21
Example Questions
  • A6) In this question, your function should be
    able to put a floating number x in the array
    location y
  • float MyClassFun(int j)
  • int index
  • indexj
  • if (jlt0)
  • index0
  • if (jgt49)
  • index49
  • return aindex
  • An example use of this function in a main program
    is
  • MyClass A
  • int x10
  • float y 1.234
  • A.Fun(x) y

22
Example Questions
  • Q7) Given the main program and function Fun

void Fun(int a, int b) aa5
ba-2 main () int a50, b50 int
p, q pa3 qb5 Fun(p,q) //
Line (X)
a) What are the contents of p and q after the
execution of line (X)? b) Propose a way how Fun
should be modified in order to make p point to
the first element in a and q point to the first
element in b. Also indicate how Fun should be
called in line(X)
23
Example Questions
  • A7)

void Fun(int a, int b) aa5
ba-2 main () int a50, b50 int
p, q pa3 qb5 Fun(p,q) //
Line (X)
a) Since the parameters are passed by value, the
contents of p and q are unchanged.
24
Example Questions
  • A7)

b) Propose a way how Fun should be modified in
order to make p point to the first element in a
and q point to the first element in b. Also
indicate how Fun should be called in line(X)
void Fun(int a, int b) aa-3
bb-5 main () int a50, b50 int
p, q pa3 qb5 Fun(p,q) //
Line (X)
void Fun(int a, int b) aa-3
bb-5 main () int a50, b50
int p, q pa3 qb5 Fun(p,q)
// Line (X)
25
Example Questions
  • Q8) (a) For the following declaration of the
    Stack class, assuming that the public methods
    Stack, Push, Pop, StackEmpty and StackFull are
    implemented as discussed in class, implement the
    C member function Count that will return the
    number of items in the owner object. Your member
    function should access the data structure through
    the existing member functions.

template ltclass Tgt class Stack private T
stacklistMaxstacksize int
top public Stack(void) void Push(const T
item) T Pop(void) int StackEmpty(void)
const int StackFull(void) const int
Count(void) const
26
Example Questions
  • Q8) (b) Using the Stack class definition in part
    (a), implement a global function
  • template ltclass Tgt
  • int Bottom (StackltTgt s, T last)
  • that returns 0 if stack s is empty and 1 if it
    is non-empty, assigning the bottom element to the
    argument last. Upon return from your function the
    original stack should remain unchanged. Your
    function implementation should be properly
    commented for understandability.

27
Example Questions
  • Q8) (c) Still using the Stack class definition in
    part (a), what is the output from the following
    sequence of operations?
  • Stackltintgt S
  • int x3, y5, z7
  • s.Push(8)
  • s.Push(x)
  • s.Push(y)
  • s.Push(z)
  • xs.Pop()
  • s.Pop()
  • s.Push(4)
  • coutltlt x ltlt endl // output1 .
  • coutltlt s.Count()ltltendl // output2 .
  • ys.Pop()
  • zs.Pop()
  • coutltlt y ltlt endl // output3 .
  • While (!s.StackEmpty())
  • xs.Pop()
  • coutltltxltltendl // output4 .

28
Example Questions
  • A8) (a) (with the ability of member function to
    access private data members)
  • template ltclass Tgt
  • int StackltTgtCount(void) const
  • return (top1)
  • (b) template ltclass Tgt
  • int Bottom(StackltTgt s, T last)
  • StackltTgt temp_s // temp_s will store the
    original stack in reverse order
  • // first reverse original stack into temp_s
  • if (s.StackEmpty) return 0
  • else
  • while(!s.StackEmpty())
  • temp_s.Push(s.Pop())
  • // now, bottom of original is top of temp_s
  • lasttemp_s.Pop() // this is the wanted
    item s.Push(last) // restore bottom
  • while (!temp_s.StackEmpty()) // restore the
    rest
  • s.Push(temp_s.Pop())
  • return 1

29
Example Questions
  • A8) (c)
  • Stackltintgt S
  • int x3, y5, z7
  • s.Push(8)
  • s.Push(x)
  • s.Push(y)
  • s.Push(z)
  • xs.Pop()
  • s.Pop()
  • s.Push(4)
  • coutltlt x ltlt endl // output1 7
  • coutltlt s.Count()ltltendl // output2 3
  • ys.Pop()
  • zs.Pop()
  • coutltlt y ltlt endl // output3 4
  • While (!s.StackEmpty())
  • xs.Pop()
  • coutltltxltltendl // output4 8

7
5
3
8
30
Example Questions
  • Q9) You are given the Stack and Queue classes
    covered in lectures

template ltclass Tgt class Stack private T
stacklistMaxStackSize int top public
Stack(void) void Push(const T item) T
Pop(void) void ClearStack(void) int
StackEmpty(void) const int StackFull(void)
const
template ltclass Tgt class Queue private int
front, rear, count T qlistMaxQSize
public Queue(void) void Qinsert(const T
item) T QDelete(void) int QLength(void)
const int QEmpty(void) const
Write a function void findinQ (int key, Queue
ltintgt MyQ) which searches a given key in a
Queue and deletes it if it exists. If the key
does not exist the function ends after the search
without performing an operation on the queue. You
can only use stacks and at most one temporary
variable in this function.
31
Example Questions
  • A9)

void findinQ (int key, Queue ltintgt
MyQ) Stackltintgt S1 Stackltintgt S2 int
temp while(!MyQ.QEmpty()) tempMyQ.QDelete(
) if(temp!key) S1.Push(temp) while(!S1.
StackEmpty()) S2.Push(S1.Pop()) while(!S2.Stac
kEmpty()) MyQ.QInsert(S2.Pop())
32
Example Questions
include ltiostream.hgt void triple(double
num) main() double d10.0 triple(d)
coutltltd return 0 //Triple nums
value void triple(double num)
num3num (C)
include ltiostream.hgt void triple(double
num) main() double d10.0 triple(d)
coutltltd return 0 //Triple nums value void
triple(double num) num3num (D)
include ltiostream.hgt void triple(double
num) main() double d10.0 triple(d)
coutltltd return 0 //Triple nums
value void triple(double num)
num3num (A)
include ltiostream.hgt void triple(double
num) main() double d10.0
triple(d) coutltltd return 0 //Triple
nums value void triple(double num)
num3num (B)

Q10) Given above C programs write down the
output of each, indicate the erroneous program(s)
if any. Justify your answers.
33
Example Questions
include ltiostream.hgt void triple(double
num) main() double d10.0 triple(d)
coutltltd return 0 //Triple nums
value void triple(double num)
num3num (C)
include ltiostream.hgt void triple(double
num) main() double d10.0 triple(d)
coutltltd return 0 //Triple nums value void
triple(double num) num3num (D)
include ltiostream.hgt void triple(double
num) main() double d10.0 triple(d)
coutltltd return 0 //Triple nums
value void triple(double num)
num3num (A)
include ltiostream.hgt void triple(double
num) main() double d10.0
triple(d) coutltltd return 0 //Triple
nums value void triple(double num)
num3num (B)

Write a Comment
User Comments (0)
About PowerShow.com