Is there something wrong with this code for checking and merging two vectors?

In summary: Merged: vec3[k] = vec1[p]"; } } //Printing: cout<<"In summary, vec3[k] = vec1[p] for k = "<<k<<endl; return 0; } }In summary, the code checks if within one vector 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. If the check is positive, the
  • #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:
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".
 
Physics news on Phys.org
  • #2
you can use MATLAB coder
Write in MATLAB and by MATLAB coder compile to C
 
  • #3
I don't know what you mean. I never used Matlab and I have to write everything in C/C++.
 
  • #5
Kernul said:
But I'm thinking there is something wrong here. I'd really appreciate your help.
You would stand a much better chance of getting help if you indicated what messages or output is causing you to think something is wrong. Diagnostic messages are often the key to enabling others to identify errors without needing to execute the code themselves.
 
  • #6
NascentOxygen said:
You would stand a much better chance of getting help if you indicated what messages or output is causing you to think something is wrong. Diagnostic messages are often the key to enabling others to identify errors without needing to execute the code themselves.

You're right.
The problem I found is when I change the numbers in the example. If I put a not-sorted vector, the condition is still true and print the merged vector.
It should say that the condition is false and print the message.
 
  • #8
Kernul said:
You're right.
The problem I found is when I change the numbers in the example. If I put a not-sorted vector, the condition is still true and print the merged vector.
It should say that the condition is false and print the message.
Isn't a precondition for your code that both vectors have their elements in sorted order? And that your goal is to meld (not "melt") the two lists into one sorted list?
 
  • #9
Mark44 said:
Isn't a precondition for your code that both vectors have their elements in sorted order? And that your goal is to meld (not "melt") the two lists into one sorted list?
Yes! That's why I am asking where is the problem.
My code meld them even if the condition is not true and print them wrong. I don't know how to fix this. It seems everything okay to me. I really need an outside view from someone else. :olduhh:
 
  • #10
Kernul said:
Yes! That's why I am asking where is the problem.
My code meld them even if the condition is not true and print them wrong. I don't know how to fix this. It seems everything okay to me. I really need an outside view from someone else. :olduhh:
If it's a precondition that both vectors be in sorted order, then you shouldn't be trying to merge two vectors that aren't already sorted. A precondition is an assumption that your code is making about the inputs. In other words, I don't see a problem. If your code works for any two vectors that are each in sorted order, don't fiddle with the vectors in such a way that you violate your assumed precondition.
 
  • #11
Mark44 said:
If it's a precondition that both vectors be in sorted order, then you shouldn't be trying to merge two vectors that aren't already sorted. A precondition is an assumption that your code is making about the inputs. In other words, I don't see a problem. If your code works for any two vectors that are each in sorted order, don't fiddle with the vectors in such a way that you violate your assumed precondition.
But the code has to check first if they are sorted or not and then merge them if they are sorted. The problem is that the code I wrote says that the vectors are sorted even if they are not.
 
  • #12
In the part of your code with the comment "//Checking" don't try to check both vectors simultaneously. Check each vector separately and set a boolean variable for each one. If both vectors are in sorted order, then you can proceed to the section with the comment "//Merging vectors:"
 
  • #13
But the exercise ask to 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). So I have to check that way and not simply check each vector separately. It's just a case that they are sorted.
 
  • #14
Kernul said:
You're right.
The problem I found is when I change the numbers in the example. If I put a not-sorted vector, the condition is still true and print the merged vector.
For which vector? Things are a bit confusing, as you said you are given two vectors, V1 and V2, and your subsequent work talks about VX and VY. In your code, you have vec1, vec2, and vec3.

When you change the vectors, what specific values are you talking about for vec1 and vec2?
Kernul said:
It should say that the condition is false and print the message.
 
  • #15
Mark44 said:
For which vector? Things are a bit confusing, as you said you are given two vectors, V1 and V2, and your subsequent work talks about VX and VY. In your code, you have vec1, vec2, and vec3.

When you change the vectors, what specific values are you talking about for vec1 and vec2?
For both the vectors. vec1 is VY and vec2 is VX. vec3 is just the vector I will use to merge the other two.
About the specific values, it can be everything. For example:
int vec1[N]={1,5,9,13};
int vec2[M]={0,2,6,4,8,10,12,14};
In this case the code should say "Checked: Condition false." because "9" is bigger than "6" and not less than "4" (VX [i + 1] < VY [j + 1] < VX [i + 2]), but instead it says that it is true and it print it anyway.
 
  • #16
