#### [c++]; Non-Overlapping Spheres c++ [closed]

I’m trying to do a program which eliminates those spheres who overlap knowing their (x,y,z) coordinates and their radius. My code is the following:

`````` i=0;
vector<double>::iterator h = r.begin();
vector<double>::iterator w;

while (h != r.end())
{
j=0;
w = r.begin();
while(w != r.end())
{
d.insert(d.begin() + i, sqrt(pow(xcentro[i] - xcentro[j], 2) + pow(ycentro[i] - ycentro[j], 2) + pow(zcentro[i] - zcentro[j], 2)));
if ((d[i] < (r[i] + r[j])) && (r[i] >= r[j]) && (i != j))
{
//hacer 0 la esfera j-esima
r[j] = 0;
xcentro[j] = 0;
ycentro[j] = 0;
zcentro[j] = 0;
}
else if ((d[i] < (r[i] + r[j])) && (r[i] < r[j]))
{
//hacer 0 la esfera i-esima
r[i] = 0;
xcentro[i] = 0;
ycentro[i] = 0;
zcentro[i] = 0;
}
else{
cout << "." << endl;
}
j++;
w++;
}
i++;
h++;
}
r.erase(std::remove(r.begin(),r.end(), 0), r.end());
xcentro.erase(std::remove(xcentro.begin(), xcentro.end(), 0), xcentro.end());
ycentro.erase(std::remove(ycentro.begin(), ycentro.end(), 0), ycentro.end());
zcentro.erase(std::remove(zcentro.begin(), zcentro.end(), 0), zcentro.end());
``````

However, the code eliminates more spheres than it should and it doesn’t eliminate those vectors whose components are cero. What could be wrong?

Source: Windows Questions C++