【问题标题】:How do I code the c program that calculates the Euclidean distance in matrix form? [duplicate]如何编写以矩阵形式计算欧几里得距离的 c 程序? [复制]
【发布时间】:2020-12-01 00:21:31
【问题描述】:

从用户那里取笛卡尔坐标系(P0,P1,P2,P3,P4)中5个点的X和Y坐标,放到一个数组中(不是两个数组,肯定是一个数组)我想编写一个程序,将其放入二维欧几里德距离数组中,如下所示。

Array of coordinates (vector with 10 elements) 
X0 Y0 X1 Y1 X2 Y2 X3 Y3 X4 Y4


 Euclidean distance array (matrix in 5x5 form) 
    P1 -> P0 Distance
    P2 -> P0 Distance   P2 -> P1 Distance 
    P3 -> P0 Distance   P3 -> P1 Distance  P3 -> P2 Distance
    P4 -> P0 Distance   P4 -> P1 Distance  P4 -> P2 Distance  P4 -> P3 Distance

对于数字 1 到 10,我只是得到这个打印输出。

1 2 3 4 5 6 7 8 9 10 

我的代码

#include <stdio.h>
#include <conio.h>
#include <math.h>



int main() {
        int k[5][2],s[5][2];
        int i=0,j=0,p=0,z=0,x1,x2,y1,y2;
        for(i=0;i<5;i++){
        for(j=0;j<2;j++){
                printf("enter coordinates");
                scanf("%d",&k[i][j]);
            }
        }
        for(i=0;i<5;i++){
            for(j=0;j<2;j++){
                printf("%d\t",k[i][j]);
        }
        }
    return 0;
} 

【问题讨论】:

  • 是的,相似度差不多,但我还是不知道怎么用我最后说的方式打印出来,所以是5 X 5矩阵。

标签: c


【解决方案1】:

计算距离:

int getDistance (int x1, int y1, int x2, int y2)
{
    double distance = pow(x2 - x1, 2) + pow(y2 - y1, 2);
    distance = sqrt(distance);

    return (int)distance;
}

使用嵌套循环填充距离数组:

int distances [5][5];

for (i = 0; i < 5; i++) //rows
{
    for (j = 0; j < 5; j++) //cols
    {
        distances[i][j] = getDistance(k[i][0], k[i][1], k[j][0], k[j][1]);
    }
}

和另一个嵌套循环打印距离数组:

    for (i = 0; i < 5; i++) //rows
{
    for (j = 0; j < 5; j++) //cols
    {
        printf("%d ", distances[i][j]);
    }
    printf("\n");
}

完整代码:

#include <stdio.h>
#include <conio.h>
#include <math.h>

int getDistance (int x1, int y1, int x2, int y2)
{
    double distance = pow(x2 - x1, 2) + pow(y2 - y1, 2);
    distance = sqrt(distance);

    return (int)distance;
}

int main() {

    int k[5][2],s[5][2];
    int i=0,j=0,p=0,z=0,x1,x2,y1,y2;

    for(i=0;i<5;i++){
        for(j=0;j<2;j++){
            printf("enter coordinates");
            scanf("%d",&k[i][j]);
        }
    }

    //populate array
    int distances [5][5];

    for (i = 0; i < 5; i++) //rows
    {
        for (j = 0; j < 5; j++) //cols
        {
            distances[i][j] = getDistance(k[i][0], k[i][1], k[j][0], k[j][1]);
        }
    }

    //print distances
    for (i = 0; i < 5; i++) //rows
    {
        for (j = 0; j < 5; j++) //cols
        {
            printf("%d ", distances[i][j]);
        }
        printf("\n");
    }

    for(i=0;i<5;i++){
        for(j=0;j<2;j++){
            printf("%d\t",k[i][j]);
        }
        printf("\n");
    }
    return 0;
}

结果:

0 2 5 8 11
2 0 2 5 8
5 2 0 2 5
8 5 2 0 2
11 8 5 2 0

【讨论】:

  • pow(x2 - x1, 2) 有整数溢出的风险。 pow(0.0 + x2 - x1, 2) 没有。当然,最终的return (int)distance; 无论如何都会溢出。
猜你喜欢
  • 2020-06-16
  • 2016-08-02
  • 2018-03-28
  • 2018-06-14
  • 2015-01-13
  • 2014-05-08
  • 2012-06-27
  • 2014-11-18
相关资源
最近更新 更多