Homework assistance with Vectors.

First of all hello. I am extremely new to C++ and am trying to learn through a class. I have a problem with one of my assignments and need advice. Here is the problem:

Create a vector container that holds 4 elements. Follow the instruction carefully to create the vector program.

1. Create an integer instance (blank object) of class vector. You can use any object name for this instance - for example, intList.

2. Add the following vector elements using .push_back( ) method ==> 13, 75, 28, 35

3. Display "Line 1: List Elements: ". And print each element of the vector object at the same line. Between each number, add two blank spaces. Then, move to the next line.

4. Using for-loop, multiply each element of the vector object by two. Vector must contain new values

5. Repeat instruction #3, in this case, use "Line 2" instead of "Line 1".

6. 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; for example ==> vector <int>::iterator object_name;

You can use any name for the iterator object, but I suggest listIt.

7. Using the iterator as a counter variable (instead of i), repeat instruction #3 (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].

8. Reset the iterator to the beginning of the vector object.

9. Move the iterator forward twice, so that it will point to the third element.

10. Use the .insert( ) method of the vector object to insert a number, 88 at the position specified by the iterator.

11. Using "Line 4", repeat instruction #7.


Now, here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector <int> intList;
int i;
intList.push_back(13);
intList.push_back(75);
intList.push_back(28);
intList.push_back(35);
cout <<"Line 1: List Elements: ";
for (i=0; i <4; i++)
cout << intList[i] << " ";
cout <<endl;

for (i=0; i <4; i++)
intList[i] *=2;
cout <<"Line 2: List Elements: ";
for (i=0; i <4; i++)
cout << intList [i] << " ";
cout << endl;
vector <int>::iterator ListIt;
cout << "Line 3: List Elements: ";
for (ListIt = intList.begin();
ListIt != intList.end();
++ListIt);
cout <<*ListIt << " ";
cout << endl;
ListIt = intList.begin();
++ListIt;
++ListIt;
intList.insert(ListIt, 88);
cout << "Line 4: List Elements: ";
for (ListIt = intList.begin();
ListIt != intList.end();
++ListIt)
cout <<*ListIt << " ";
cout << endl;
system ("pause");
return 0;
}


Something doesn't seem right, any help is appreciated.

Thanks for any help.

Last edited on
In future please use Code tags [ Code ] [ /Code ] around your code to make it easier for people to read/understand.

While quickly looking through it the only error I see is you have an extra ; at the end of your for loop that is stopping it from running. If that doesn't solve it edit your post and put code tags and I may be able to help more.

 
for (ListIt = intList.begin();ListIt != intList.end();++ListIt);  // Remove the last ; 
Wow, sorry about the [ Code ] issue. But I think you were correct. It runs now. Thank you!!!
Topic archived. No new replies allowed.