CSE 1302C Test 2_Su11.docx

advertisement
CSE 1302C – Test 2
6/20/2010
Name:__________________________________
Directions: Pencil and paper only (no books or notes). All answers are to be your own without the
assistance of others (i.e. don’t cheat).
Question 1 (30 pts): Assume that you have a sorted array (ascending) and an unsorted linked list, and
each data structure has n elements in it. Using O-notation (Big-Oh), show how long it would it take to do
the following operations (below). You must explain why it takes the time that it does…

Find the minimum element in the array and put it (a copy) into the linked list. Why?

Find the maximum element in the array and put it (a copy) into the linked list. Why?

Find the minimum element in the linked list and put it (a copy) into the array. Why?

Search for an element in the array and put it (a copy) into the linked list. Why?

Search for an element in the linked list and put it (a copy) into the array. Why?

Copy all the data from the linked list and put it into the array. Why?
Question 2 (30 pts): What is the output of the following code chunk? Show the stack at its tallest.
static int recurse(int n, int j)
{
Console.WriteLine("n:" + n + "
j:" + j);
if (n <= 0)
{
return 1;
}
else if (n > j)
{
return j * recurse(n - 2, j - 1);
}
else
{
return n * recurse(n - 1, j - 2);
}
}
static void Main(string[] args)
{
Console.WriteLine(recurse(8, 7));
}
Question 3 (40 pts): Assume you have a text file called “data.jef” which has a separate number on each
line. For half of the credit (20 pts), write a code snippet that finds the largest number in the file and
prints it out to the screen (e.g. “the largest number is 487”). For the other half of the credit (20 pts),
handle any exceptions that might occur; after all, I may have lied and “data.jef” doesn’t exist!
Download