Docsity

CSci 1200 Data Structures Test 1 Solutions - Prof. Charles V. Stewart, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CSci 1200 — Data Structures
Test 1 — February 13, 2012
SOLUTIONS
1. (15 points) Write a function that takes a vector of doubles and copies its values into two vectors
of doubles, one containing only the negative numbers from the original vector, the other containing
only the positive numbers. Values that are 0 should not be in either vector. For example, if the
original vector contains the values
-1.3, 5.2, 8.7, -4.5, 0.0, 7.8, -9.1, 3.5, 6.6
then the resulting vector of negative numbers should contain
-1.3, -4.5, -9.1
and the resulting vector of positive numbers should contain
5.2, 8.7, 7.8, 3.5, 6.6
Start this problem by writing the function prototype as you think it should appear and then write
the code.
Solution:
void copy_pos_neg( const vector<double>& original,
vector<double>& negatives, // passing by reference is required
vector<double>& positives ) // passing by reference is required
{
negatives.clear(); positives.clear(); // not necessary
for ( unsigned int i=0; i<original.size(); ++i )
{
if ( original[i] < 0 )
negatives.push_back( original[i] );
else if ( original[i] > 0 )
positives.push_back( original[i] );
}
}
pf3
pf4
pf5

Partial preview of the text

Download CSci 1200 Data Structures Test 1 Solutions - Prof. Charles V. Stewart and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSci 1200 — Data Structures

Test 1 — February 13, 2012

SOLUTIONS

  1. (15 points) Write a function that takes a vector of doubles and copies its values into two vectors of doubles, one containing only the negative numbers from the original vector, the other containing only the positive numbers. Values that are 0 should not be in either vector. For example, if the original vector contains the values

-1.3, 5.2, 8.7, -4.5, 0.0, 7.8, -9.1, 3.5, 6.

then the resulting vector of negative numbers should contain

-1.3, -4.5, -9.

and the resulting vector of positive numbers should contain

5.2, 8.7, 7.8, 3.5, 6.

Start this problem by writing the function prototype as you think it should appear and then write the code.

Solution:

void copy_pos_neg( const vector& original, vector& negatives, // passing by reference is required vector& positives ) // passing by reference is required { negatives.clear(); positives.clear(); // not necessary for ( unsigned int i=0; i<original.size(); ++i ) { if ( original[i] < 0 ) negatives.push_back( original[i] ); else if ( original[i] > 0 ) positives.push_back( original[i] ); } }

  1. (10 points) Short answer questions. Please keep your answers very brief.

(a) State two reasons why are standard library vectors are preferrable to ordinary C++ arrays.

Solution: Here are the primary answers I’m hoping to see: (1) Vectors aren’t fixed in size, and (2) vectors know their own size. A number of other answers are possible, primarily built around the member functions for vectors.

(b) When passing a vector to a function why is using pass-by-value usually a bad idea? Solution: The answer should state that the (entire) vector is copied each time the function is called. They can also, instead, mention that changes to the vector are not preserved.

  1. (14 points) Write a recursive function that takes two non-negative integers and returns true if the first is less than (but not equal to) the second. Importantly, - the only comparison operation allowed is to test if a value is equal to 0 (you may also use &&, but you do not need it), - no loops are allowed, and - the only arithmetic operations allowed are to subtract 1.

The function prototype should be

int is_less( int m, int n)

Solution:

int is_less( int m, int n) { if ( n == 0 ) return false; else if ( m == 0 ) return true; else return is_less( m-1, n-1 ); }

  1. (18 points) Consider the following declaration for a Robot class that is designed to realize some of the functionality of the circular robots from Homework 1.

class Robot { public: Robot( float x0, float y0, float in_dx, float in_dy, float rad );

void step(); bool inside_walls( float xbound, float ybound ) const; float distance_to( const Robot& r1 ) const;

private: float x, y; // Robot’s current position float dx, dy; // Robot’s step float radius; // Robot’s radius --- not all robots have the same radius };

(a) Show the implementation of the step member function, which moves the robot a single step for a single time unit. Solution: void Robot::step() { x += dx; y += dy } (b) Show the implementation of the inside walls member function. It should return true if the robot is within the bounds of the walls and is at least radius away from the walls. Solution: void Robot::inside_walls( float xbound, float ybound ) const { return radius < x && x < xbound-radius && radius < y && y < ybound-radius; }

(c) Show the implementation of the distance_to member function which returns the distance between the robots. Here the distance is the Euclidean distance between the centers minus the radii of the two robots. If the resulting distance is negative, the function should return 0. As an example, if one robot is at (0, 0) with radius 1.5 and the other robot is at (3, 4) with radius 2.5, then the function should return 1 since the distance between the centers is 5 and the two radii sum to 4. You may use the sqrt function. Solution: float Robot::distance_to( const Robot& r1 ) const { float dx = x - r1.x; float dy = y - r1.y; float dist = sqrt(dxdx + dydy) - radius - r1.radius; if ( dist < 0 ) return 0; else

return dist; }

for ( int i=0; i<matches.size(); ++i ) cout << " " << matches[i] << ’\n’; }