Practice Exam I for Data Structures | CSCI 1200, Exams of Data Structures and Algorithms

Material Type: Exam; Class: DATA STRUCTURES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Spring 2008;

Typology: Exams

Pre 2010

Uploaded on 08/09/2009

koofers-user-zms
koofers-user-zms 🇺🇸

9 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science II CSci 1200
Test 1 Questions
Overview
Test 1 will be held Tuesday, February 12, 2008, 2:00-3:30pm, Darrin 308. No make-ups will
be given except for emergency situations, and even then a written excuse from the Dean of Students
office will be required.
Purpose: Check your understanding of the basics of (a) C++, (b) solving small computational prob-
lems, (c) the standard library (streams, strings and vectors), and (d) memory management.
Coverage: Lectures 1-7, Labs 1-4, HW 1-3.
Closed-book and closed-notes. At the start of the test, we will provide a handout summary of important
C++ syntax, including standard library classes.
Below are many sample questions. Solutions to most of the problems will be posted on-line.
The test questions will be drawn from these questions, often with minor modifications, and from
the lecture, homework and lab problems.
How to study?
Work through the sample questions, writing out solutions! You are welcome to do this with other
students.
Review and re-do lecture exercises, lab and homework problems.
Identify the problems that cause you difficulty and review lecture notes and background reading
on these topic areas.
Questions
1. Write a code segment that copies the contents of a string into a vector of char in reverse order.
2. Write a function that takes a vector of strings as an argument and returns the number of vowels that
appear in the string. For the purposes of this question, a vowel is defined as an ’a’, ’e’, ’i’, ’o’ or ’u’.
For example, if the vector contains the strings
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<string>& strings )
3. Write a recursive function to multiply two non-negative integers using only addition, subtraction and
comparison operations. No loops are allowed in the function. The function prototype should be
int multiply( int m, int n)
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Practice Exam I for Data Structures | CSCI 1200 and more Exams Data Structures and Algorithms in PDF only on Docsity!

Computer Science II — CSci 1200

Test 1 Questions

Overview

  • Test 1 will be held Tuesday, February 12, 2008, 2:00-3:30pm, Darrin 308. No make-ups will be given except for emergency situations, and even then a written excuse from the Dean of Students office will be required.
  • Purpose: Check your understanding of the basics of (a) C++, (b) solving small computational prob- lems, (c) the standard library (streams, strings and vectors), and (d) memory management.
  • Coverage: Lectures 1-7, Labs 1-4, HW 1-3.
  • Closed-book and closed-notes. At the start of the test, we will provide a handout summary of important C++ syntax, including standard library classes.
  • Below are many sample questions. Solutions to most of the problems will be posted on-line.
  • The test questions will be drawn from these questions, often with minor modifications, and from the lecture, homework and lab problems.
  • How to study?
    • Work through the sample questions, writing out solutions! You are welcome to do this with other students.
    • Review and re-do lecture exercises, lab and homework problems.
    • Identify the problems that cause you difficulty and review lecture notes and background reading on these topic areas.

Questions

  1. Write a code segment that copies the contents of a string into a vector of char in reverse order.
  2. Write a function that takes a vector of strings as an argument and returns the number of vowels that appear in the string. For the purposes of this question, a vowel is defined as an ’a’, ’e’, ’i’, ’o’ or ’u’. For example, if the vector contains the strings

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& strings )

  1. Write a recursive function to multiply two non-negative integers using only addition, subtraction and comparison operations. No loops are allowed in the function. The function prototype should be

int multiply( int m, int n)

  1. Write a function called less_string that mimics the effect of the < operator on strings. In other words, given strings a and b, less string( a, b) should return true if and only if a < b. Of course, you may use < on individual characters in the string. Start by getting the function prototype correct. Here are examples of pairs for which your function should return true:

a = "abc", b = "abd" a = "cab", b = "cabbage" a = "christine", b = "christopher"

  1. What is the output from the following program? We strongly suggest that you draw the contents of the vectors to help you visualize what is happening.

void more_confused( vector a, vector & b ) { for ( unsigned int i=0; i<2; ++i ) { int temp = a[i]; a[i] = b[i]; b[i] = temp; } cout << "1: "; for ( unsigned int i=0; i<a.size(); ++i ) cout << a[i] << " "; cout << endl; cout << "2: "; for ( unsigned int i=0; i<b.size(); ++i ) cout << b[i] << " "; cout << endl; }

int main() { vector a, b; a.push_back(1); a.push_back(3); a.push_back(5); b.push_back(2); b.push_back(4);

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; }

  1. Consider the following declaration of a Point class, including an associated non-member function:

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.

  1. Consider the following start to a class declaration:

class bar { public: bar( int in_x, double in_y, const vector& in_z ) : x(in_x), y(in_y), z(in_z) {}

private: int x; double y; vector z; };

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.

  1. Given an array of integers, intarray, and a number of array elements, n, write a short code segment that uses pointer arithmetic and dereferencing to add every second entry in the array. For example, when intarray is 0 1 2 3 4 5 6 7 8 1 16 4 -3 2 76 9 3 6 and n==9, the segment should add 1 + 4 + 2 + 9 + 6 to get 22. Store the result in a variable called sum.
  2. Show the output from the following code segment.

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;

  1. Write a Vec class member function that creates a new Vec from the current Vec that stores the same values as the original vector but in reverse order. The function prototype is

template Vec Vec::reverse() const;

Recall that Vec class objects have three member variables:

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

  1. Here is a program that is supposed to detect and output each distinct integer that occurs 2 or more times in an input sequence. Unfortunately, there is a small problem with this program causing it to work incorrectly for some inputs.

for( unsigned int i=0; i<4; ++i ) cout << a[i] << " "; cout << endl;

  1. In this problem you will implement a simple class named Major to store information about students and their declared majors. Since students often change their minds, your class will need to handle these changes and keep track of how many times a student changed majors. Please carefully read through all of the information below before working on the class design.

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 all_students; all_students.push_back(sally); all_students.push_back(fred); all_students.push_back(bob); all_students.push_back(alice);

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

  1. Very briefly state two reasons why vectors are preferrable to arrays.
  2. Show the output of the following code. Assume that all necessary header files have been included.

void order( vector a, double min, double & max ) { sort( a.begin(), a.end() ); min = a[0]; max = a[ a.size()-1 ]; }

int main() { vector b(4); b[0]=10; b[1]=21.1; b[2]=2.3; b[3]=7.9; double first=0, last=0; order( b, first, last ); cout << "first = " << first << ", last = " << last << ’\n’; for ( unsigned int i=0; i<b.size(); ++i ) cout << b[i] << ’ ’; cout << ’\n’; return 0; }

  1. Write a function that takes a vector of doubles as an argument and returns an integer giving the length of the longest run of consecutive, increasing values. As one example, if the vector contains

3.4, 7.5, 1.1, 11.9, 17.3, 27.1, 16.9, 15.4, 29.