Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
2.65/5 (3 votes)
See more:
Lets say i have two arrays
int x[]={1,9,15,20,28};
int y[]={8,15,20,28,30,1};

Now i want to compare the two arrays and see how many values matches.
Example for the above scenario the out put will be
4 vales matched.

Thanks
Posted

You need three two doses of std::sort and one of std::set_intersection. You don't need to mess about with loops. The recipe is:

- sort the arrays into ascending order
- use set_intersection to get a vector of the common elements from each array
- read out the number of common elements from the vector
 
Share this answer
 
Comments
Stefan_Lang 17-Jan-14 3:55am    
My 5, although if the task was set as homework this is probably over the top ;)
You need to compare each element of x[] array with each element in y[] array using 2 loops[^] (second one must be inside first one).

Try!

BTW: Google is your friend!
 
Share this answer
 
Comments
Philippe Mori 16-Jan-14 19:59pm    
If array are very large (thousand of items or more), that solution might not be efficient enough since it is O(n²).
Maciej Los 17-Jan-14 2:05am    
I know ;) but it looks like OP is a beginner ;) So He/She needs to start with basics.
Anyway, thank you for your comment.
Smells like homework.  What have you tried?

IMHO, the most efficient way to do this would be to use HashSet<T>.
Sorry, didn't realize this was a C++ question.

/ravi
 
Share this answer
 
v2
Comments
PIEBALDconsult 15-Jan-14 19:25pm    
I would have said Hashset too.
Member 9291223 15-Jan-14 19:55pm    
I tried to use the following but no luck:

int counter (int ran[],int user[])
{
int counter=0;
for (int i=0;i<5;++i)
{
int counter=0;
for (int j=0;j<5;++j)
{

if (ran[i] == user[j])
counter ++;

// cout << "There are" counter << "matches";
//cout << counter << ' ';

// Return an error, or print "error there is a duplicate value" etc
}
return counter;
}
Stefan_Lang 16-Jan-14 3:52am    
Remove the second declaration (and initialization) of counter.
hope try this
C++
#include <stdio.h>
#define ARY_SIZE 10
 
int main (void)
{
//Local Declarations
    int numbersA[ARY_SIZE];
    int numbersB[ARY_SIZE];
    int i;
    int j;
 
//Statements
    printf("Please Enter 10 Integers For Array A\n");
    for (int i = 0; i < ARY_SIZE; i++)
        scanf("%d", &numbersA[i]);
     
    printf("\nPlease Enter 10 Integers For Array B\n");
    for (int j = 0; j < ARY_SIZE; j++)
        scanf("%d", &numbersB[j]);
     
    for (int i = 0; i < ARY_SIZE; i++)
       {
        for (int j = 0; j < ARY_SIZE; j++)
            {
            if (numbersA[0] == numbersB[0])
                return true;
            }
        }
    return 0;
}
 
Share this answer
 
v2
int main()
{
int n1,n2;
int*ptr;
int*aptr;
cout<<"Please Inter Size Of Array A:";
cin>>n1;
cout<<"Please Inter Size Of Array B:";
cin>>n2;

ptr=new int [n1];

for(int i=0;i<n1;i++)>
{
cout<<"Please Inter Element Array A"<<i<<" : "<<endl;
cin>>*(ptr+i);
}

aptr=new int [n2];

for(int j=0;j<n2;j++)>
{
cout<<"Please Inter Element Array B"<<j<<" : "<<endl;
cin>>*(aptr+j);

}
for(i=0;i<n1;i++)>
{
for (j=0;j<n2;j++)>
{

if (*(ptr+i)==*(aptr+j))
cout<<"Element"<<i<<"A="<<"Element"<<j<<"B"<<endl;
}
}

return 0;
}
 
Share this answer
 
v2
Comments
Stefan_Lang 27-Jan-14 2:48am    
Downvoted for the following reasons:

1. The question was both outdated and sufficiently answered
2. No explanations
3. Doesn't solve the problem
4. No formatting, no comments, some odd choices of programming style (e. g. *(ptr+i) instead of ptr[i])
5. Introduction of dynamic arrays even though that was not requested. Omitting the delete statements, causing memory leaks.

Overall this was more harmful than helpful for the purposes of answering the question.
Hamed Moezzi Azimi 27-Jan-14 14:33pm    
Hi sorry
My English is not good
I think the answer is correct
Program is very simple, requiring no explanation
  Solving the problem with a dynamic array provides complete program
  Of course, The program is still true without using a dynamic array
Thanks
I would use use one for loop than to increment the rand[] than the single value to a function with the user array compare the rand in a for loop in different function and than return the counter if it evaluates as a match it will increment. It really the same as what your doing but your setting the counter to zero again on the nested for loop which won't work. I only separate the for loops as preference your way should work if you don't set the counter to zero again within the nested for loop.

C#
int counter (int ran[],int user[])
{
    int counter=0;
     for (int i=0;i<5;++i)
        {
            int counter=0;
            for (int j=0;j<5;++j)
            {

              if (ran[i] == user[j])
              counter ++;

            // cout << "There are" counter << "matches";
            //cout << counter << ' ';

            // Return an error, or print "error there is a duplicate value" etc
         }
    return counter;
}
 
Share this answer
 
Comments
Stefan_Lang 27-Jan-14 2:47am    
Sorry, misplaced this response! Please ignore!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900