Lab 10. Vectors
Points (10 overall)
Collaboration policy:
You will be working in pairs during labs.
Pairs will be randomly selected and will be announced by the TA. Each week you will have a new random partner. You will discuss the programs with your
partner, but you will still be typing in your own code to show to the TA. You can help each other debug, give plenty of
suggestions and hints, **explain** why things work or don't work, etc.
To gain experience with
Vectors can store any data type as its base type. Write a program that declares a vector of Points with 6 components.
Write a function to fill the vector. You will use a for loop to ask the user to click on the graphics window 6 times (actually size()times), storing each Point clicked in the vector of Points. Let them see the location of the Points already chosen by outputting the 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 part:
. . 0 3. 2 . . 1 . 4 5
Write a main program that declares the vector and fills the vector. Test this code 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. Your program should have them always draw one line and then ask if they want to draw more (think about what loop is best for this situation). If they want to draw more lines, they should reply with a “yes”. After you get the 2 points for the line, clear the screen, then redraw the points 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). 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 redraw the Points and the Lines, you should use functions. Write a function that will output all the Points in the point vector. Write another function to output all the Lines in the line 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.