King Fahd University of Petroleum and Minerals

advertisement
1
King Fahd University of Petroleum and Minerals
College of Computer Science and Engineering
Information and Computer Science Department
ICS 202-02: Data Structures
Spring 2008-2009
Quiz#1, Wednesday March 11th 2009
Name:
ID#:
1. (10 points) Suppose we have a container which contains only instances of
the Integer class. Design a Visitor class, MaxVisitor, that will find the
maximum value of all instances of the container after completing the
execution of the accept method that takes an instance of MaxVisitor as its
argument.
2 pts
3 pt
public class MaxVisitor extends AbstractVisitor {
private Integer max=null;
public void visit (Object myInteger) {
Integer value = (Integer) myInteger;
if (max == null)
max = value;
if (value.compareTo(max) >= 0)
max = value;
}
}
2
2. (10 points) For the following piece of code:
a) (7 points) Find the number of times statement1 gets executed.
b) (3 points) Express the running time of this piece of code in terms
of big O() notation.
n ; i++) {
1 for (i=1; i <=
2
sum[i] = 0;
3
for (j=1; j <= i2 ; j++)
4
sum[i] = sum[i] + j; //statement1
5 }
6 return true
n
a)
i
2
n
 1   i
i 1 j 1
4 pts
b) O

2

i 1
1 pt
n

 2
n 1

n 1
6
2 pts

n n n  O  n 1.5 

n
n

 c  c   1  c .  n  m  1
i m
i m
n
i 
i 1
n
i
2

n  n  1
2
n  n  1 2n  1
Download