0

I was trying to locate vector a in vector b. So it's like if vector a is in vector b then return true else false. And vector a should be like {1,2,3} and b should like {4,1,2,3,5,5,7}. The output of this code is coming like true 1 and false 0. So my problem is I don't want 1 and 0 to show up in the output. Also I want to write this code in recursive form so could anyone help me with this problem?

    bool x(vector<int>& a, vector<int>  b)
{
    vector<int> index ( b.size(),0 );
    int counter = 0;

    if ( a.size() <= b.size() ) {

        for ( int i = 0; i < a.size(); i++ ) {
            for ( int j = 0; j < b.size(); j++ ) {
                if ( a[i]== b[j]) {
                    index[j] = 1;
                }
            }
        }

        for ( int i = 0; i < index.size(); i++ ) {
            if ( index[i] == 1 ) {
                for ( int j = i; j < index.size(); j++ ) {
                    if ( index[j] == 1 ) {
                        counter++;
                    }
                }
                if(counter == a.size()){
                    cout<<"true"<<endl;
                    return true;
                    break;
                }
                else{
                    counter = 0;
                    cout<<"false"<<endl;
                    return false;
                  //  continue;
                }

            }
        }
    }
     return 0;
}

4 Answers4

1

if you just don't like the output: true 1 and false 0. the simplest way to solve it is: if(x(a, b)){ cout<<"true"<<endl; } else { cout<<"false"<<endl; }

BUT, before you try to convert you algorithm to a recursive one, I afraid that your algorithm is wrong. try this: vector<int> a; vector<int> b; a.push_back(1); a.push_back(2); a.push_back(3); b.push_back(1); b.push_back(1); b.push_back(1); cout<<x(a,b)<<endl;

you will get true ,and the correct answer is false.

check your algorithm !

huangjiadog
  • 29
  • 1
  • 4
  • So I checked it but could not figure out what went wrong? – user4032883 Oct 13 '14 at 07:14
  • I think that you'd better check it out by yourself. it's good for you~ Attention:the point is the vector a not the vector b.you must make sure the data in a is in b, not the data in b is in a – huangjiadog Oct 14 '14 at 03:20
0

The reason you're seeing "true 1" and "false 0" is probably because while you print "true" and "false" as strings in your function, you return a bool, which if you print outside the function will be default print as "1" and "0". To overcome that, you can use std::boolalpha as discussed here: Converting bool to text in C++

As for converting your algorithm to a recursive one, I'm afraid that's off topic for Stack Overflow until you try it yourself.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

Like John said, you're printing the cout (standard out) Strings, not integers (1/0). Also agree with the off-topic, you need to suggest a solution and ask for help/recommendations/feedback. There's a heap of tutorials on the net about how to it.

But if you want to recurse, you should create an array3 whose length is the same size array1, whereas you'll recurse over array2 to check if any elements of array2 exist (either as objects or as values) in array1. You'll need a function that produces an array and takes in arrays (why are you using vectors?) and then iterates over the 2nd array, passing array1, array3 and current index value as arguments.

Fibonacci recursive problem is taught in just about every programming course in Uni (or at least it should be), so it's a good starting point to look at.

Justin Mitchell
  • 665
  • 6
  • 13
0

Recursion function of this :

// it will return true if vector is present
// i is pointer for a 
// j is pointer for b
bool subVector(vector<int>& a, vector<int>  b,i,j)
{
    if( i<a.size() && j<b.size()
    {
        if(a[i]==b[i])
        {
            return subVector(a,b,i+1,j+1)
        }
        else
        {
            return subVector(a,b,0,j+1)
        }
    }
    else
    {
        if(i>a.size())
        {
            return true;
        }
        else{
          return false;
        }
    }       
}
// Calling
subVector(a,b,0,0);

Note : Not Compiled.

SeeTheC
  • 1,560
  • 12
  • 14