Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:19:02+00:00 2026-06-12T13:19:02+00:00

The following code calculate the nearest distance in two points. The part if(j==0) is

  • 0

The following code calculate the nearest distance in two points.
The part if(j==0) is tested redundantly for UsedServices.Count-1 time,
is there any way not to introduce this redudancy?
Of course we can separate the case out from for loop, I am just thinking is there a more elegant way to achieve this.

double[] nearestDistant=new double[UnUsedServices.Count];

for (int i=0;i<UnUsedServices.Count;i++)
{
    for (int j=0;j<UsedServices.Count;j++)
    {
        double distance=GetDistance(UnUsedServices[i].coords, 
                                    UsedServices[j].coords);

        if (j==0) //Used once and redundant for UsedServices.Count-1 time!
        {
            nearestDistant[i] = distance;
        }
        else
        {
            nearestDistant[i] = Math.Min(nearestDistant[i], distance);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:19:04+00:00Added an answer on June 12, 2026 at 1:19 pm

    You could initialize nearestDistant[i] to Double.MaxValue before the inner loop and then you can remove the if.

    The side effect of this is when UsedServices.Count == 0 the nearestDistant[i] would be set to Double.MaxValue. If that’s ok with you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code calculate the distance between two geo points and return the distance
I created the following code to calculate the duration between two timestamps which can
I need to calculate the time complexity of the following code: for (i =
I have the following code to calculate the execution time. $TimeTaken['start'] = microtime(); require_once
I implemented the following code that use the data points in dat to calculate
I am using the following code snippet to calculate a total price. This works
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
I have the following code, to calculate the max in a range of cells:
i am using the following code(based on this http://goo.gl/5HhSx ) to calculate the difference
Please your opinion on the following code. I need to calculate the diff in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.