Kernul said:
For both the vectors. vec1 is VY and vec2 is VX. vec3 is just the vector I will use to merge the other two.
About the specific values, it can be everything. For example:
int vec1[N]={1,5,9,13};
int vec2[M]={0,2,6,4,8,10,12,14};
In this case the code should say "Checked: Condition false." because "9" is bigger than "6" and not less than "4" (VX [i + 1] < VY [j + 1] < VX [i + 2]), but instead it says that it is true and it print it anyway.
What are the values of i and j when this happens?
 
  • #17
Mark44 said:
What are the values of i and j when this happens?
Both "2".
 
  • #18
I think that you need to check that the element of vec1 is between the two successive elements of vec2. What you're checking is that the order is like so:
vec2[j] < vec1 < vec2[j + 1]

You also need to check the other way; namely
vec2[j] > vec1 > vec2[j + 1]

Using the values you gave of i = 2 and j = 2, we have vec1[2] == 9, and vec2[2] == 6, vec2[3] == 4
Since 9 is not between 6 and 4, condition should be set to false.

At least, that's how I understand the problem you're having.
 
  • #19
Yes, that's how it should be. The problem is in the for loops and I don't know how to fix the right way.
Unfortunately the exercise doesn't give an example from which I can see how it should be done.
 
  • #20
Maybe this? I added the second conditional expression to your if statement.
C:
if ( (vec1[i] > vec2[j] && vec1[i] < vec2[j + 1]) ||
     (vec1[i] < vec2[j] && vec1[i] > vec2[j + 1])   ) {
    condition = true;
    break;
}
else {
     condition = false;
}

I'm just tweaking your code. I haven't tried to discern what your algorithm is, so can't comment on whether your approach is the right one.
 
  • #21
Mark44 said:
Maybe this? I added the second conditional expression to your if statement.
C:
if ( (vec1[i] > vec2[j] && vec1[i] < vec2[j + 1]) ||
     (vec1[i] < vec2[j] && vec1[i] > vec2[j + 1])   ) {
    condition = true;
    break;
}
else {
     condition = false;
}

I'm just tweaking your code. I haven't tried to discern what your algorithm is, so can't comment on whether your approach is the right one.
But I should check only the first one. If it is the second one then the condition the exercise is talking about is false and shouldn't print it.
Maybe I should simply do like this:
C:
if ( vec1[i] > vec2[j] && vec1[i] < vec2[j + 1] ) {
    condition = true;
    break;
}
else {
    cout<<"Checked: Condition false.";
    return 0;
}
But I think I started getting why it doesn't work.
Every time it checks from the second value of vec1, it starts checking from the first one and so it will always say condition false.
I should put j = something else that starts from the value in the last j position used instead of j = 0 every time it checks.
How should I do this?
 
  • #22
Kernul said:
But I should check only the first one. If it is the second one then the condition the exercise is talking about is false and shouldn't print it.
Maybe I should simply do like this:
C:
if ( vec1[i] > vec2[j] && vec1[i] < vec2[j + 1] ) {
    condition = true;
    break;
}
else {
    cout<<"Checked: Condition false.";
    return 0;
}
But I think I started getting why it doesn't work.
Every time it checks from the second value of vec1, it starts checking from the first one and so it will always say condition false.
I should put j = something else that starts from the value in the last j position used instead of j = 0 every time it checks.
How should I do this?
It's not clear to me what you're saying above. Using your example vectors (with the switched values in vec2), and with i = j = 2, the condition in your if statement above works out to:
9 > 6 && 9 < 4
The first expression is true, but the second is false, which makes the overall condition false. (It only takes one of the subexpressions being false to make the combined expression false.)
If you want to check whether 9 is between 6 and 4, the check should be 9 < 6 && 9 > 4. If you want to check that vec1[ i ] is between vec2[j] and vec2[j+ 1], you need to check both possibilities -- 1) when the two vec2 values are in sorted order, and 2) when they're out of order. That was the intent of the code example I gave before.
 
Last edited:
  • #23
Mmm... the last thing you said made me rethink of all the exercise. I think I understood it wrong.
Okay, I'll try to do it again and commenting it step by step.
I'll post it here later.
Thanks for your help, Mark44!
 
  • #24
