Solved

C# algorithm advice

Posted on 2004-10-20
6
187 Views
Last Modified: 2010-08-05
Hi Experts,

I am trying to develop an algorithm which involves the selection of an immediate minimum and maximum limit when an input is provided.

For instance I have a list of numbers which increment equally;

-20, -16, -12, -8, -4, 0, 4, 8, 12, 16, 20

If this algorithm recieves an input of 3, then as I know the set of numbers increment equally in 4 then the minimum and maximum to extract is 0 and 4 respectively. If the input is -13 then the minimum and maximum is -16 and -12 respectively.

Is there an algorithm already out there that implements, if not can someone guide to an eficient way of developing this?

Thanking you in advance.

regards Imran

0
Comment
Question by:imran89
  • 3
  • 3
6 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 125 total points
ID: 12363380
If the sequence is always evenly spaced and centered on zero, you can do:

   int min;
   int max;
   int n;
   int k;
   // n = test number
   // k = spacing (4 in your example)

   int m = n%k;
   if ( m < 0 ) m += 4;
   min = n-m;
   max = n-m+4;

If the series is offset from 0, then you need to do a bit more computation to adjust:

   max += offset;
   min += offset;
   if ( min > n )
   {
      max = min;
      min = max - k;
   }
   else if ( max < n )
   {
      min = max;
      max = min + k;
   }
0
 

Author Comment

by:imran89
ID: 12363867
Hi drichards

Thankyou for your prompt reply. I am going to implement this with some more test cases that I have.

Would this method work when decimal values are involved with the same condition that they are equally spaced?

regards Imran
0
 
LVL 19

Expert Comment

by:drichards
ID: 12369860
You can use a similar algorithm with decimal types using the decimal.Remainder method rather than the % opoerator:

    decimal d; // test number
    decimal k; // spacing
    ...
    decimal d2 = decimal.Remainder(d,k);
    if ( d2 < 0 ) d2 = k+d2;
    decimal min = d-d2;
    decimal max = min + k;

For doubles you would have to write your own remainder function as I didn't find one in .NET - maybe it's there and I just didn't find it.  Be careful about rounding errors though.

    double d; // test number
    double k; // spacing
    ...
    double d2 = -Math.Floor(d/k)*k + d;  // Compute remainder
    if ( d2 < 0 ) d2 = k+d2;
    double min = d-d2;     // Be careful about rounding here
    double max = min + k; // and here
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:imran89
ID: 12372898
Hi drichards,

I presume the remainder functionality doest apply to floats either?

regards Imran
0
 
LVL 19

Expert Comment

by:drichards
ID: 12384488
Correct.  You can convert floats to doubles and use the double formula.
0
 

Author Comment

by:imran89
ID: 12414442
Hi drichards,

I applied your algorithm to my application and it seems to work and cover all conditions.

Thanks for your help.

regards
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now