Comparing two sets of integers

This is a discussion on Comparing two sets of integers within the C Programming forums, part of the General Programming Boards category; I have 2 sets of ten integers, and want to compare each from the first set against each from the ...

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?

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.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

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.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.