CSCI 216 Fall 2013 Exam #1

Instructions:  This test is closed book, closed neighbor, closed cell phone, closed calculator, closed computer, but open mind.  Answer the questions below concisely and completely.  Write legibly—if I can’t decipher it, it’s wrong.  Do NOT do extra work—just answer the questions as asked.  If you need more room for an answer, use the back of one of the sheets, and indicate to me where I can find your answer.  Not counting the bonus question(s), there are 105 points on the test.  For additional point(s), write you’re your name only on pages one, three, five, and so on.  Read through the entire test before answering ANY questions in order to find the easy questions and answer them first, and thus maximize your score.  Work on the bonus question(s) only after you have finished the exam.  Good luck and have fun.

 

Each of the questions below is worth 7 points.

1.     Write C++ code to declare a structure named complex that has two doubles in it, one named real, and one named imag.

 

 

 

 

 

 

 

 

2.     Write C++ code to declare foo to be a variable of type complex, defined in question 1 above.

 

 

 

 

 

 

 

 

3.     Write C++ code to set the imag field of the variable foo, defined in question 2, to contain the value 3.5.

 

 

 

 

 

 

 

 

4.     Write C++ code to declare ptr to be a pointer to a structure of type complex.

 

 

 

 

 

 

 

 

5.     Write C++ code to set ptr to point to the variable foo, declared in question 2 above.

 

 

 

6.     Write C++ code to use ptr to assign the value 7 to the real field of the structure pointed to by the pointer ptr, declared in question 5 above.

 

 

 

 

 

 

 

 

7.     Using the style we used in class, write a C++ class header for a complex number class that has two variables, a double named real and a double named imag.  It should have a default constructor as well as one that takes a double and one that takes two doubles, and a destructor.  It should have member functions named add, sub, mul, and div, as well as read and print.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8.     For the complex class in question 7, above, how would you change the function prototypes for addition, subtraction, multiplication, and division, to overload the operators +, -, *, and / as member functions?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9.     For the complex class in question 7 above, how would we overload the operators +, -, *, and / as friend functions?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

10.  For the complex class in question 7 above, what would the function prototypes look like if we replaced the read and print member functions with overloaded operators >> and <<?

 

 

 

 

 

 

 

 

 

 

 

 

 

11.  Write the C++ code for the constructor that takes two doubles and assigns the values passed in to the two variables real and imag.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

12.  Write C++ code to declare bar to be a variable of type complex defined in question 7 above.

 

 

 

 

 

 

 

13.  Write C++ code to declare ptr to be a pointer to a complex object which is a member of the class defined in question 7 above.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.  Write C++ code to implement the member function add defined in the complex class defined in question 7 above.  Remember to add two complex numbers you add the real parts and you add the imaginary parts.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

15.  How do we access the first parameter of a member function for a class in C++?

 


 

BONUS:  Sudoku…each row, column, and 3x3 highlighted square should contain each of the digits 1 through 9 exactly once.  Fill in the empty squares to make this true.

 

 

7

9

 

 

5

 

 

1

5

3

 

 

9

 

8

 

 

2

 

6

 

 

 

 

 

 

 

5

 

3

 

1

 

 

8

 

8

1

 

 

 

2

5

 

6

 

 

4

 

8

 

1

 

 

 

 

 

 

 

1

 

9

 

 

3

 

1

 

 

8

7

1

 

 

9

 

 

3

2