# Thread: Comparing two sets of integers

1. ## Comparing two sets of integers

I have 2 sets of ten integers, and want to compare each from the first set against each from the second, and increment a counter for each positive match. I can think of a couple of long ways to do this using a load of if statements, but wondered if there is any quick method for doing it. Whats the best way?

2. What's wrong with using loops?

3. With only two sets of ten integers the naive algorithm with two nested loops that literally "compare each from the first set against each from the second" is fine, but a more generally efficient algorithm would sort the two sets of integers and then make a single parallel pass through them to count matches.

4. Well, two nested loops were the first thing I thought about, but the integers are in variables, and I don't know how to increment from one variable to the next.

e.g. I have the integer variables:

Code:
```a1,a2,a3,a4,a5,a6,a7,a8,a9,a10
b1,b2,b3,b4,b5,b6,b7,b8,b9,b10```
But there is no way I know to cycle through each set.

5. Originally Posted by bertazoid
Well, two nested loops were the first thing I thought about, but the integers are in variables, and I don't know how to increment from one variable to the next.

6. Ok, if I use integer arrays, can each element of the array contain an integer of 2 digits or more? (since most of my variables are between 2 and 4 digits).

7. Originally Posted by bertazoid
Ok, if I use integer arrays, can each element of the array contain an integer of 2 digits or more? (since most of my variables are between 2 and 4 digits).
If they cannot, then all your 20 integer variables cannot either. However, the C standard mandates minimum limits for int, and according to these minimum limits (namely, [-32767,32767]) there will be no problem with your expected range.

8. Originally Posted by bertazoid
Ok, if I use integer arrays, can each element of the array contain an integer of 2 digits or more? (since most of my variables are between 2 and 4 digits).

Were you thinking of an array of char's. Then this would be something to consider. For int values you are safe.

9. Ok so I should be ok then? I'll give it a whirl. Thanks

10. If you want (or have) to keep your variables seperate and not in an array, you can easily assign an array of int pointers to them. Using the array makes iterating through the set easy:
Code:
```#include <stdio.h>

int main() {
int a=3,b=4,c=5,x=1,y=2,z=3,*setA[3]={&a,&b,&c},
*setB[3]={&x,&y,&z}, i, ii, count=0;
for (i=0;i<3;i++) for (ii=0;ii<3;ii++)
if (*setA[i]==*setB[ii]) count++;
printf("Number of matches: %d\n",count);
}```
This way, of course, you can also change the value of the variables without worry. If you copy them into a normal array you can't.

11. That just adds as much work as making the variables an array. This is what arrays are for.