|I am taking an elective course in C++ programming, but the instructor gave us ONE week to learn c++, now he is into algorithms and I am so lost. Can anyone help me with the following homework? I have been studying all week and this is due tomorrow at 3:00!! I really appreciate anyone's help!! |
In this homework, you will create a vector container that holds 4 elements. Follow the instruction carefully to create the vector program.
1. Include both <iostream.h> and <vector>. Add std namespace statement.
2. Begin main( ) function. Depending on the software you are using, you may use either void main() or int main(). When you use int main(), don’t forget return 0; statement at the end of main( ) function.
3. Create an integer instance (blank object) of class vector. You can use any object name for this instance – for example, intList.
4. Create an integer variable i. This variable will be used as a counter variable in the for-loop statement.
5. Add the following vector elements using .push_back( ) method of vector object four times: 13, 75, 28, 35
6. Using cout statement, print “Line 1: List Elements: ”. Quotation marks show what to print, so do not print the quotation marks themselves.
7. Using for-loop, continue to print each element of the vector object in order to the same line. Between each number, add two blank spaces. After for-loop, move to the next line using endl of cout statement.
8. Using for-loop, multiply each element of the vector object by two.
9. Repeat instruction #6 and #7, in this case, use “Line 2” instead of “Line 1”.
10. Create an instance of vector iterator. Because iterator is a member of the class vector, you must specify vector class name, container element type, and the scope resolution operator. It will look like vector <int>::iterator object_name;
You can use any name for the iterator object, but I suggest listIt.
11. Using the iterator as a counter variable (instead of i), repeat instruction #6 and #7 (use “Line 3”). The starting value of iterator should be obtained from .begin( ) method of the vector object, and the end value from .end( ) function. The iterator object is a pointer, so to print the element, use *listIt instead of intList[listIt].
12. Reset the iterator to the beginning of the vector object.
13. Move the iterator forward twice, so that it will point to the third element.
14. Use the .insert( ) method of the vector object to insert a number, 88 at the position specified by the iterator.
15. Using “Line 4”, repeat instruction #11.
16. Close the main( ) function. If you used int main( ), do not forget to add return 0; statement at the end.
17. Compile and submit the source file, the execution file, and the hardcopy of the source code.