C# algorithm advice

Posted on 2004-10-20
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

Question by:imran89
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 19

Accepted Solution

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;

Author Comment

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
LVL 19

Expert Comment

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Author Comment

ID: 12372898
Hi drichards,

I presume the remainder functionality doest apply to floats either?

regards Imran
LVL 19

Expert Comment

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

Author Comment

ID: 12414442
Hi drichards,

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

Thanks for your help.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connect Gridview column to Textbox in C# 2 54
Windows Universal App 22 55
Can Selenium do Load Testing? 2 58
Health check of winows and webservces 5 50
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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