STL Vector C++

Can anyone please help me with this question??????

Create two vectors. Fill both vectors with 100 random integers in the range [-1000, +1000],in random order. After that, do the following:

a) Create a third vector, which contains the sums of the integers in the same positions as in the two vectors created earlier.
b) Print out the number of positive integers and the number of negative integers in the third
vector
Last edited on
Have you done the first part yet:
Create two vectors. Fill both vectors with 100 random integers in the range [-1000, +1000],in random order. After that, do the following:

?
if not:
http://www.cplusplus.com/reference/vector/vector/
http://www.cplusplus.com/reference/cstdlib/rand/

Do that first.
Thanks
Last edited on
I don't see anywhere where you actually add a value to any of your vectors. read that first link on vectors.

and can you use code tags please?
The code is not complete yet what I have done is only for the first part but I'm not sure whether the random values is stored in the vector1 and vector2 or not. For the adding part I'm not sure how to do it.
read that first link on vectors.

http://www.cplusplus.com/reference/vector/vector/at/
http://www.cplusplus.com/reference/vector/vector/push_back/

edit:
be careful of stuff like this:
a=-(rand() % 1001);

maybe use this instead:
a=-1 * (rand() % 1001);

also, you could use one for loop to populate both of the original vectors.
Last edited on
I have try the push_back function but still there's an error or the loop doesn't stop. Can anyone show me the coding please.
still there's an error or the loop doesn't stop

which one is it?
just try and get one vector populating with values to begin with. you might be better off declaring like this:
vector <int> vector1;
and then using push_back 100 times in your for loop.
e.g.
1
2
3
4
5
6
7
8
9
vector <int> vector1;
const int numElements = 100;

for(int i=0;i<numElements;i++)
{
    randomNum = // generate your number
    vector1.push_back(randomNum);
}
Last edited on
Can anyone just show me how to fill my vector1&vector2 with random value???
According to your code above:
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
cout<<"Element in vector1:-------------------------- "<<endl;

for(int i=0 ;i <vector1.size() ;i++ )
{
b=rand() % 2;
if (b == 0)
{
vector1[i]=rand() % 1001; // Note
}
else
{
vector1[i]=-(rand() % 1001); // Note
}

cout <<"Integer number for vector1 at location "<<i+1<<" is : "<<vector1[i]<<endl; // Note

}

cout<<endl<<"Element in vector2:------------------------- "<<endl<<endl;

for(int i=0 ;i <vector2.size() ;i++ )
{
b=rand() % 2;
if (b == 0)
{
vector2[i]=rand() % 1001; // Note
}
else
{
vector2[i]=-(rand() % 1001); // Note
}

cout <<"Integer number for vector1 at location "<<i+1<<" is : "<<vector2[i]<<endl; // Note 
Thanks for the help on storing the random values in my vector. Now I need to calculate the sum of my vector1 and vector2 according to their positions or index number???
1
2
3
4
5
6
7
8
9
10
11
12
vector <int> vector1;
const int numElements = 100;
int sum = 0;

for(int i=0;i<numElements;i++)
{
    randomNum = // generate your number
    sum+=randomNum;
    vector1.push_back(randomNum);
}

// outside of this loop 'sum' will give you the total sum. 
Last edited on
Thanks a lot mutexe.

Can anyone help me on how to Delete all the contents of the third vector. Replace its content with the content of the first vector, and then continue (add at the back) with the content of the second vector.
Last edited on
First try reading the info about vector (class members, etc) and see if you can work this out for yourself...

http://www.cplusplus.com/reference/vector/vector/

Andy
Last edited on
Maybe he'll read it now that two people have given him that link..
One can but hope!!
I know how to delete the content in the third vector but I don't know how to replace it with the value of vector 1 and vector 2 together in vector 3. Should I use the assign function but still I don't know how to put vector 1 and vector 2 value in vector 3. Can anyone please help me??
Last edited on
simple way would be:

1
2
3
4
5
6
7
8
for(int i=0 ;i <vector1.size() ;i++ )
{
   vector3.push_back(vector1.at(i));
}
for(int j=0 ;j <vector2.size() ;j++ )
{
   vector3.push_back(vector2.at(j));
}


I haven't checked the syntax as I've just written that on my phone.
If you're talking about this:

a) Create a third vector, which contains the sums of the integers in the same positions as in the two vectors created earlier.

it sounds like it prob means that the first element of vector3 is the sum of the first element of vector1 and the first element of vector2, etc.

Andy

Topic archived. No new replies allowed.