CS 12 - Array and Vector Exercises


CS 12 Homepage
The vector class in the Standard Template Library is a terribly useful tool for professional C++ programmers. Everywhere that you could use an array, a vector will do. Vectors will also track your memory for you (like well-behaved dynamic arrays) and can expand to fit the data inserted in them (like a self-expanding array). In fact, the only two things that are not so good about vectors is that they still don't do bounds checking for you, and compiler errors involving vectors are notoriously hard to read. Still, the benefits far outweight the risks.

Since vectors are such a useful tool, and being able to expertly operate on arrays (and array-like structures) is very important, we're introducing an additional set of array exercises which are to be done using vectors.

The Assignment

7 points: Implement a program that reads in an arbitrarily large list of integers, stores them in a vector, and then performs a sequence of operations on them. You should simply read in integers until EOF, and then perform your set of operations on them. Each operation should be written in its own function, and that function should not do any I/O (writing functions that way is more general purpose.). The minimum operations that your program must perform are the following: .2 points Find max: find the maximum value in the vector (hint: you can make this really easy with what you have already written.)

.2 points Reverse: write a function to reverse your vector

.2 points Remove: write a function that takes in an integer n and remove all instances of n from your vector. (That is, if your vector was [1,2,4,5,1,3] and you called remove(myVector, 1), it would return [2,4,5,3]).

.2 points Standard Deviation: Write a function to calculate the standard deviation of your vector.

.2 points Sort: write your own function to sort your vector.

.2 points Sort: Find how to use the STL builtin algorithms for sorting your vector.

.2 points Read about vector capacities (Chapter 11.3). Use the "reserve" method for your vector. What happens if you reserve less space than you use? What would happen if you used more space than you allocated with a dynamic array of integers?


© 2003 UC Riverside Department of Computer Science & Engineering. All rights reserved.