### I'm completely lost with arrays/vectors!!

I need to manipulate this program to 1)prompt the user for an array input using the max of 4, 2)use and display the results reported by the vector
classâ€™s capacity() and max_size() functions, and 3)to use the random_shuffle algorithm. The last one I think I can get. But the first two are giving me a hard time.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] ={136, 122, 109, 146}; int i; // create a vector of strings using the n[] array vector partnums(n, n + NUMELS); cout << "\nThe vector initially has a size of " << int(partnums.size()) << ",\n and contains the elements:\n"; for (i = 0; i < int(partnums.size()); i++) cout << partnums[i] << " "; // modify the element at position 4 (i.e. index = 3) in the vector partnums[3] = 144; cout << "\n\nAfter replacing the fourth element, the vector has a size of " << int(partnums.size()) << ",\n and contains the elements:\n"; for (i = 0; i < int(partnums.size()); i++) cout << partnums[i] << " "; // insert an element into the vector at position 2 (i.e. index = 1) partnums.insert(partnums.begin()+1, 142); cout << "\n\nAfter inserting an element into the second position," << "\n the vector has a size of " << int(partnums.size()) << "," << " and contains the elements:\n"; for (i = 0; i < int(partnums.size()); i++) cout << partnums[i] << " "; // add an element to the end of the vector partnums.push_back(157); cout << "\n\nAfter adding an element to the end of the list," << "\n the vector has a size of " << int(partnums.size()) << "," << " and contains the elements:\n"; for (i = 0; i < int(partnums.size()); i++) cout << partnums[i] << " "; // sort the vector sort(partnums.begin(), partnums.end()); cout << "\n\nAfter sorting, the vector's elements are:\n"; for (i = 0; i < int(partnums.size()); i++) cout << partnums[i] << " "; cout << endl; return 0; }``````
Last edited on
for 1) do

 ``1234567891011`` ``````int num = 0; //instead of line 11 in your code //Use following lines instead of line 15, 16 in your code vector partnums(4); //declares vector of ints with size 4 cout << enter four numbers << endl; for( i = 0; i < 4; i++ ) { cin >> num; partnum.push_back(num); }``````

for 2) you already know how to use vector elements and output, so I did not get what the problem is
Topic archived. No new replies allowed.