CSCI 261 Programming Concepts (C++)

Fall/Winter 2011

Homework 24: Meet the Vector

Right-click me and select "Save target as..." to save.

Concepts

For this assignment, focus on one main concept: how to create and use vector objects.

Drawbacks to Using Arrays

While arrays are powerful, efficient data structures they have certain constraints that often "annoy" the student of programming. One specific drawback to arrays is that, unless you know about dynamic memory allocation, you have to specify the size of the array such that it is known at compile time.

For example, let's say you wanted to create a program that reads an unknown number of values from a data file. Would you store them in an array that can hold ten values? Or one thousand? How many data points are in the file? You don't know. More importantly, what should the size of your data structure be? You don't know.

"Ahh, I'll just declare an array that can hold 100,000 values, surely that's plenty," you might think. What would happen if the data file contained more than 100,000 data points? You would be in deep doo-doo.

While there are ways to solve this problem with arrays and dynamic memory allocation, it is far more sane to learn how to use a vector object.

vector Objects

In class you were introduced to vectors and some ways in which you may use them, so we'll simply summarize the details here.

First, recognize that you must include the vector library like so:

#include <vector>

Next, to create a vector that holds, say, character values, you need to recognize the "strange" syntax:

vector<char> myLetters;

This is like telling the computer, "Hey computer, myLetters is a vector that can hold char values." Say that five times aloud to disturb the people around you. Better yet, shout it, and watch everyone really freak out.

Notice that the datatype of myLetters is "a vector that can hold chars." Also notice how you do not need to specify the size of the vector.

Since vectors are objects, they have member functions that you may call. For example, you can push an element onto the end of the vector, pop an element off the end, ask the vector what it's first/last element is, and ask the vector to tell you how many elements it contains.

There are many more features and clever uses of vectors than what we can describe this week. Your goal should be to recognize that vector objects exist, that they allow greater flexibility than arrays, and how to use a vector in a program. The truth is, these days there are very few people at C++ programming parties using an array to store a series of values -- most prefer to use vectors.

Instructions

Write a program that allows the user to enter an unknown number of characters, stores those characters in a data structure (a vector) and then prints the values to the screen. An example interaction looks like this:

Witness my exciting first use of vectors!
Enter as many letters as you like, or ! to quit. Mmmkay?
Enter a letter: b
Enter a letter: u
Enter a letter: r
Enter a letter: r
Enter a letter: p
Enter a letter: !
Great. You entered: burrp
The first character you entered was b.
The last character you entered was p.

Note how the last character typed was an exclamation point and was not printed to the screen. Furthermore, it should not be stored in the vector.

While there are many ways of solving this assignment, you must make use of the vector member functions noted below in the rubric. You may define your own functions, but we recommend just entering this simple program entirely in main.

Requirements and Rubric

A friendly message from The Terminator, our grading program

*bzzzt* Hel-lo. I will check for the following:


Your program must terminate after capturing an unknown number of characters and printing the output mentioned above.

Your program should print the characters entered all on one line (not one character per line).

Your program must stop accepting input when a ! is entered.


*bzzt* This is an easy one. Are you sure you can *bzzt* defeat me?

This work is worth 95 points.

Requirement Points Notes
Place your name in the comment header in main.cpp 2
Correct submission of src directory as a .zip file. 3
Allows the user to enter any number of characters. 10
Stops prompting the user for input when a ! is entered 5 while loop?
Correctly prints all characters entered 10
Does not store ! in the vector 5
Does not print ! as one of the characters entered 5
Correctly prints the first character entered 5
Correctly prints the last character entered 5
Correct instantiation of the vector object 5
Uses the push_back member function to store values in the vector 10
Uses the size member function 10
Uses the front member function 10
Uses the back member function 10

Concepts Exercised: application, OOP, repetitive tasks, I/O, making decisions, declaring facts, APIs, functions