0

I have to compare two arrays, i need to know if the same name of a value in the other array exists. My problem is that it always returns only one matched value but there are two with the same name.

One array is 9 length big the other 3.

Is there maybe an easier solution because mine looks a little bit too complex, am I right?

Thank you in advance

Here is my Code:

for (int i=0;i<9;i++ )
    {
        int counter = 0;
        int j = 0;
        if (stockTest[j].getTestTitle() == products[i].getTitle())
        {

            cout << stockTest[j].getTestTitle() << " is available ";
            counter = counter + 1;  // counter + 1 because it is available

        }

        if ((j == 0) && (counter == 0) && (i == 9)) // try everything till i is 9 if counter is still 0 display message.
        {
            cout << stockTest[j].getTestTitle() << " is not available ";

        }

        if ((j == 1) && (counter == 0) && (i == 9)) // compare everything from stockTest[1], till i is 9 if counter is still 0 display message.
        {
            cout << stockTest[j].getTestTitle() << " is not available ";

        }

        if ((j == 2) && (counter == 0) && (i == 9)) //compare everything from stockTest[2], till i is 9 if counter is still 0 display message.
        {
            cout << stockTest[j].getTestTitle() << " is not available ";

        }

        if ( i == 9)
        {
            j = j + 1; //there are three values to compare in the other array so I will increment like this till 2 (the next if statement will end the loop if j == 2)
            i = 0;     // i again 0 so that again all 9 values from the array will be compared
            counter = 0; // counter = 0 so that if the value is not found the counter == 0 is true
        }

        if ((j == 2) && ( i = 9 ))
        i = 9; //i is now 9 which means that loop should end now however I can delete this line of code and the program would still display only one value. I expected an infinte loop if i delete it?



    }  
1
  • 2
    There is a function that does this already called std::set_intersection. Commented May 28, 2015 at 15:35

2 Answers 2

2

If the arrays can be sorted on title, then one solution would be to use std::set_intersection.

Pre C++ 11 code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <ostream>

struct stockTest
{
    std::string title;
    std::string getTitle() const { return title; }
    stockTest(const std::string s) : title(s) {}
    friend std::ostream& operator << (std::ostream& os, const stockTest&);
};

// for output purposes
std::ostream& operator << (std::ostream& os, const stockTest& s)
{
    os << s.getTitle();
    return os;
}

// compares two stockTest items
bool Comparer(const stockTest& f1, const stockTest& f2)
{
    return f1.getTitle() < f2.getTitle();
}

using namespace std;

int main()
{
    stockTest s1[] = {stockTest("abc"), stockTest("123"), stockTest("456")};
    stockTest s2[] = {stockTest("123"), stockTest("Joe"), stockTest("789"), stockTest("456")};

    // first, we sort our arrays
    std::sort(s1, s1 + 3, Comparer);
    std::sort(s2, s2 + 4, Comparer);

    // this vector will contain the similar items
    std::vector<stockTest> v_intersection;

    // use set_intersection to do the hard work
    std::set_intersection(s1, s1 + 3, s2, s2 + 4, std::back_inserter(v_intersection), Comparer);

    // output the results
    cout << "The similar names are: " << endl;
    copy(v_intersection.begin(), v_intersection.end(), ostream_iterator<stockTest>(cout, "\n"));
}

Note that at the end, we created a vector that contains the common names. Also note that we had to sort the arrays first, plus provide set_intersection to know how the items are sorted (according to the Comparer functor).

Live Example: http://ideone.com/GA8ey0

Sign up to request clarification or add additional context in comments.

1 Comment

Also, for large sized arrays, this will run faster than writing a doubly nested loop. But again, only if the items are sorted before processing them.
1

First, you reinitialize counter to 0 every iteration of the first loop which probably has something to do with it.

Second, i would do something like the following with two for loops:

int counter = 0;
for(int i = 0; i<3; i++)
{
  for(int j=0; j<9; j++)
  {
     if(array1[i] == array2[j])
     {
        counter++;
     }
  }
}

It is difficult to follow your logic as im not sure what you're using counter for or why (but breaking it down to simplicity, it is just a counter that stores how many times an equivalent value has been matched. Lets not over complicate it).

So its just a simple outer and inner for loop where you iterate and compare all 9 values of the second array to each value of the first array (3 values). I hope this helps.

7 Comments

Thank you, yes it is a good idea to make it like this. However for some reason it still does not find my other value.. It does not see them as the same. They are both extracted from a txt file.. so maybe there is somewhere the problem but I think you answered the question.
Are they extracted from the same text file or different files? Also, try and step through your code and see what it is extracting. It may not be properly handling certain characters or maybe whitespaces, but glad i could help.
From different files, I thought that I checked everything but I think I need to double check.
The drawback is that the solution has O(n^2) complexity, where n is the maximum number of items in array1 or array2. So if array1 has 1,000 items, and array2 has 1,000 items, you're looping 1,000,000 times. If the items can be sorted (using a decent sort routine such as std::sort), then the set_intersection can be used to soften the blow.
yeah you are probably right but I'm still a beginner in C++, so I think my assignment requires that simple loop one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.