c++ homework help - vector for loop

Ibm thinkpad / R31
February 1, 2009 at 14:34:25
Specs: Windows XP, 654
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.


See More: c++ homework help - vector for loop

Report •


#1
February 1, 2009 at 17:24:01
I feel it would be improper for anyone to undertake your homework for you, that way you will never fully understand what is expected of you.

You have not submitted even one line of your coding for us to consider.

I suggest you approach your tutor and explain your lack of understanding, perhaps others in your group have already done that.


Report •

#2
February 1, 2009 at 18:25:39
It was never my intention to ask someone to do my homework....I was asking for help. I should have been more specific by saying I was stuck at the for...loop part.

I was frustrated when I posted and am very sorry it came across that way.... Here is what i have so far....have a few errors I am trying to figure out. But I will figure them out myself. Thanks anyway for assuming the worse.


#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;
return 0;


Report •

#3
February 2, 2009 at 06:36:03
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.
You're only using one space. I don't know how uptight your professor is about such things.

vector <int>:iterator ListIt;
Should be vector <int>::iterator ListIt; (Notice the second ':')

Also, just below that, ListIt != intList.end()
should be: ListIt != intList.end(); (Forgot the ';')


Report •

Related Solutions


Ask Question