- #1
Kernul
- 211
- 7
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], ...
The code I did is this:
But I'm thinking there is something wrong here. I'd really appreciate your help.
My teacher only let us use these libraries: iostream, fstream, cstdlib, cmath and cstring. So I can't use the library "vector".
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], ...
The code I did is this:
Code:
#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.\n";
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<<"]";
}
//Terminating the program:
return 0;
}
But I'm thinking there is something wrong here. I'd really appreciate your help.
My teacher only let us use these libraries: iostream, fstream, cstdlib, cmath and cstring. So I can't use the library "vector".