Vector Functions

I am trying to write a void function that outputs my vectors into my main. I have the following code for the function, however it says I have an error on my coutlines and wants me to use "cout<< &V1<< ends;", can someone please explain this to me please.

I also have another function that sets random numbers between -10 and 10 of the vectors. When I run everything together, I am not getting the result I want.


void output(vector <int> V1, vector<int> V2)
{


int size1;
int size2;

cout<< "How big do you want V1 to be? ";
cin>> size1;

cout<< "How big do you want V2 to be? ";
cin>> size2;

V1.resize(size1);
V2.resize(size2);

cout<< V1<<endl;

cout<< V2<< endl;


}


void fill_vector(vector<int> V1, vector<int> V2)
{


srand((int)(time(0)));

for (int i=0; i< V1.size();++i)
{
V1[i]=rand()% 20 +(-10);

cout<< V1[i] << " ";

}

cout<< endl;

for (int i=0; i< V2.size(); ++i)
{
V2[i]=rand()%20+ (-10);

cout<< V1[i]<< " ";
}

cout<< endl;


}

int main()
{
vector<int> V1(0);
vector<int> V2(0);


output( V1, V2);

fill_vector(V1, V2);


cout<< endl;


return 0;
}
Last edited on
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
43
44
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

void output( const std::vector<int>& vec )
{
    std::cout << "[ " ;
    for( int v : vec ) std::cout << v << ' ' ;
    std::cout << "]\n" ;
}

void output( const std::vector<int>& v1, const std::vector<int>& v2 )
{
    output(v1) ;
    output(v2) ;
}

void fill_vec( std::vector<int>& vec )
{ for( int& v : vec ) v = std::rand() % 21 - 10 ; } // values in [-10,10]

void fill_vec( std::vector<int>& v1, std::vector<int>& v2 )
{
    fill_vec(v1) ;
    fill_vec(v2) ;
}

int main()
{
    std::srand( std::time(nullptr) ) ;

    std::size_t sz ;

    std::cout << "How big do you want V1 to be? " ;
    std::cin >> sz ;
    std::vector<int> v1(sz) ;

    std::cout << "How big do you want V2 to be? " ;
    std::cin >> sz ;
    std::vector<int> v2(sz) ;

    fill_vec( v1, v2 ) ;
    output( v1, v2 ) ;
}
Last edited on
Topic archived. No new replies allowed.