Thread: Looking not founding (the problem)

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25

    Looking not founding (the problem)

    Hi Guys!

    I have this little piece of code, that generates an exception if I set NUM_P to lets say 4000 or more.

    Code:
    #define NUM_P 2300
    #define PARTY_NUM 100
    #define PEOPLE_PER_PARTY NUM_P/PARTY_NUM
    
    	for (a = 1; a<= PARTY_NUM; a++)
    	{
    		for (i = PEOPLE_PER_PARTY*(a-1); i < PEOPLE_PER_PARTY*a; i++)
    	{
    		for (k = PEOPLE_PER_PARTY*(a-1); k < PEOPLE_PER_PARTY*a -(i-PEOPLE_PER_PARTY*(a-1)); k++)
    		{
    
    
    			if ( k != 0)
    			if (bday[i] == bday[k + i])  
    				count++;
    	
    		}
    	}
    	}
    This is basically a counter that checks an array to see if 2 int's match.



    Before I tried to pirk it up a little the code was like this

    Code:
    #define NUM_P 2300
    #define PARTY_NUM 100
    #define PEOPLE_PER_PARTY NUM_P/PARTY_NUM
    
    	for (a = 1; a<= PARTY_NUM; a++)
    	{
    		for (i = PEOPLE_PER_PARTY*(a-1); i < PEOPLE_PER_PARTY*a; i++)
    	{
    		for (k = PEOPLE_PER_PARTY*(a-1); k < PEOPLE_PER_PARTY*a; k++) 
    		{
    			if ( i != k)
    			if (bday[i] == bday[k])
    				count++;
    
    	
    		}
    	}
    	}
    What I tried to do in the first algorithem is to count the array (for example for bday[10] to bday[20] so that we wouldn't count twice, or just plain wrong) because you can see that in the second algorith(second prog.) if let's say 10,14,15,16 match then we would count++ 4*3 = 12 times, but we only had 4 matches so this has to be fixed.


    So now to the conclusion, what I'm askin' is if you can scan through it if you can catch the bug, because I've been looking at it for some time now and I'm starting to get delirious. I'll continue debugin' meanwhile any help will be appreciated.

    PS: If you happen to find any loopholes in the algorythm be sure to mention them.

    Thnx

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The second number must always be larger than the first, so k needs to start, not at the beginning, but at i+1. And do you want the number of matches, or the number of people? For example 2 people give 1 match, 3 people give 3 matches, 4 people (your example) give 6 matches, et cetera.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    I must say I don't understand what you mean.
    The second number must always be larger than the first, so k needs to start, not at the beginning, but at i+1.
    Yeah you're right that is wierd, now that I see it, I'll go with the num. of matches, more natural

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    In a case like this (with only 366 possibilities) you may as well use
    an integer array of 366 elements for your counters. Suppose it's
    called "days". Initially zero it out, then loop through the people and
    do this for each:
    Code:
    ++days[bday[i]]
    Then loop through "days" to see how many elements are more than 1,
    indicating a match.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The second number must always be bigger than the first. You can't go matching 10 with 11 and then 11 with 10. You need to match 10 with 11 through 20; 11 with 12 through 20 (not 10 through 20!); 12 with 13 through 20; ...; 18 with 19 and 20; 19 with 20.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    The second number must always be bigger than the first. You can't go matching 10 with 11 and then 11 with 10. You need to match 10 with 11 through 20; 11 with 12 through 20 (not 10 through 20!); 12 with 13 through 20; ...; 18 with 19 and 20; 19 with 20.
    I totaly agree but I don't see where I'm doing that, do you mean this line
    Code:
    if (bday[i] == bday[k + i])
    if it is, I don't see the flaw.

    Nucleon, that is a good idea, BUT not for what I'm doing, at least I think that it wouldn't work (wouldn't do the comparison with the idea on which it is based)

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Crazed View Post
    I totaly agree but I don't see where I'm doing that, do you mean this line
    Code:
    if (bday[i] == bday[k + i])
    if it is, I don't see the flaw.

    Nucleon, that is a good idea, BUT not for what I'm doing, at least I think that it wouldn't work (wouldn't do the comparison with the idea on which it is based)
    I was looking at the second program you posted which was
    Code:
    for (k = PEOPLE_PER_PARTY*(a-1); k < PEOPLE_PER_PARTY*a; k++)
    that. Since (I thought) that was the code that was giving you too many matches.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I would have done the coding for you on this one since the title alone gave me a smile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM