Thread: Set intersection in C

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    1

    Set intersection in C

    I have a task for college and I have to write a code for an intersection of sets in C. I have written the code, and when I try to test the program with 9 previously defined tests, I pass 8 out of 9 of them. I get an error in one of the tests, where my condition actually coincide with previous condition. I have no idea how to fix this so I'm here asking y'all how can i fix this. Here is the code in pastebin, so you could see and try to help me.

    Note: We didn't learn about arrays or lists, so I am not allowed to use that, and loops too...

    "Unesite brojeve a, b, c, d" - means "Insert numbers a,b,c,d"
    "Rezultantni interval je" - means "Final interval is"
    "Skupovi se ne sijeku" - means "Sets do not intersect"

    I hope that you can help me
    Here is the code: https://pastebin.com/hBTxEQEv

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the test that fails?

    If you were not given the test values, then you should write your own 9 tests that try to replicate what your instructor is testing. Knowing what is the failing test will give you information on what you did wrong and some hint as to how to fix it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    The final condition definitely doesn't make sense.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    138
    I don't see any logic to ensure that the numbers are in proper order. That is, you never check if a < b, or c < d, prior to doing your other checks. This would be validation and your course may not be worried about it right now, but it's something to be aware of.

    Next, consider what happens if some of the compared values are exactly equal to each other. Your relations all check for strict greater or strict less than. And in most cases that may be enough. But think through a case where values are equal.

    Also, what happens if both ends of your intervals are the same number? That is, something like 1.0 1.0 for a and b. Will your code work?

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    You can rewrite any loop as a set of recursive calls. There's a school of thought that you should do that. However not normally in C, but in a so-called "functional" programming language. Scheme is an example.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intersection of 2 arrays
    By Cazan Adelin in forum C Programming
    Replies: 6
    Last Post: 01-13-2015, 08:40 AM
  2. intersection of two sets
    By csharp100 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2011, 05:22 PM
  3. intersection of two STL lists
    By misterMatt in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2009, 12:25 AM
  4. (C#) Ray->Box intersection
    By Devils Child in forum Game Programming
    Replies: 6
    Last Post: 12-18-2008, 01:02 AM
  5. Vector intersection
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-27-2006, 11:23 PM

Tags for this Thread