Initialize two arrays that specify n points with coordinates (X, Y) in a two-dimensional space. Describe a function that finds the distances between all points and displays them in a table.

Counts the distance only between two points. What is wrong in the code?

float alldist(int *x, int *y); int main() { int *x=new int [5] {1, 4, 6, 2, 1}; int *y=new int [5] {6, 7, 8, 9, 10}; cout << "Расстояние между всеми точками:" << alldist(x, y) << endl; } float alldist(int *x, int *y) { float d; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; i++) { d = sqrt(pow((x[i+1] - x[i]), 2) + pow((y[j+1] - y[j]), 2)); return d; } } } 
  • one
    One distance between all points? "Seven red lines. All strictly perpendicular." - Igor
  • one
    The code says - to return the first distance. Everything has arrived. If you explain what "distance between all points" means, then you can think further. - MBo
  • I like x[i] and y[j] much more. Is it between what is the distance? - Igor
  • do not judge strictly, I'm new, tell me how to fix it - marsianin
  • one
    OK. A table means you need a (two-dimensional) array. Will you use c ++ features? If so, then the vector of vectors can be returned from the function. Or maybe you don’t need to return anything - the function simply prints them in the course of the calculations. - MBo

1 answer 1

Something like this :

 void alldist(int *x, int *y, int n) { float d; for (int i = 0; i < n-1; i++) { for (int j = i + 1; j < n; j++) { d = sqrt(pow((x[j] - x[i]), 2) + pow((y[j] - y[i]), 2)); cout <<"dist "<< i << ":" << j << " " << d << " "; } cout << endl; } } 
  • console opens, but displays nothing - marsianin
  • I added the link - MBo
  • everything is working. and if the maximum distance needs to be output, then it can be done in the same code? - marsianin
  • after each calculation, you need to compare the result with the current maximum - MBo