How do I then assign the necessary storage space for my a_array?and yeah it is crashing with the calculation and a_array in it. |
You can specify a size for a vector upfront using the
resize() method. Alternatively,
push_back() assigns the storage for the new element(s) as they're added - this is what you're already doing for
t_array and
v_array in your main function.
I'd recommend looking at the reference material for vectors on this site, if you're going to use them. It's an incredibly useful class, and understanding properly how to use them will be helpful.
Also for the pass by reference where exactly do I need the & |
It comes at the end of the type of the parameter - or, as some people prefer, at the start of the name:
1 2 3 4 5
|
vector<double>& a_array
// OR
vector<double> &a_array
|
Both are valid.
do I only need it for the a_array vector since the other two don't need to change? |
You only need it for
a_array, because that's the only one that changes. However, depending on the size of the vectors, it may be more efficient to do it for all of them, so you don't spend time making unnecessary copies as you pass in the vectors (although with newer compilers, this may be optimized away anyway, so it might not be worth worrying about).
If you do pass the other vectors in by reference, you can specify them as
const, to indicate - and enforce - that their contents aren't changed by the function, e.g.
const vector<double>& t_array