
Suzanne O. answered 10/17/19
International Experience and Multiple State Certifications
Interesting. Vector is not a standard construct of C++. So you have a little extra to include.
either:
#include <vector>
or
#include <bits/stdc++.h>
With that said, a vector is like an array, but it is elastic (dynamic). A vector will expand or contract depending on how many values are inserted or removed, whereas an array always has a fixed size (static) even when it is created dynamically.
Remember, there are always several different ways to achieve the same results in any programming language, from simple linear execution all the way to recursive functions. We will follow the KISS principle and go very basic to get the job done.
For the basic version of the program, you could declare all 15 myvect, rather than code to dynamically create these variable names. If you are a glutton for punishment, there are ways to dynamically generate variable names (see https://stackoverflow.com/questions/29257495/is-it-possible-to-use-dynamic-name-for-variables-in-c and http://www.cplusplus.com/forum/general/28821/).
From what I understand of the problem as you present it, you need user input for:
- the number of vectors to be input (nvect)
- the values to inert into each vector (y)
Your prompt would remind the user that the minimum is 2 vectors. When you have nvect, it could be used to control the for loop which loads each of the vectors (myvect1, myvect2, myvect3...myvect15). However, you might want to consider using a switch to load your myvects instead of nested for loops.
To load a value into the vector use:
myvectx.push_back(y); //where x is a number from 1 to 15 and y is the user input value
If I understand your statement of the problem, you then need to add these vectors up to get one long vector?
That would look something like this:
MyReallyBigVector.reserve(myvect1.size()+myvect2.size()...+myvectx.size());
MyReallyBigVector.insert(MyReallyBigVector.end(), myvect1.begin(), myvect1.end());
MyReallyBigVector.insert(MyReallyBigVector.end(), myvect2.begin(), myvect2.end());
...
MyReallyBigVector.insert(MyReallyBigVector.end(), myvectx.begin(), myvectx.end());
Once that MyReallyBigVector is loaded, spill its contents to the screen:
for
(
auto
x : myvector)
cout << x <<
" "
;
Believe it or not, this problem is easier using vectors instead of arrays because vectors are dynamic and can added to or subtracted from without redefining them.
Here are some resources for help with C++ and more specifically in handling arrays and vectors:
https://www.w3schools.com/cpp/default.asp
https://www.geeksforgeeks.org/bitsstdc-h-c/
https://stackoverflow.com/questions/12791266/c-concatenate-two-int-arrays-into-one-larger-array #11
https://www.programmingsimplified.com/cpp/source-code/add-arrays
https://www.geeksforgeeks.org/advantages-of-vector-over-array-in-c/
https://www.geeksforgeeks.org/how-does-a-vector-work-in-c/