Thread: help plz-begginer question

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    help plz-begginer question

    Function returns the maximum size of the common between the two arrays?
    (function recieve (array a, length of a , array b, length of b ))

    for ex:
    array a{2,1,2,3}
    array b{2,3}
    the res would be 2.{2,3 shown in array a and its the max

    array a{2,2,2}
    array b{2}
    the res would be 1.

    array a{2,1,2,3}
    array b{3,4,5}
    the res would be 1.

    array a{4,5,2,1,2,3}
    array b{9,8,8,7,8,8,2,3}
    the res would be 2.

    hope u understood the question

    i am stuck i cant figured it out ..
    i thought to run with two loops
    Code:
    for (i=0; i<lenght of a; i++)
        {
            for (j=0; j<length of b; j++)
            {
                if(a[i]==b[j])
                {
                    count++;
    and here i got stuck i can keep the count .. but then when it founds another nums that are equals i dont know how to split the counter from 0 and then to check what is the max .

    thank u .. and i really tried for a couple of hours and the right solution dont hit me .
    Last edited by erag; 12-01-2009 at 06:20 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sorry, the stupid vbulletin code catcher made me do this:
    Code:
    Quote Originally Posted by erag View Post
    array a{4,5,2,1,2,3} array b{9,8,8,7,8,8,2,3} the res would be 1.
    Looks to me like it would be 2 (2 and 3). Or do I not understand your question?
    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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Count *is* the max - it's a quantity, and can't be greater than the number of values in the smaller of the arrays. you'll need to add that bit of code, and put it in place right after the two loops are completed.

    I don't understand your comment about "split the counter from 0 and then to check what is the max".

    Duplicate numbers will have to be handled so only 1 of them is sent down for the loops to work with. I'd suggest using a bucket sort type of arrangement (aka distribution count), to make sure each number going to the loops, is a unique number.

    Alternatively, you could sort both arrays and use some logic to handle this, but that would be more work.
    Last edited by Adak; 12-01-2009 at 05:47 PM.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Also your only example of any match greater that 1 was for your 2 consecutive numbers. Are you not worried about matching elements that are split? Are you more after locating the longest match or just the first match found? I think there are some necessary points of your assignment missing here. In either case if you know how to loop through your arrays calculating for both and tallying up the results should be easy on any account.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    hi guys thank u for the quick answer ..
    slingerland3g : i am intersted in locating the longest match .
    Adak :"I'd suggest using a bucket sort type" -i dont know what it is ..
    and yes the duplacted ones are the ones who causes me trouble.
    can you direct me by writing the code so i could understand it ?
    i got lost and i know that is not to hard.
    thnks.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    somebody?

  7. #7
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Quote Originally Posted by erag View Post
    somebody?
    Code:
    #include <stdio.h>
    #define a_size 6
    #define b_size 8
    int function(int array_a[], int lenght_a, int array_b[], int lenght_b);
    int main()
    {
    	int a[a_size] = { 4, 5, 2, 1, 2, 3 };
    	int b[b_size] = { 9, 8, 8, 7, 8, 8, 2, 3 };
    	
    	int result = function(a, a_size, b, b_size);
    	printf("%d\n", result);
    	
    	return 0;
    }
    int function(int array_a[], int lenght_a, int array_b[], int lenght_b)
    {
    	int i, j, count = 0, hold = 0;
    	for(i = 0; i < a_size; i++) {
    		for(j = 0; j < b_size; j++) {
    			if((array_b[j] == array_a[i])&&(array_b[j] != hold)) {
    				count++;
    				hold = array_b[j];
    			}
    		}
    	}
    	return count;
    }
    This should work
    Last edited by rob90; 12-02-2009 at 08:49 AM.

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    thank u !!i'll check out all the case in a bit
    but i noticed that if i send:
    Code:
    a{0,0}
    b{0}
    the res is 0 instead of 1
    and in the question i need to return count .. but if the function didnt find nothing"count=0"
    it should be "return (-1)"
    how do i resolve it?

    another case i just noticed
    Code:
    int a[a_size] = { 2,4 };
    int b[b_size] = { 2,3,4}}
    the func return "2" insteead of "1".


    thanks again you really helped me...
    Last edited by erag; 12-02-2009 at 09:11 AM.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Quote Originally Posted by erag View Post
    thank u !!i'll check out all the case in a bit
    but i noticed that if i send:
    Code:
    a{0,0}
    b{0}
    the res is 0 instead of 1
    and in the question i need to return count .. but if the function didnt find nothing"count=0"
    it should be "return (-1)"
    how do i resolve it?

    another case i just noticed
    Code:
    int a[a_size] = { 2,4 };
    int b[b_size] = { 2,3,4}}
    the func return "2" insteead of "1".


    thanks again you really helped me...
    that solves the first problem, about the second one why does it have to return 1?
    Code:
    int function(int array_a[], int lenght_a, int array_b[], int lenght_b)
    {
    	int i, j, count = 0, hold = -1;
    	for(i = 0; i < a_size; i++) {
    		for(j = 0; j < b_size; j++) {
    			if((array_b[j] == array_a[i])&&(array_b[j] != hold)) {
    				if(count == 0) {
    					hold = 0;
    				}
    				count++;
    				hold = array_b[j];
    			}
    		}
    	}
    	return count;
    }

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    cause the num 2 shows 1 time in array a. and num 4 is also showed 1 timein array a.

    the two nums shows in array be one time each so the func shoulld return 1
    the func check within the array a the max appearence and the max is 1.
    1 for 2
    1 for 4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plz answer the following question.....
    By raja2033 in forum C Programming
    Replies: 5
    Last Post: 04-06-2009, 08:35 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Newbie Source Code question. Hlp Plz?
    By BOOGIEMAN in forum Game Programming
    Replies: 4
    Last Post: 12-14-2001, 04:30 AM