Complicated Vector Exercise

Feb 17, 2015 at 1:55pm
The exercise is this:
Assigned two vectors (V1 and V2) of integers. Check if within one of the two vectors (VY) there is a sequence of values in consecutive positions such that every element is included between two successive elements of a sequence of elements of the other vector (VX):
VX [i] < VY [j] < VX [i + 1] and VX [i + 1] < VY [j + 1] < VX [i + 2]
Note that, the relationship in order to exist, the sequence of elements VY must have at least two elements and the sequence of elements VX must contain double elements of VY. If the check is positive, generate and print the vector obtained by melting the two successions respecting the found relation:
..., VX [i], VY [j], VX [i + 1], VY [j + 1], VX [i + 2], ...

I don't want you to write the code of this exercise. I just want to be "guided" through every point of the exercise, like:
"First do this loop. So, what's the code for this loop? What do you need?" etc.

I'd really appriciate your help.

EDIT: Our teacher only let us use these libraries: iostream, fstream, cstdlib, cmath and cstring.
Last edited on Feb 17, 2015 at 2:07pm
Feb 17, 2015 at 4:45pm
Not sure I understood the exercise. But if you want to check

VX [i] < VY [j] < VX [i + 1] and VX [i + 1] < VY [j + 1] < VX [i + 2]

for all the elements in the vectors then make two loops, one for the i and one for the j. Just make sure every loop goes over all the legal values of i and j (and skip problematic ones like the last values).

You could alternatively try a recursion but I think two loops will do, and a 'for' loop might be the easiest.
Feb 17, 2015 at 5:12pm
What you're saying is maybe something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	//Initialization:
	const int N = 4;
	const int M = 8;
	int vec1[N] = {1, 3, 5, 7};
	int vec2[M] = {0, 2, 4, 6, 8, 10, 12, 14};
	bool condition = false;
	
	//Procedure:
	for (int i = 0; i<N; i++) {
		for (int j = 0; j<M; j++) {
			if (vec1[i]>vec2[j] && vec1[i]<vec2[j+1]) {
				condition = true;
			}
		}
	}
Feb 19, 2015 at 8:55am
I've been able to do the check. This is an example:
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
#include <iostream>

using namespace std;

int main () {
	//Initialization:
	const int N = 4;
	const int M = 8;
	const int K = 12;
	int vec1[N] = {1, 5, 9, 13};
	int vec2[M] = {0, 2, 4, 6, 8, 10, 12, 14};
	int vec3[K];
	bool condition1 = false;
	bool condition2 = false;
	
	//Procedure:
	//Checking:
	if (N >= 2 && M>=N*2) {
		for (int i = 0; i<N; i++) {
			for (int j = 0; j<M; j++) {
				if (vec1[i]>vec2[j] && vec1[i]<vec2[j+1]) {
					condition1 = true;
					break;
				}
			}
		}
		for (int i = 0; i<M; i++) {
			for (int j = 0; j<N; j++) {
				if (vec2[i]>vec1[j] && vec2[i]<vec1[j+1]) {
					condition2 = true;
					break;
				}
			}
		}
	}
	
	//Output:
	if(condition1 == true && condition2 == true) {
		cout<<"Checked.";
	}
	
	//Termination:
	return 0;
}


Now I have to melt together the two vectors respecting the relation found. How should I do that? With another for loop?
Feb 20, 2015 at 12:11pm
Okay, after a looong time spent on this exercise I finally found a solution:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>

using namespace std;

int main () {
	//Initialization:
	const int N = 4;
	const int M = 8;
	int vec1[N] = {1, 5, 9, 13};
	int vec2[M] = {0, 2, 4, 6, 8, 10, 12, 14};
	int vec3[N+M];
	bool condition = false;
	
	//Procedure:
	//Checking:
	if (N >= 2 && M >= N*2) {
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				if (vec1[i]>vec2[j] && vec1[i]<vec2[j+1]) {
					condition = true;
					break;
				}
				else {
					condition = false;
				}
			}
		}
	}
	
	//Merging vectors:
	if (condition == true) {
		cout<<"Checked: Condition true."
		int i = 0, j = 0, k = 0;
		while (i < N && j < M) {
			if (vec1[i] <= vec2[j]) {
				vec3[k] = vec1[i];
				i++;
			}
			else {
				vec3[k] = vec2[j];
				j++;
			}
			k++;
		}
		if (i < N) {
			for (int p = i; p < N; p++) {
				vec3[k] = vec1[p];
				k++;
			}
		}
		else {
			for (int p = j; p < M; p++) {
				vec3[k] = vec2[p];
				k++;
			}
		}
	}
	else {
		cout<<"Checked: Condition false.";
	}
	
	//Output:
	if (condition == true) {
		cout<<"[";
		for (int l = 0; l<N+M; l++) {
			cout<<vec3[l]<<" ";
		}
		cout<<"]";
	}
	
	//Termination:
	return 0;
}