Sorry! I've been busy these week and couldn't post it.
Anyway, this is the final code and works fine now(I think):
Code:
#include <iostream>

using namespace std;

int main () {
    //Initialization:
    const int N = 4;                                    //Initialization of the vector constant.
    const int M = 8;                                    //Initialization of the vector constant.
    int count = 0;
    int V1[N] = {3, 5, 54, 122};                        //Initialization of the first vector.
    int V2[M] = {2, 4, 10, 34, 67, 108, 146, 172};        //Initialization of the second vector.
    int V3[5];                                            //Initialization of the the vector to use to merge the two successions.
    bool check = false;
  
    //Procedure:
    //Checking:
    if (N >= 2 && M >= N * 2) {                            //Checking if the first vector has more than one element and the second vector has more than the double of the first vector.
        for (int j = 0; j < N; j++) {                    //For each element of the first vector.
            if (check == false) {
                for (int i = count; i < M; i++) {                //For each element of the second vector starting from the last check.
                    if (V1[j] > V2[i] && V1[j] < V2[i + 1] && V1[j + 1] > V2[i + 1] && V1[j + 1] < V2[i + 2]) { //If the element of the first vector is greater than the element of the second vector and less than the next element of the second vector
                        check = true;                            //the condition is true.
                        V3[0] = V2[i];
                        V3[1] = V1[j];
                        V3[2] = V2[i + 1];
                        V3[3] = V1[j + 1];
                        V3[4] = V2[i + 2];
                        break;
                    }
                    else {                                        //Else the condition is false.
                        check = false;
                        count = i + 1;                            //Increasing the counter for next check.
                        break;                                    //Goes to the next check.
                    }
                }
            }
            else {
                break;                                    //The condition is true, so it goes out of the for loop.
            }
        }
    }
    else if (M >= 2 && N >= M * 2) {                    //Checking the other way with first and second vector switched.
        for (int j = 0; j < M; j++) {
            if (check == false) {
                for (int i = count; i < N; i++) {
                    if (V2[j] > V1[i] && V2[j] < V1[i + 1] && V2[j + 1] > V1[i + 1] && V2[j + 1] < V1[i + 2]) {
                        check = true;
                        V3[0] = V1[i];
                        V3[1] = V2[j];
                        V3[2] = V1[i + 1];
                        V3[3] = V2[j + 1];
                        V3[4] = V1[i + 2];
                        break;
                    }
                    else {
                        check = false;
                        count = i + 1;
                        break;
                    }
                }
            }
            else {
                break;
            }
        }
    }
    else {                                                //In case the two vectors do not meet the conditions the program ends.
        cout << "Condition: false";
        return 0;
    }
  
    //Output:
    cout << "Condition: true\n";
    cout << "Merging the two successions.\n";
    cout << "[";
    for (int i = 0; i < 5; i++) {
        if (i != 4) {
            cout << V3[i] << " ";
        }
        else {
            cout << V3[i] << "]";
        }
    }
  
    //Terminating the program:
    return 0;
}
 

1. What is a complicated vector exercise?

A complicated vector exercise is a mathematical problem that involves vectors, which are quantities that have both magnitude and direction. These exercises typically require the use of vector operations such as addition, subtraction, and scalar multiplication.

2. Why are complicated vector exercises important?

Complicated vector exercises are important because they help develop critical thinking and problem-solving skills. Vectors are also widely used in various fields such as physics, engineering, and computer graphics, so understanding them is essential for these careers.

3. What are some common types of complicated vector exercises?

Some common types of complicated vector exercises include finding the magnitude and direction of a vector, determining the result of vector addition and subtraction, and solving for unknown vector components using given information.

4. How can I approach solving a complicated vector exercise?

The key to solving a complicated vector exercise is to break it down into smaller, more manageable steps. Start by identifying the given information and what you are trying to find. Then, use vector operations and the appropriate formulas to solve for the unknown variables.

5. What are some tips for mastering complicated vector exercises?

Practice is key when it comes to mastering complicated vector exercises. Make sure to familiarize yourself with vector operations and their properties. Also, try to understand the underlying concepts rather than just memorizing formulas. Additionally, seeking help from a tutor or teacher can also be beneficial.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
20
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
729
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
Back
Top