1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Complicated Vector Exercise

  1. Jun 8, 2015 #1
    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 (C):
    #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 appriciate your help.
    My teacher only let us use these libraries: iostream, fstream, cstdlib, cmath and cstring. So I can't use the library "vector".
     
  2. jcsd
  3. Jun 8, 2015 #2
    you can use matlab coder
    Write in matlab and by matlab coder compile to C
     
  4. Jun 9, 2015 #3
    I don't know what you mean. I never used Matlab and I have to write everything in C/C++.
     
  5. Jun 9, 2015 #4
  6. Jun 10, 2015 #5

    NascentOxygen

    User Avatar

    Staff: Mentor

    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.
     
  7. Jun 10, 2015 #6
    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. Jun 10, 2015 #7

    Mark44

    Staff: Mentor

  9. Jun 10, 2015 #8

    Mark44

    Staff: Mentor

    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?
     
  10. Jun 10, 2015 #9
    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:
     
  11. Jun 10, 2015 #10

    Mark44

    Staff: Mentor

    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.
     
  12. Jun 10, 2015 #11
    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.
     
  13. Jun 10, 2015 #12

    Mark44

    Staff: Mentor

    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:"
     
  14. Jun 10, 2015 #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.
     
  15. Jun 12, 2015 #14

    Mark44

    Staff: Mentor

    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?


     
  16. Jun 12, 2015 #15
    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.
     
  17. Jun 12, 2015 #16

    Mark44

    Staff: Mentor

    What are the values of i and j when this happens?
     
  18. Jun 12, 2015 #17
    Both "2".
     
  19. Jun 12, 2015 #18

    Mark44

    Staff: Mentor

    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.
     
  20. Jun 12, 2015 #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.
     
  21. Jun 12, 2015 #20

    Mark44

    Staff: Mentor

    Maybe this? I added the second conditional expression to your if statement.
    Code (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.
     
  22. Jun 12, 2015 #21
    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:
    Code (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?
     
  23. Jun 12, 2015 #22

    Mark44

    Staff: Mentor

    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: Jun 12, 2015
  24. Jun 12, 2015 #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!
     
  25. Jun 18, 2015 #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 (C):
    #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;
    }
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted