Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I have a list of Array of Points, i have to calculate min distance compare each & every element with one another to see
it will be done through the nested loop ?

(0.2,0.4,0.6)
(0.3,0.5,0.25)
(0.456,0.2345,0.12)
(0.178,0.124,0.98)
(0.123,0.456,0.123)

I have min distance formula only struggling to loop in efficient way. Please help

What I have tried:

for(int i = 0; i < 4; i++)
    for(int j = 1; j < i; j++)
Posted
Updated 26-Mar-24 23:23pm
Comments
Graeme_Grant 27-Mar-24 5:05am    
hmmm... Sounds like homework...

1 solution

Your outer loop needs to run from 0 to the second-to-last element of the list.

Your inner loop needs to run from outer loop + 1 to the last element of the list.

Eg:
C#
for (int i = 0; i < list.Count - 1; i++)
{
    for (int j = i + 1; j < list.Count; j++)
    {
        // Compare list[i] to list[j] here...
    }
}
This will compare every pair of elements from the list, without comparing the same pair twice.
 
Share this answer
 
Comments
CPallini 27-Mar-24 5:40am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900