CSE 1302C_Test2_SP11.docx

advertisement
CSE 1302C – Test 2
4/4/2011
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: What is the output of the following code chunk? Show the stack at its tallest.
static double recurse(double i, double j)
{
Console.WriteLine ("i is " + i + " and j is " + j);
if (i <= 1)
{
return 0;
}
else
{
return 1 + recurse(i / j, j);
}
}
static void Main(string[] args)
{
Console.WriteLine(recurse(16.0, 2.0));
}
Question 2: Given the code below, draw the status of the stack by the end of main.
static void Main(string[] args)
{
Stack<int> myStack = new Stack<int>();
myStack.Push(6);
myStack.Push(3);
myStack.Push(1);
myStack.Push(5);
myStack.Pop();
myStack.Push(40);
}
Question 3: Assume we have an array of n sorted numbers (ints) and a linked list of n unsorted numbers (ints).
Using O-notation (e.g. O(log2n) ), fill in how long it takes to do the following operations in the most efficient way
possible.
Array
Linked List
Find the min
Find the max
Find the average of all numbers
Insert a new number
Find if a number is in the data
structure (e.g. if it exists)
Remove a number if it exists
Question 4: Assume you have a text file called “data.jef”. Write a code snippet that counts the total number lines
in that file and prints out the total to the user (e.g. “this file has 105 lines of text”).
Extra credit (5 points): what’s the common name of the function in question 1?
Download