Need help distance.

Hi everyone, i am trying to find the distance of 3 dimensional bodies. I am able to find the two bodies furthest from each other, but i cannot find the bodies closest to each other.

it displays 0(it's body to itself). is there a way to negate the program from comparing it to itself?

The program works but it always find the closest body as itself.
part of the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
    for(int l=0; l<size; l++){
        cout<<s[l].getName()<<endl;
        
        int smallest=0;
        for(int q=1;q<size;q++){

                if(s[l].getDistance(smallest)>s[l].getDistance(q)){
                    smallest=q;
                    
                }
        }
        
        cout<<"The smallest distance is between: "<<s[smallest].getName()<<" "<<s[l].getName()<<" "<<s[l].getDistance(smallest)<<endl;
        
        cout<<endl;
    }
There are special cases whenever l is equal to q. In those cases, your if statement evaluates to something similar to this:
s[l].getDistance(smallest)>0 //Distance to the same body is 0.

Maybe you could use another if statement in the inner loop to avoid those comparisons?

-Albatross
Last edited on
Topic archived. No new replies allowed.