





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: DATA STRUCTURES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Spring 2008;
Typology: Exams
1 / 9
This page cannot be seen from the preview
Don't miss anything!






abe lincoln went to the white house
Your function should return the value 12. You may assume that all letters are lower case. Here is the function prototype:
int count_vowels( const vector
int multiply( int m, int n)
a = "abc", b = "abd" a = "cab", b = "cabbage" a = "christine", b = "christopher"
void more_confused( vector
int main() { vector
more_confused( a, b ); a[0] = 7; a[1] = 9; more_confused( b, a );
cout << "3: "; for ( unsigned int i=0; i<a.size(); ++i ) cout << a[i] << " "; cout << endl;
cout << "4: "; for ( unsigned int i=0; i<b.size(); ++i ) cout << b[i] << " "; cout << endl;
return 0; }
class Point { public: Point(); Point( double in_x, double in_y, double in_z ); void get( double & x, double & y, double & z ) const; void set( double x, double y, double z ); bool dominates( const Point & other ); private: double px, py, pz; };
bool dominates_v2( const Point & left, const Point & right );
(a) Provide the implementation of the default constructor — the constructor that takes no arguments. It should assign 0 to each of the member variables. (b) The member function dominates should return true whenever each of the point’s coordinates is greater than or equal to each of the corresponding coordinates in the other point. The function should return false otherwise. For example, given Point p( 1.5, 5.0, -1 ); Point q( 1.4, 5.0, -3 ); Point r( -3, 8.1, -7 ); Then p.dominates( q ) should return true but the function calls p.dominates( r ) r.dominates( q ) q.dominates(r) should each return false. Write member function dominates. (c) The function dominates v2 should be a non-member function version of dominates. Its behavior should be essentially the same as the member function version, so that for the Point objects defined in (b), dominates_v2( p, q ) should return true but the function calls dominates_v2( p, r ) dominates_v2( r, q ) dominates_v2( q, r) should each return true. Write dominates v2.
class bar { public: bar( int in_x, double in_y, const vector
private: int x; double y; vector
This class is incomplete because no member functions are defined.
(a) Write a member function of class bar called OrderZ that sorts the member variable z into in- creasing order. Show both its prototype in bar and its implementation, outside of the declaration for bar. (b) Write a function that takes a vector of bar objects and re-arranges them so that the objects are ordered by decreasing value of x, and by increasing y for bar’s with equal values of x. Give two different solutions. One solution uses an operator< on bar objects and the other uses a non-operator comparison function on bar objects. In both cases, you will need to add member functions to bar. Be sure to include these in your solution.
int x = 45; int y = 30; int *p = &x; *p = 20; cout << "a: x = " << x << endl;
int *q = &y; int temp = *p; *p = *q; *q = temp; cout << "b: x = " << x << ", y = " << y << endl;
int * r = p; p = q; q = r; cout << "c: *p = " << *p << ", *q = " << *q << endl; cout << "d: x = " << x << ", y = " << y << endl;
template
Recall that Vec
T* m_data; // Pointer to first location in the allocated array size_type m_size; // Number of elements stored in the vector size_type m_alloc; // Number of array locations allocated, m_size <= m_alloc
for( unsigned int i=0; i<4; ++i ) cout << a[i] << " "; cout << endl;
Here are several examples of how we will create and initialize Major objects:
Major sally("Sally"); Major fred("Fred"); Major bob("Bob"); Major alice("Alice");
Initially each student’s major is listed as undeclared, but they can change their major with the declareMajor member function:
sally.declareMajor("Economics"); fred.declareMajor("Information Technology"); bob.declareMajor("Chemistry"); sally.declareMajor("Information Technology"); bob.declareMajor("Psychology"); bob.declareMajor("Biology");
We also need various accessor functions to get the student’s name, their major, and the number of times they changed their major. Here’s an example use of these member functions:
cout << bob.getName() << " changed majors " << bob.numChanges() << " time(s) and is currently majoring in " << bob.getMajor() << "." << endl;
Which will result in this output to the screen:
Bob changed majors 3 time(s) and is currently majoring in Biology.
We can also store multiple Major objects in a vector to organize the student registration database:
vector
In the third part of this problem you will write code to sort and output this database with the students grouped by their current major and sorted alphabetically:
Biology Bob Information Technology Fred Sally Undeclared Alice
(a) Using the foregoing sample code as your guide, write the class declaration for the Major object. That is, write the header file (major.h) for this class. You don’t need to worry about the #include lines or other pre-processor directives. Decide how you are going to store the information for each object. Focus on getting the member function prototypes correct. Use const and call by reference where appropriate. Make sure you label what parts of the class are public and private. Don’t include any of the member function implementations here (even if they are just one line). Save the implementation for the next part.
(b) Now implement the constructor and member functions you declared in the previous part, as they would appear in the corresponding major.cpp file.
(c) Now we need to prepare the student lists for each department. Given the all_students vector that stores all of the students, your task is to write code that sorts the students into groups by major and then alphabetically within each group. This can be done with a single call to the sort function for vectors. First write the helper function you’ll need for the sort operation. (d) Finally, write the code that uses this helper function to sort and then output the registration database in this format: Biology Bob Information Technology Fred Sally Undeclared Alice
void order( vector
int main() { vector
3.4, 7.5, 1.1, 11.9, 17.3, 27.1, 16.9, 15.4, 29.