Docsity

Test 1 - Practice Problems on Data Structures | CSCI 1200, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI-1200 Computer Science II — Spring 2006
Test 1 — Practice Problems
General Information
•Test 1 will be held Tuesday, February 7th, 2006 10-11:50am, West Hall Auditorium. 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.
•Coverage: Lectures 1-6, Labs 1-3, HW 1-2.
•Closed-book and closed-notes except for 1 sheet of 8.5x11 inch paper (front & back) that may be hand-
written or printed. Computers, cell-phones, palm pilots, calculators, PDAs, etc. are not permitted
and must be turned off.
•All students must bring their Rensselaer photo ID card.
•Below are relevant sample questions from previous tests. The best thing you can do to prepare for
this test is practice. Try these problems with pencil & paper first. Then practice programming (with
a computer) these exercises and other exercises from lecture, lab, homework and the textbook.
Practice Problems
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. 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 function called less string that mimics the effect of the <operator on strings. In other
words, given strings aand b,less string( a, b) should return true if and only if a<b. You
may not use <on the strings, but you may use <on individual characters in the strings. 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"
4. 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.
pf3
pf4

Partial preview of the text

Download Test 1 - Practice Problems on Data Structures | CSCI 1200 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Spring 2006

Test 1 — Practice Problems

General Information

  • Test 1 will be held Tuesday, February 7th, 2006 10-11:50am, West Hall Auditorium. 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.
  • Coverage: Lectures 1-6, Labs 1-3, HW 1-2.
  • Closed-book and closed-notes except for 1 sheet of 8.5x11 inch paper (front & back) that may be hand- written or printed. Computers, cell-phones, palm pilots, calculators, PDAs, etc. are not permitted and must be turned off.
  • All students must bring their Rensselaer photo ID card.
  • Below are relevant sample questions from previous tests. The best thing you can do to prepare for this test is practice. Try these problems with pencil & paper first. Then practice programming (with a computer) these exercises and other exercises from lecture, lab, homework and the textbook.

Practice Problems

  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. 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 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. You may not use < on the strings, but you may use < on individual characters in the strings. 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. 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.

  1. Give an order notation (“O”) estimate of the worst-case number of operations required by the fol- lowing sorting function. Briefly justify your answer:

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 behav- ior 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. Write a complete program (you don’t need to provide the #include statements) that (a) reads in a sequence of strings until a string starting with the letter ’x’ is read, and (b) then reads in another sequence of strings until the end of input is reached. For each string in the second sequence, the program should output the string and the number of times it occurs in the first sequence. For example, given the input:

hello now good now no hello first last second won now song xray now silly first

The program should output:

now 3 silly 0 first 1

You should write functions as appropriate to make your program clean and modular.