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 ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What's wrong with using loops?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    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. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Quote 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.
    Use two arrays instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by bertazoid View Post
    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. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Ok so I should be ok then? I'll give it a whirl. Thanks

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    That just adds as much work as making the variables an array. This is what arrays are for.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 08:09 PM
  3. Comparing integers
    By jw232 in forum C++ Programming
    Replies: 17
    Last Post: 04-01-2008, 12:36 PM
  4. creating new sets
    By axon in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2003, 06:37 PM
  5. Replies: 6
    Last Post: 08-04-2003, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21