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

  • Thread starter Thread starter Kernul
  • Start date Start date
  • Tags Tags
    Exercise Vector
Click For Summary

Discussion Overview

The discussion revolves around a C++ code implementation for checking and merging two vectors of integers based on specific conditions. Participants explore the requirements of the exercise, the logic of the code, and potential issues related to checking the order of the vectors and the merging process.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • The original poster (OP) describes the task of checking if a sequence in one vector is bounded by elements of another vector and expresses concern about the correctness of their code.
  • Some participants suggest using MATLAB as an alternative approach, which the OP clarifies is not acceptable for their assignment.
  • One participant emphasizes the importance of providing diagnostic messages to help identify issues in the code.
  • The OP identifies a specific problem where the code incorrectly reports that the condition is true even when the vectors are not sorted, leading to incorrect merging.
  • Another participant points out that if the vectors are assumed to be sorted, the OP should not test unsorted vectors, suggesting that the code should check for sorted order before merging.
  • The OP insists that the exercise requires checking the relationship between the two vectors as specified, rather than simply checking each vector's order independently.
  • Clarification is sought regarding the naming of the vectors in the code, as there seems to be confusion between the terms used (V1, V2, VX, VY).

Areas of Agreement / Disagreement

Participants express differing views on how to approach the problem, particularly regarding the necessity of checking the order of the vectors and the logic of the merging process. The discussion remains unresolved as participants explore various perspectives without reaching a consensus.

Contextual Notes

There is an assumption that both vectors should be sorted for the merging process to be valid. However, the OP's code does not currently enforce this precondition, leading to potential logical errors in the output.

Kernul
Messages
211
Reaction score
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) {
        count<<"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 {
        count<<"Checked: Condition false.";
    }
   
    //Output:
    if (condition == true) {
        count<<"[";
        for (int l = 0; l<N+M; l++) {
            count<<vec3[l]<<" ";
        }
        count<<"]";
    }
   
    //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
you can use MATLAB coder
Write in MATLAB and by MATLAB coder compile to C
 
I don't know what you mean. I never used Matlab and I have to write everything in C/C++.
 
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.
 
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.
 
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?
 
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 {
    count<<"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 {
    count<<"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.
        count << "Condition: false";
        return 0;
    }
  
    //Output:
    count << "Condition: true\n";
    count << "Merging the two successions.\n";
    count << "[";
    for (int i = 0; i < 5; i++) {
        if (i != 4) {
            count << V3[i] << " ";
        }
        else {
            count << V3[i] << "]";
        }
    }
  
    //Terminating the program:
    return 0;
}
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
8
Views
2K