What's the best algorithm for it in C

Q

QQ

Hello I have a integer range
[a1 a2]
Now I have another integer range [b1 b2]
ai,bi =0,1,....,M

I wanna compare the relationship between the two ranges
for example
[a1 a2]=[0 99]
[b1 b2]=[20 80]
so there are 9 situations

1) a1=b1, a2<b2
2) a1=b1, a2=b2
....

I'd like to know which situation it belongs to
My current algorithm is to set another array c[2]

if a1<b1 set c[0]=0
if a1=b1 set c[0]=1
if a1>b1 set c[0]=2
if a2<b2 set c[1]=0
if a2=b2 set c[1]=1
if a2>b2 set c[1]=2

decide the situation by c

Is there any better way for it?

Thanks a lot!
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello I have a integer range
[a1 a2]
Now I have another integer range [b1 b2]
ai,bi =0,1,....,M

I wanna compare the relationship between the two ranges
for example
[a1 a2]=[0 99]
[b1 b2]=[20 80]
so there are 9 situations

1) a1=b1, a2<b2
2) a1=b1, a2=b2
...

I'd like to know which situation it belongs to
My current algorithm is to set another array c[2]

if a1<b1 set c[0]=0
if a1=b1 set c[0]=1
if a1>b1 set c[0]=2
if a2<b2 set c[1]=0
if a2=b2 set c[1]=1
if a2>b2 set c[1]=2

decide the situation by c

Is there any better way for it?

A little trickery might do.

Transforming your non-C example above into C, here's a function that
would return the 'situation' number...

int situation(int a1, int a2, int b1, int b2)
{
int c1, c2;

c1 = (a1 < b1) ? 0 : ((a1 == b1) ? 1: 2);
c2 = (a2 < b2) ? 0 : ((a2 == b2) ? 1: 2);

return (c1 * 3) + c2;
}

which returns
0 when (a1 < b1) and (a2 < b2)
1 when (a1 < b1) and (a2 == b2)
2 when (a1 < b1) and (a2 > b2)
3 when (a1 == b1) and (a2 < b2)
4 when (a1 == b1) and (a2 == b2)
5 when (a1 == b1) and (a2 > b2)
6 when (a1 > b1) and (a2 < b2)
7 when (a1 > b1) and (a2 == b2)
8 when (a1 > b1) and (a2 > b2)

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCpwzsagVFX4UWr64RAjzvAJ42pzpOh60YEKqDbZAmO66HdZxgiwCgxlNo
wkBTEpsQBrMPh6/aElCpYu8=
=yGOG
-----END PGP SIGNATURE-----
 
C

CBFalconer

QQ said:
Hello I have a integer range [a1 a2]
Now I have another integer range [b1 b2]
ai,bi =0,1,....,M

I wanna compare the relationship between the two ranges
for example
[a1 a2]=[0 99]
[b1 b2]=[20 80]
so there are 9 situations
.... snip ...

I think there are 3 cases if you don't have arbitrary distinctions
between the ranges. They are entirely disjoint, one encompasses
the other, and they overlap.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
 
S

Sarath

Hi Lew,

That was really nice piece of code.
I too wrote upto end but updated the array 'C' within the function
itself.
I thought of no other way of returning 2 vales from a function.
But c1*3+c2 was the answer.
I didnt got that idea.


Sarath.B
IIIT-H
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top