Please, tell me if there is even the slightest mistake.
Feb 20, 2015 at 12:52pm
Unfortunately, I think you've made a big mistake. From the problem description:
Check if within one of the two vectors (VY) there is a sequence of values in consecutive positions such that every element is included between two successive elements

Your code determines if there is a single element that fits the criteria. The problem is looking for a larger sequence.

You may want to check with the professor about this input:
vx = {1 2 19 21 3 4 9 10 5 6 }
vy = { 10 20}

With the current problem wording, I believe that this sequence works. I suspect that the professor intended to say that the two vectors are sorted, which makes the problem a whole lot easier to solve.

Also ask what if there are two sequences that meet the criteria?
Feb 21, 2015 at 12:38pm
Oh, I got it. I'll go ask him about that this monday and let you know.
Feb 21, 2015 at 1:17pm
vx = 5,10,15,20
vy = 3,6,9,12,15,18

vxy = {5,6,10} , {5,9,10,12,15}, .... {15,18,20}

do you need to find all of them? or the longest one ?

Last edited on Feb 21, 2015 at 1:18pm
Feb 21, 2015 at 2:34pm
I think you have to output the merged vector. It says that in the description.
So it should be something like:
{3,5,6,9,10,12,15,15,18,20}
Feb 21, 2015 at 5:52pm
no, you need not merge the whole length. merge as long as bigger elements found, taking elements alternately from vx and vy.

so 3,5,6 now you cannot input 9, because its vx's turn. i.e 10, then from vy you cannot 9, because !(9>10)
Feb 24, 2015 at 1:59pm
It seems that the professor is on a "long journey"...
Anyway, let's just say that the two vectors must be sorted.
Your code determines if there is a single element that fits the criteria. The problem is looking for a larger sequence.

How do I check if every element fits the criteria? Should I do multiple condition variables? (okay, this sounds really bad)

no, you need not merge the whole length. merge as long as bigger elements found, taking elements alternately from vx and vy.

But your output and the example output (..., VX [i], VY [j], VX [i + 1], VY [j + 1], VX [i + 2], ...) are not the same. You print multiple vectors while in the example it prints one entire vector that fits the criteria.

Damn, this exercise is making me crazy...
Feb 24, 2015 at 3:28pm
its your post:
The exercise is this:
Assigned two vectors (V1 and V2) of integers. Check if within one of the two vectors (VY) there is a sequence of values in consecutive positions such that every element is included between two successive elements of a sequence of elements of the other vector (VX):
VX [i] < VY [j] < VX [i + 1] and VX [i + 1] < VY [j + 1] < VX [i + 2]
Note that, the relationship in order to exist, the sequence of elements VY must have at least two elements and the sequence of elements VX must contain double elements of VY. If the check is positive, generate and print the vector obtained by melting the two successions respecting the found relation:
..., VX [i], VY [j], VX [i + 1], VY [j + 1], VX [i + 2], ...

I don't want you to write the code of this exercise. I just want to be "guided" through every point of the exercise, like:
"First do this loop. So, what's the code for this loop? What do you need?" etc.

I'd really appriciate your help.

EDIT: Our teacher only let us use these libraries: iostream, fstream, cstdlib, cmath and cstring.


apparently you also need #include <vector>
you wrote "V1" and "V2" only once. what's the relation vx and vy has with V1 and V2 ?

......
But your output and the example output (..., VX [i], VY [j], VX [i + 1], VY [j + 1], VX [i + 2], ...) are not the same. You print multiple vectors while in the example it prints one entire vector that fits the criteria.

how they are not same?
from below: (?)
vx = 5,10,15,20
vy = 3,6,9,12,15,18
vxy = 3,5,6,10

Feb 24, 2015 at 3:31pm
Wait, every single piece of the example output is a number, right? I mean, for example:
VX [i] = number
VY [j] = another number
VX [i + 1] = another number
etc.
Am I right? Because if it isn't I think I didn't understand the exercise well.
Topic archived. No new replies allowed.