In this lab you will experiment with the basics of making and using vectors and then implement vectors in a simple algorithm.
The main topics you may need in this lab are:
Your job is to write a program in the file Vector.cpp that:
- First gets the "Input size of vector" from the user
- Creates a vector of that size
- Reads the values for the vector elements from the user
- Store the values in the vector, in the order they are read in.
- Output the elements of the vector, in ascending, then decending order, by sorting them using the function you make in Part II.
Vector's use the vector library be sure to include it: #include <vector>
When you create a vector, you must specify what type of data the vector will contain and how many of those elements the vector will have. The basic syntax for declaring a vector is:
vector<type> name (size);
So if you wanted to create a vector named myVector to hold 50 doubles you would type:
vector<double> myVector (50);
When you create a vector the values of that vector's elements are undefined untill you store a value in it. If you want all of a vector's elements to start off with an initial certain initial value you can specify an initial_value when you create the vector, like so:
vector<type> name (size,initial_value);So to create a vector like in the example above and initialize all of it's elements to zero, you would type:
vector<double> myVector (50,0);
To access the value of an element in a vector you use the [] operator. Be aware that the indices start at zero, so to access the first (or rather "zeroeth") element in a vector you must type, for the above example: myVector[0].
For the second part of this lab you will write a function that will
sort the elements of your vector from part one in ascending (counting
up) or descending (counting down) order.. The algorithm that you will
use to sort your vector with be an implementation of the Bubblesort
method. The function should have the following prototype:
vector<int> bubble_sort (vector<int> input, bool ascending);
This function should return a new vector, that is a sorted version of input in either ascending or descending order depending on the value of ascending. (i.e. if ascending == false then sort in descending order)
The sorting will be done using the BubbleSort algorithm, one of the simplist algorithms to implement.
Implement the algorithm from the following pseudocode:
- Step through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
- Starting back at the begining of the list, step through again.
- Continue to do this untill you make a pass through the list that requires no swaps.
Once you've done all of this, your program should be done. Make sure that your program runs exactly as the vector_example program provided.
If your input looks like this
10
0
1
3
4
6
8
7
9
2
5
Your ouput should look like this:
Input
0 1 3 4 6 8 7 9 2 5
Ascending
0 1 2 3 4 5 6 7 8 9
Decending
9 8 7 6 5 4 3 2 1 0
As you may have noticed, this lab does not use your robot. This means two things:
- This lab should not take you as long becaue you can complete it via putty or a shell. This does not mean you should wait until the last minute. Lab 5 and 6 are more difficult, and lab 5 is already posted. It is suggested you use your time wisely and begin work on lab5 as soon as you can.
- Your robot won't be used in grading of this lab. The grading will be more harsh because of this. You are provided with a sample executable (vector_example) and a few test inputs. Make sure your lab output is identical. Make use of diff to compare your output with the example. This is how you will be graded.