vector exercies

Hi all,

Kanul asked a question at 17 Feb about a school project he had to do. I looked at it and worked two evenings on it. I don't think my code is very efficient and it can have some improvements. Can you help me with that?

That topic of Kanul is closed now, you can read it here.

http://www.cplusplus.com/forum/beginner/157016/

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

void Print_Vector(const std::vector<int> Vector, int start, int stop) {
	std::cout << "The (part of) vector is " << std::endl;
	for (std::vector<int>::const_iterator i = Vector.begin() + start; i != Vector.begin() + stop;++i) {
		std::cout << *i << " ";
	}
	std::cout << std::endl;
}

int main()
{
	std::vector<int> V1;
	std::vector<int> V2;
	std::vector<int> V3;

	V1.clear();
	V2.clear();
	V3.clear();

	V1 = { 1, 5, 9, 12, 16, 25, 38, 44, 45, 48, 47, 65, 71, 83 };
	V2 = { 3, 8, 11, 17, 23, 35, 43 };
	
	//fill one vector in total is sequence of v1(0); v2(0); v1(1); v2(1) etc. 
	//In the new vector the condition:	v1(0) < v2(0) ; v2(0) < v1(1) etc...
	//can be checked.
	for (std::vector<int>::size_type i=0; i != V1.size(); ++i) {
		V3.push_back(V1.at(i));
		if (i < V2.size()) {
			V3.push_back(V2.at(i));
		}
	}

	//loop trough the vector, start with the second element. If the elemtens is 
	//smaller than the previous element, than print the part of the vector.
	int n{ 0 }, m{ 0 };
	for (std::vector<int>::size_type i = 1; i != V3.size(); ++i) {
		n = i - 1;
		if (V3.at(i) < V3.at(n)) {
			Print_Vector(V3, m, n + 1);
			m = i;
		}
		if (i+1 == V3.size()) {
			Print_Vector(V3, m, i+1);
		}
	}

	std::cin.get();
	return 0;
}
Last edited on
Our teacher only let us use these libraries: iostream, fstream, cstdlib, cmath and cstring

above from Kernul's post 2 years ago, do you still want to remain bound by this restriction?
Good point Gunner. I did not see that restriction. As you see I have used <vector> and not an array as I would have done if <vector> was not allowed.
Topic archived. No new replies allowed.