Thread: Function problem

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question Function problem

    I really don't know how to write the function part although i tried I'm not sure if it makes sense, I'm a beginner and sometimes i can't tell where i messed up.

    Suppose we declare int a[N], b[M], c[N], where N and M are predefined
    global constants, somewhat we initialize a and b such that they contain two sets and assume that the values in a and b are already sorted in ascending order (so you don’t need to anything
    else, but use them as is).
    NOW, We are interested in finding:
    a. the difference between a and b, and put the elements of a-b into c and
    b. the number of elements in c.

    Write a function that will take a, b, c as parameters and determine the difference set c and the number of elements in c. The function will return the number of elements in c while storing the
    difference between a and b into c.

    For example, if a = {2, 5, 7, 8, 13} and b = {1, 5, 7, 9, 12}, then your function
    will return 3 as the number of elements in c,
    and we will have c = {2, 8, 13} as a-b.




    Code:
    int set_difference(int a[N], int b[M], int c[N])
    
    {
    
    int i;
    
    for (i=0; i<b; i++) {
    
    if ( x[N]==c)
    
    return i;
    
    else if (x[N] > c)
    
    
    
    
    }
    return (-1);
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    try to explain your algorithm in english or any pseudo code, then we can discuss it to get proper c.

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    He means subtracting two arrays (pseudo-sets) and store the result in c. You could use this (It's kinda slow, O(n²) performance)

    Code:
    int function(int a[N], int b[M], int c[N])
    {
        for every element in a
            for every element in b
                if the element in a matches the element in b
                    set a flag
            if the flag is not set
                add the element in a to c
        return the size of c
    }
    Of course, if the arrays are 100% sorted, you could use binary search. Maybe that would be faster.
    Last edited by Brafil; 05-08-2009 at 08:14 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your example is wrong, or I'm too tired to interpret it correctly:
    Write a function that will take a, b, c as parameters and determine the difference set c and the number of elements in c. The function will return the number of elements in c while storing the
    difference between a and b into c.

    For example, if a = [2, 5, 7, 8, 13] and b = [1, 5, 7, 9, 12], then your function
    will return 3 as the number of elements in c,
    and we will have c = [2, 8, 13] as a-b.
    Shouldn't C be: [2, 8, 9, 12, 13]? It's basically finding all the elements between the two that don't match, right? Or is it supposed to just find the ones in A that aren't in B? If it's not the latter, then your example is wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM