Study Guides (238,252)
Canada (115,037)
CS 246 (16)
Ric Holt (3)

Midterm + solution of Winter 2009 useful for practice

8 Pages
Unlock Document

University of Waterloo
Computer Science
CS 246
Ric Holt

CS 246 Midterm Review Sean Hsueh Winter 2009, Updated: February 23, 2009 1. Considering the following code and its output. Draw out the state of the memory at the speci▯ed location. Write out the output of stderr. #include #include using namespace std; char vowels[] = "aeiou"; void mystery ( const char▯ ptr ) f const char▯ tmp = ptr; cout << "&ptr: " << &ptr << endl; cout << "ptr: " << ( void▯ ) ptr << endl; cout << "&tmp: " << &tmp << endl; for ( ; ▯tmp; tmp++ ) f cerr << "tmp: " << ( void▯ ) tmp << endl; cerr << ▯tmp << endl; g // RIGHT HERE g int main () f char mystring [ 10 ]; strcpy ( mystring, "test" ); int size = strlen ( mystring ); cout << "&vowels: " << &vowels << endl; cout << "&mystring: " << &mystring << endl; cout << "&size: " << &size << endl; mystery ( vowels ); g; Stdout ------------------------------------------------------------------ &vowels: 0x804a040 &mystring: 0xbf8fedd6 &size: 0xbf8fedd0 &ptr: 0xbf8fedb0 &tmp: 0xbf8feda0 1 2. Consider the following code. What is the output of the program? Listing 1: out1.h cout << "264" << endl; Listing 2: out2.h cout << "1351" << endl; Listing 3: out3.h cout << "454" << endl; Listing 4: out4.h cout << "." << endl; Listing 5: prog.cpp #include using namespace std; int main () f #include "out3.h" #include "out4.h" #include "out1.h" #include "out2.h" #include "out4.h" return 0; g 2 3. Write a stack for doubles using a linked list. The stack has the following interface. Do not use STL: Listing 6: DoubleStack.h class DoubleStack f private: // Whatever is necessary.. public: DoubleStack (); void push ( double val ); double pop (); // assume pop is always called with at least 1 element in stack double peek (); // assume peek is always called with at least 1 element in stack bool isEmpty (); ~DoubleStack (); g; 3 4. Write a program that compares two ▯les and output the line numbers as well as its content where they di▯er. If a ▯le is shorter than the other, then the last comparable line is the number of lines in the shorter ▯le. After making the last possible comparison, output the number of lines compared, and the number of line di▯erences found. Here is an example: ▯ File dif1: No one can beat my level 20 cleric. Except maybe a player with the ability to save and reload. However if I had god mode on, it would be a different story. ▯ File dif2: No one can beat my epic level cleric. Except maybe a player with the ability to save and reload. How
More Less

Related notes for CS 246

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.