473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

What's the best algorithm for it in C

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!

Nov 14 '05 #1
3 1220
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

QQ wrote:
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)

iD8DBQFCpwzsagVFX4UWr64RAjzvAJ42pzpOh60YEKqDbZAmO6 6HdZxgiwCgxlNo
wkBTEpsQBrMPh6/aElCpYu8=
=yGOG
-----END PGP SIGNATURE-----
Nov 14 '05 #2
QQ wrote:

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)
Nov 14 '05 #3
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

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Wahoo | last post by:
Dear All, What is the name of sorting algorithm used in list (STL) class? and how it work? Thanks!!! Best Regards, Wahoo
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
12
by: nib | last post by:
What kind of problems is c best at solving?
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
22
by: vietor.liu | last post by:
is this? int hashpjw(char* str,int size) { unsigned long h=0,g; for(unsigned char *p=(unsigned char*)str;*p;++p) { h=(h<<4)+*p; if(g=(h&0xf0000000)) {
5
by: Tor Erik | last post by:
I would be surprised if it is the naive: m = 0 s1 = "me" s2 = "locate me" s1len = len(s1) s2len = len(s2) found = False while m + s1len <= s2len:
1
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
9
by: Jeff Dege | last post by:
I've been programming in C++ for a good long while, but there are aspects of the language I've never needed, and hence never bothered to really learn. It's the curse of working on a developed...
20
by: pratap | last post by:
Could someone clarify how could one reduce the size of an executable code during compile time. Could one use specific compile time flag with makefile or is it advisable to go for dynamic linking....
69
by: Yee.Chuang | last post by:
When I began to learn C, My teacher told me that pointer is the most difficult part of C, it makes me afraid of it. After finishing C program class, I found that all the code I wrote in C contains...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.