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!

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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Complicated Vector Exercise
  1. Matlab exercises (Replies: 1)

Loading...