using arrays for set operators

This is a discussion on using arrays for set operators within the C++ Programming forums, part of the General Programming Boards category; I want to use arrays to program a calculator that uses set operatiors. Like: A= 1,2,3,4 B=1,2,3,4,5 A union B ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    72

    using arrays for set operators

    I want to use arrays to program a calculator that uses set operatiors. Like:

    A= 1,2,3,4 B=1,2,3,4,5

    A union B = 1,2,3,4,5

    I'm asking the user for the input, but after that I'm stuck. I guess that I can use a loop that goes through the array and checks for dublicates and removes them. And then I can just display the output. I have no idea how to do this. Can anybody help me?

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Well assuming your set is always in sorted order, here is a simple solution-

    1 Create an array of the size of the largest set.

    2 Loop through the array and check each element against the smallest set.
    3 If the array doesn't contain the current element, add it to the array.
    4 Continue from step 2 with the next largest set until you have reached the largest set.

    If your elements are not in sorted order, you will just have to sort the new array.

    Note that there are much more efficient ways of doing this.
    Last edited by Spidey; 07-19-2009 at 07:18 PM.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Well, I don't know. What if it's not sorted?

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Just use the same technique, and sort the array after your done with the union.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    std::set which prevents you from adding duplicates could be used too, right?

  6. #6
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Yes, std::set does not let you add duplicates to the set and keeps itself sorted.
    I was assuming you had to use arrays, but if you can use set; all you have to do is

    Go through all elements in all your arrays
    And add them to the set.
    This will make sure that your elements are unique and in sorted order.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I have no idea how to do this. Can anybody help me?

    You could always use std::set_union.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    I would use assoc_vector from the Loki library - job done!
    (or at least learn from it)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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