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.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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<int> 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

1
2
3
4
5
6
7
8
9
10
11
int num = 0; //instead of line 11 in your code

//Use following lines instead of line 15, 16 in your code
vector<int> 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.