Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Struct Question (http://www.programmingforums.org/showthread.php?t=12138)

aznballerlee Dec 8th, 2006 8:24 PM

Struct Question
 
First, a function called getOverlap was created with the following prototype:
:

double getOverlap (double span1[], double span2[])
The given parameters contain exactly two values, the lower and upper bounds of the interval of a given line. If the intervals are overlapping the function, the function should return the size of the overlap.

After this problem, we defined the struct:
:

Struct Span
{
        int low;
        int high;
};


I'm supposed to change the implementation to use the following prototype:
:

double getOverlap (Span span1, Span span2)

Here's what I tried. Can anyone give feedback of the correctness of the code? Thanks.

:

Struct Span
{
        int low;
        int high;
};

double getOverlap (Span span1, Span span2)
{
        int counter = 0;
        int n = strlen(span1);
        for (int i = 0; i < n; i++)
        {
                if ((span1 -> low) == (span2 -> high))
                        counter++;
        }
        return counter;
}


Jessehk Dec 8th, 2006 8:48 PM

:

strlen(span1]

strlen() returns the length of a C-string. You are using it with an instance of a structure, which is wrong and will raise errors.

I don't really understand the logic of your loop.

I have a suggestion:

Write out the function as it should be using plain english and post back. That way, we can seperate the logic errors from the programming misconceptions. :)

I rewrote the function for you, but I'd like to see you try that before I post it (I'm not trying to be condescending here :p).

Jimbo Dec 8th, 2006 8:50 PM

Looks like you've got some syntax errors. Have you tried running this through a compiler? If you do, you'll find these, and you can test for yourself it it's working.

To answer your question, this won't work. You need to compare the ranges described by each Span to see what their intersection is. Instead you are... uh... I'm not sure. You don't need a counter. You can't use strlen() without suppling a c-string. Your logic doesn't answer the question.

Here's how it could be done, logically: (assume Spans are called A and B)
- find the maximum of the lows (call it C)
- find the minimum of the highs (call it D)
- find D-C, where a negative value should return 0.
Make sense?

[edit:] beaten by Jessehk :p

aznballerlee Dec 8th, 2006 9:01 PM

I tried implementing your algorithm; I hope that it's on the right traack.

:

double getOverlap (Span span1, Span span2)
{
    int c = low;
        for (int i = 0; i < span1.size() ; i++)
        {
                if (span1[i] > c)
                        c = span 1[i];
        }

        int d = high;
        for (int j = 0; j < span2.size() ; j++)
        {
                if (span2[j] < d)
                        d = span 2[j];
        }

        int a = d - c;
        if (a>=0)
                return a;
        else
                return 0;

}


Jimbo Dec 8th, 2006 9:45 PM

What's low? What's high? How are you indexing span1 and span2? What are span1.size() and span2.size()? Why are you still using loops?

All you need to do is get span1.low and span2.low and find which is largest. Then get span1.high and span2.high and see which is smaller. Then compare them as I described above.

I'm thinking you should probably be reading through a book on programming in C at this point...

aznballerlee Dec 8th, 2006 9:59 PM

I've read the book, but my final exam is tomorrow, so that's not a good option.
I just did'nt understand the question quite well, about the high and lows.
Your point to use span1.low, etc. made this more clear.

This is another attempt:

:

double getOverlap (Span span1, Span span2)
{
        int c = low;
        if (span1.low > span2.low)
                low = span1.low;
        else
        { 
                low = span2.low;
        }

        int d = high;
        if (span1.high < span2.high)
                high = span1.high;
        else
        {
                high = span2.high;
        }

        int a = d-c;
        if (a>=0)
                return a;
        else
                return 0;
}


Jimbo Dec 8th, 2006 10:06 PM

The logic looks right this time, but there's still some syntax errors. You never declared low or high, and so setting c and d is wrong. In fact, you could just replace c and d with low and high respectively, and it should work. Something like this:
:

int getOverlap(Span s1, Span s2)
{
  int low, high;
  if(s1.low < s2.low)
    low = s2.low;
  else
    low = s1.low
  if(s1.high < s2.high)
    high = s1.high;
  else
    high = s2.high;
  int a = high - low;
  if(a < 0)
    return 0;
  else
    return a;
}


aznballerlee Dec 8th, 2006 10:10 PM

Low and high were defined in the struct, I'm not sure if you saw that part of this code (preceding my double function):
:

struct Span
{
int low;
int high;
}


Anyway, I get your logic. This should help me for other implementations for this same function.

Jimbo Dec 8th, 2006 10:11 PM

Yes, but that corresponds to span1.low and span2.low, etc... It doesn't correspond to low in the line:
:

int c = low;

aznballerlee Dec 8th, 2006 10:16 PM

Okay I understand you.


All times are GMT -5. The time now is 1:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC