Beruflich Dokumente
Kultur Dokumente
2009
page 1 of 8
Name____________________
DURATION: 3 HOURS
Total marks =
You do NOT need to write any #include statements for any of the questions. .
1. (1) Write a C++ statement that implements the following equation (sin() and cos() are available):
sin x + cos2 y
=
z+
2. (2) Determine the output of the following program:
int x=7;
int main (void) {
cout << x << endl;
int x = 3;
for (int x=0; x < 2; x++) {
cout << x << endl;
}
cout << ++x << ::x++ << endl;
return 0;
}
3. (2) When we pass a variable to a function, we can pass it by value or by reference. Explain when it is appropriate to pass by value vs. passing by reference.
4. (2) Write a function that swaps the two double numbers that are passed and does not return a value.
5. (2) Assuming that x and y are arrays of real numbers, convert the following mathematical expression into a
C++ function that returns the computed value
z=
N1
xiyi
i=0
6. (2) If we are inside of a for or while loop, explain what happens when we encounter:
a) continue
b) break
7. (1) When should a class implement a destructor?
page 2 of 8
Name____________________
13. (1) Although not required by C/C++, I have instructed you to always use braces for your if-else, for, and
while constructs, even if there is only a single statement, as shown below. Why is this a good practice?
if (a > b) {
max = a;
} else {
max = b;
}
15. (1) Define scope and explain why it is important to limit the scope of variables.
page 3 of 8
Name____________________
16. (3) Given the declaration of the class Time (which is in the file time.h):
class Time {
private:
int hours, minutes, seconds;
public:
Time (int hour, int min, int sec);
int elapsed (Time t1);
};
write the constructor and the member function elapsed() that returns the difference between two times in
seconds. You do not need to do any error checking in the constructor. Assume that these functions will be
in a different file, time.cpp. Hint: do all of your elapsed() calculations in seconds.
17. (1) Given the following function prototype, write another function prototype that overloads the given function and has a default value.
double changeValues (float x, float y);
18. (2) When we define a class, we declare some class members as private and others as public. Which members are typically private and which members are typically public?
19. (2) C++ will automatically create a copy constructor that performs a shallow copy.
a) What is the difference between a shallow copy and a deep copy?
b) Give an example of when we should provide a copy constructor that performs a deep copy.
20. (1) private members of a class have class scope. What does that mean?
page 4 of 8
Name____________________
21. (2) Draw a UML class diagram containing four classes and having two levels of inheritance. Show at least
one attribute and one method for each class.
22. (1) Normally, each instance from a class has its own set of variables. What is different when we have a
variable inside of a class that is declared as being static?
24. (2) Explain the difference between having a virtual function inside of a class vs. having a pure virtual function inside of a class?
26. (2) Give an example of how polymorphism can be useful when dealing with an array of objects.
27. (1) Given the following first line of a class declaration, write the first line for another class that inherits the
given class.
class Shape {
page 5 of 8
Name____________________
28. (1) Modify the following struct so that it can be used in a linked list.
struct employee {
string name;
float wage;
};
29. (2) We saw that a linear search is O(N) whereas a binary search is O(log2N). What does the O signify?
30. (2) In the lab about rectangular spherical conversion, we returned an aggregate of data from our functions. Write the code to declare such an aggregate.
31. (1) The difference between the secant method and the Newton-Raphson method for root finding is that the
secant method does not require the ____________________ of the function.
32. (2) When we performed numerical integration in the lab, we saw:
1) initially, as the number of intervals was increased, the error decreased; and
2) then the error began to increase.
Name and define/describe the two types of error that are responsible for these two phenomena.
33. (2) Given the mathematical function x3 + x - 1 and the starting interval x1 =1 and x3 =2, perform two iterations of the bisection method (I want numbers, not code).
page 6 of 8
Name____________________
34. (1) In the lab, we saw that the slope of the truncation error plot for Simpsons rule was twice as steep as the
plot for the trapezoidal rule. What quantitative effect does this have on the error when both methods are applied using the same number of intervals?
a) write a function prototype for a constructor inside of the class declaration above (you will also
need to write a line of code before the prototype so that it is accessible);
b) write a constructor for the class that creates the required array (assume that your constructor method
is located in a different file from the class declaration);
c) write the declaration for the destructor inside of the class and write the code for the destructor; and
d) write a statement that instantiates a member of the MyArray class and a statement that invokes the
getSize() function on that object (you do not need to write getSize()).
Name____________________
page 7 of 8
36. (4) An algorithm for the regula falsi method for root finding is:
1) Given the current interval (x1, x3) find the estimate of the root using
x2 = x1 (x3 x1 )
f1
f3 f1
2) If the absolute value of the function at x2 is less than some small number (), return x2
3) Select the new interval based on which side of x2 the function crosses zero
Given this, complete the C++ function
double regulaFalsi (double (*f)(double x), double x1, double x3, double epsilon) {
37. (4) Given the following equation, complete the C++ function for Simpsons rule for numerical integration
n1
n2
$
$
x
S=
f (a + ix) + 2
f (a + ix) + f (b)
f (a) + 4
i=1
i=2
i odd
i even
page 8 of 8
Name____________________
38. (4) Given that one step of the second-order Runge-Kutta method is:
k1
= hf (xn , yn )
k2
yn+1
where h
= yn + k2
=
(x2 x1 )/N
Write a function to solve an initial value problem starting at x1 and ending at x2 using N steps
double rk2 (double (*f)(double x, double y), double x0, double y0, double xn,
long N) {