Printing vectors using cout in a function

Jul 11, 2018 at 4:02pm
I am trying to write a simple program to add 2 vectors and print the output using 2 different functions. The program intends to add vectors of different sizes and return a vector equal to the size of the larger vector. Although I do not have any compilation errors, the program returns without any output.

As a beginner, I'm not able to identify where I am going wrong. I tried avoiding the use of pointers to make it as easy as possible.

Thank you in advance for your help

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
45
46
 #include <iostream>
#include<vector>

using namespace std;

vector<int> addvectors(vector<int> a,vector<int> b);
void printvector(vector<int> a1);

int main()
{
    vector<int> x(5,1);
    vector<int> y(10,2);
    vector<int> z;
    z=addvectors(x,y);
    printvector(z);
    return 0;


}

void printvector(vector<int> a1){

for(int i=0;i<a1.size();i++){

    cout<<a1.at(i)<<' ';
}

}

vector<int> addvectors(vector<int> a,vector<int> b){

vector<int> c;
// setting the size of the output vector
if (a.size()>b.size()){b.resize(a.size(),0);}
else{a.resize(b.size(),0);}


//addition

for (int i=0;i<a.size();i++){
    c[i]=a[i]+b[i];

}
return c;

}
Jul 11, 2018 at 4:17pm
You're not setting the size of c anywhere.
1
2
3
4
5
6
7
8
if (a.size()>b.size()) {
    b.resize(a.size());
    c.resize(a.size());
}
else {
    a.resize(b.size());
    c.resize(b.size());
}
Last edited on Jul 11, 2018 at 4:17pm
Jul 11, 2018 at 4:25pm
Thanks a lot. This worked. I have a couple of doubts though

1. I thought you can initialize a vector and dynamically allot elements to it. Like push back or insert.Does the operator [] dynamically allot elements to a vector if size is not mentioned?

For example if declare a vector a as follows - vector<int> a.

And try to access a[5], will it return a garbage value or a compile error?

2. Why didn't the original program i wrote return a compilation error?

Thank you!
Last edited on Jul 11, 2018 at 4:35pm
Jul 11, 2018 at 4:47pm
Yes, you can dynamically append elements to a vector with push_back, but using [] doesn't automatically do that. It is only used to access elements that already exist. But you could do it like this:
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
#include <iostream>
#include <vector>
#include <algorithm>  // for max()

typedef std::vector<int> Vint;

Vint addvectors(const Vint &a, const Vint &b);
void printvector(const Vint &a);

int main()
{
    Vint x(5, 1), y(10, 2);
    Vint z = addvectors(x, y);
    printvector(z);
    return 0;
}

void printvector(const Vint &a) {
    for(size_t i = 0; i < a.size(); i++)
        std::cout << a[i] << ' ';
    std::cout << '\n';
}

Vint addvectors(const Vint &a, const Vint &b) {
    Vint c;
    size_t size = std::max(a.size(), b.size());
    for (size_t i = 0; i < size; i++) {
        int aval = i < a.size() ? a[i] : 0;
        int bval = i < b.size() ? b[i] : 0;
        c.push_back(aval + bval);
    }
    return c;
}

Jul 12, 2018 at 8:34am
If your original arrays were the same size then it's quite neat with valarrays.
Sadly, the result is undefined if they aren't the same size.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <valarray>
using namespace std;

template <typename T> ostream &operator <<( ostream &out, const valarray<T> &V )
{
   for ( auto e : V ) out << e << " ";
   return out;
}


int main()
{
    valarray<int> x(1,5);         cout << "x: " << x << '\n';
    valarray<int> y(2,5);         cout << "y: " << y << '\n';
    valarray<int> z = x + y;      cout << "z: " << z << '\n';
}
x: 1 1 1 1 1 
y: 2 2 2 2 2 
z: 3 3 3 3 3 




Best that I could come up with if the original arrays aren't the same size.
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
#include <iostream>
#include <valarray>
#include <algorithm>
using namespace std;

template <typename T> ostream &operator <<( ostream &out, const valarray<T> &V )
{
   for ( auto e : V ) out << e << " ";
   return out;
}

template <typename T> valarray<T> resize( const valarray<T> &V, int size ) 
{
   valarray<T> result( 0, size );
   for ( int i = 0; i < V.size() && i < size; i++ ) result[i] = V[i];
   return result;
}


int main()
{
    valarray<int> x(1,5);
    valarray<int> y(2,10);

    int size = max( x.size(), y.size() );
    valarray<int> z = resize( x, size ) + resize( y, size );

    cout << "x: " << x << '\n';
    cout << "y: " << y << '\n';
    cout << "z: " << z << '\n';
}
x: 1 1 1 1 1 
y: 2 2 2 2 2 2 2 2 2 2 
z: 3 3 3 3 3 2 2 2 2 2
Last edited on Jul 12, 2018 at 8:59am
Topic archived. No new replies allowed.