![]() |
Struct Question
First, a function called getOverlap was created with the following prototype:
:
double getOverlap (double span1[], double span2[])After this problem, we defined the struct: :
Struct SpanI'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 |
:
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). |
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 |
I tried implementing your algorithm; I hope that it's on the right traack.
:
double getOverlap (Span span1, Span span2) |
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... |
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) |
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) |
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 SpanAnyway, I get your logic. This should help me for other implementations for this same function. |
Yes, but that corresponds to span1.low and span2.low, etc... It doesn't correspond to low in the line:
:
int c = low; |
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