I am trying to calculate the euclidean distance in a matrix in C, I saw a similar post and tried it on my own matrix but as it is different I do not know how to make it work. This is my code:
#include <stdio.h>
#include <math.h>
int main()
{
double distance[3][3] = {0};
double myArray[3][9] = {{50, 10, 10, 20, 10, 0, 0, 0, 0},
{50, 0, 0, 0, 0, 20, 10, 15, 5},
{70, 10, 0, 10, 0, 0, 10, 0, 0}};
int i;
int j, k;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 9; j++)
{
double temp = 0.0f;
for (k = 0; k < 9; k++)
{
temp += pow(myArray[i][k] - myArray[j][k], 2);
}
distance[i][j] = (int)sqrt(temp);
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%f ", distance[i][j]);
if (j == 2)
{
printf("\n");
}
}
}
return 0;
}
The result is close to being correct, but it displays like this:
0.000000 38.000000 28.000000
38.000000 0.000000 35.000000
28.000000 35.000000 0.000000
*** stack smashing detected ***: terminated
Any help is appreciated!