Collaboration on this lab exercise is strongly ENCOURAGED. The exercise is intended for practice, not assessment -- most people who try should get all participation points. Feel free to ask for help from, and provide help to, others. You shouldn't blindly copy solutions from one another (or from anywhere else) nor simply write code for someone else (even if you explain it as you write), but you can certainly help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc. If you get done early, feel free to walk around and help others.
A vector is a structured collection of components (often called vector elements) that can be accessed individually by specifying the position of a component with a single index value.
You can think of a vector as simply an improvement over using a collection of numbered variables to hold related items of data; e.g. you might have a list of three credit card expenses named:
debit1, debit2 and debit3.
Using the structure of a vector we would re-write this as: (note that the indices go from 0 to 2, i.e. we start counting from 0).
debit[0], debit[1] and debit[2]
The great advantage of this is that we can then use the index to access different expenses: we do this by referring to debit[i], and allowing i to take on the values 0, 1 and 2 as required.
Here is a "syntax template" of a vector declaration:
vector<DataType> vector_name(int_expression);In this syntax template, DataType describes what is stored in each "slot" of the vector. The "base type" of a vector may be any type: integer, double, Point, Circle, etc.
int_expression indicates the size of the vector declared, i.e. it specifies the number of elements in the array. It must evaluate to a positive integer value. If the value of the vector size is n, the range of the index values is 0 to n-1 (this is because we always start counting at 0 rather than 1).
For example, the declaration
vector<int> score(50);Creates the vector score which has 50 elements, each capable of holding one int value.
In other words, the vector score has a total of 50 elements, labeled score[0] through score[49], all of type int.
You will need to include the library, vector, that uses the std namespace as we have done with iostream and string in previous labs.
#include <vector> using namespace std;
Write a program that declares a vector of integers with 10 components. Use a for loop to read in 10 integers from user input, and store them in this vector. Use the size() member function in the loop condition expression rather than the literal 10.
Then use another loop to print out the 10 elements. For example:
User input: 3 22 45 32 55 90 3333 43 75 20
Your output: 3 22 45 32 55 90 3333 43 75 20
Vectors can store any data type as its base type. Write a program that declares a vector of Points with 6 components. Again use a for loop to ask the user to click on the graphics window 6(actually size()) times, storing each Point clicked in the vector of Points. Also, output each Point along with it's index number. So, the first click will be labeled 0, the second will be labeled 1, etc.
Your window should look something like this after this first step:
. .
0 3
.
2 . .
1 . 4
5
Write this part and test it before moving on to the next part.
Time to connect the dots. Ask the user to pick 2 dots by using the get_int() member function, then draw a line connecting the dots.
So, if the user chooses the dots 1 and 4, the window should look something like this:
. .
0 3
.
2 .__________.
1 . 4
5
Let's expand our program to ask the user if they want to draw another line. We can use a do-while loop that asks them for the 2 dots, clears the screen, draws a line between these 2 dots, and then asks if they want to draw another line. If they reply with a "yes", we should repeat the loop. In other words, ask them for 2 dots, clear the screen, redraw the dots (and their labels), and redraw any lines that were previously drawn.
The latter step brings up an interesting problem. How do we keep track of which lines we have already drawn? Let's store them in their own vector, a vector of Lines.
But wait! We don't know how many Lines we will need to store. No problem! We can declare a vector with no size (or a size of 0).
vector<Line> lines;
Now we have a vector of size 0. In order to work with this we will have to use the push_back() member function to add lines one at a time to the vector. This member function takes one argument of the base type (Line in our case), creates a new element at the end of the vector and sets it equal to the value of the argument passed in, increasing the vector's size by one.
To add the Line from the example above to the vector, lines, the code might look something like:
lines.push_back(Line(dots[dot1], dots[dot2]));
dots is the vector of Points. dot1 and dot2 are the numbers of the dots to be connected. In the example of above, they would hold the values 1 and 4.
Now, we can safely clear the screen and redraw all the Points and all the Lines so far, since they are stored in vectors. We just design the for loop to start at element 0 and end at element size() - 1:
for (int i = 0; i < lines.size(); i++)
We can redraw the lines this way no matter how many Lines are stored in the vector.
Ok, now let's see if you have designed this program well. Change the program to accept 10 points(dots) instead of 6. If you have designed your loops correctly, using the size() member function, you should only need to change the declaration of the Point vector to be of size 10 instead of 6.
Write a predicate function that takes 2 int vectors as parameters and returns true if they are exactly equivalent and false otherwise. Equivalent means they have the exact same values in the exact same position. That also means they must be the same size.
The value of these vectors should not be changed, so they should be passed in by value.
Write a program to test your function.
Write a function that takes a vector of integers and 2 indices as arguments. The function should swap the values in those positions in the vector. For example, if the vector had the values (in this order):
10 3 8 9 4 0
and the index values passed in were 0 and 3, the vector would store the values in the following order when the function returned.
9 3 8 10 4 0
Note the 10(element 0) and 9(element 3) were swapped.
Because we are changing the vector passed in, it should be passed in by reference.
Write a program to test your function.
Write a function that takes 2 vectors of integers and merges them into one vector. The function should return this vector. You should not change the vectors passed in, so they should be passed in by value. The function should have a return type of integer vector.
The function should create a new vector. Then, it should alternate getting values from the 2 vectors, taking from the first, then taking from the second, then taking from the first, etc. You should not assume the vectors have the same size. So, one of the vectors may run out of values before the other.
When all the values have been merged into the new vector, return that vector using a return statement.
Write a program to test your function.