Math 191 --- Spring 2005 --- Exam 1 Name:

advertisement
CSCI 30, Section 0124, Spring 2014, Exam 1 (Take-Home)
1. Write a template function, EvalPoly, to evaluate a general polynomial P( x )  an x  an1 x
n
n 1
   a1 x  a0 .
The parameters for EvalPoly should be an array containing the coefficients, the degree of the polynomial, and a
parameter for x .
The function should return the value of P ( x ) .
Please note that if you evaluate the polynomial "in order" as indicated by:
P( x )  3x 3  4 x 2  7 x  8
then your code will be inefficient. The same polynomial, evaluated as indicated by:
P( x )  ((( 3) x  ( 4)) x  7) x  ( 8)
and following order of operations, will be much closer to optimum, and hence is the kind of approach you should take in
your function.
The template function EvalPoly may be defined in the same file as your “main” function, or you may include it in a “.h”
file.
The testing of your function EvalPoly should be done in the “main” function.
2. Write a template class, Vector2D, to model a two-dimensional vector.
(a) The class should have a member function to return the magnitude of the vector.
(b) The class should have a member function to return the direction of the vector as an angle in radians.
(c) The class should overload the input and output operators.
(d) The class should include a static member data item to determine if the basic elements are floating point or not. You
may wish to indicate “non-floating point” by a “number of decimal places” value of 0, and “floating point” by a
“number of decimal places” value greater than 0. This would give some flexibility to have the number of decimal
places to which “x and y” are printed out to be different than the number of decimal places to which the magnitude
and direction are printed out.
(e) The class should include a static member data item to determine the number of significant digits for the magnitude
and direction, which will always be floating point.
Finally, you should provide complete testing of the class in code that will be located in your “main” function.
Examples:
Vector2D<int,float> vif;
// The data type for the x and y coordinates of the vector <x,y> will be int.
// The data type for the magnitude and direction of the vector <x,y> will be float.
Vector2D<double,double> vdd;
// The data type for the x and y coordinates of the vector <x,y> will be double.
// The data type for the magnitude and direction of the vector <x,y> will be double.
Vector2D<long int,double> vlid;
// The data type for the x and y coordinates of the vector <x,y> will be “long int”.
// The data type for the magnitude and direction of the vector <x,y> will be double.
What you will be turning in will be a project named according to the convention:
Last Name – First Initial – Exam 1
Examples:
If Brent Dixon is turning in his exam, then the name of the project would be DixonBExam1.
If Tae Ho Kim is turning in his exam, then the name of the project would be KimTExam1.
If Junkyum Kim is turning in his exam, then the name of the project would be KimJExam1.
If Courtney Castrey is turning in her exam, then the name of the project would be CastreyCExam1.
The name of the file containing the “main” function should have the same name as the project.
Examples:
DixonBExam1.cpp
KimTExam1.cpp
KimJExam1.cpp
CastreyCExam1.cpp
The name of the file containing the Vector2D template class should be Vector2d.h.
Download