鱼C论坛

 找回密码
 立即注册
查看: 967|回复: 2

[已解决]空间中两点间距离

[复制链接]
发表于 2021-3-8 18:13:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<math.h>
int main()
{
        float GPS1[3],GPS2[3];
        int i=0,j=0;
        float d;
        printf("Please input the first coordinate of the point:\n");
        for(i;i<3;i++)
        {
                scanf("%f",GPS1[i]);
        }
        printf("Please input the second coordinate of the point");
        for(j;j<3;j++)//从这开始代码就没有继续运行下去了,为什么?如何改进?
        {
                printf("--------");
                scanf("%f",GPS2[j]);
        }
        d=sqrt((GPS1[0]-GPS2[0])*(GPS1[0]-GPS2[0])
        +(GPS1[1]-GPS2[1])*(GPS1[1]-GPS1[1])
        +(GPS1[2]-GPS2[2])*(GPS1[2]-GPS2[2]));
        printf("The distance of the two points is %f",d);
        return 0;
}
最佳答案
2021-3-8 18:33:32
        scanf() 读取数值给出的参数必须是变量地址,不可以直接使用变量本身。
#include<stdio.h>
#include<math.h>
int main(void)
{
        float d , e , GPS[2][3]                                                                    ;
        int i , j                                                                                  ;
        for(i = 0 ; i < 2 ; i ++) {
                printf("Please input the No. %d coordinate of the point:\n" , i + 1)               ;
                for(j = 0 ; j < 3 ; j ++) scanf("%f" , & GPS[i][j])                                ;
        }
        for(e = 0.0 , i = 0 ; i < 3 ; i ++) e += (GPS[1][i] - GPS[0][i]) * (GPS[1][i] - GPS[0][i]) ;
        d = sqrt(e)                                                                                ;
        printf("The distance of the two points is : %.6f\n" , d)                                   ;
}
        vc6.0 环境编译、运行实况
D:\0002.Exercise\C>cl x.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

x.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:x.exe
x.obj

D:\0002.Exercise\C>x
Please input the No. 1 coordinate of the point:
0 0 0
Please input the No. 2 coordinate of the point:
10 10 10
The distance of the two points is : 17.320508

D:\0002.Exercise\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-8 18:32:27 | 显示全部楼层
两个输入函数没有&符号
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-8 18:33:32 | 显示全部楼层    本楼为最佳答案   
        scanf() 读取数值给出的参数必须是变量地址,不可以直接使用变量本身。
#include<stdio.h>
#include<math.h>
int main(void)
{
        float d , e , GPS[2][3]                                                                    ;
        int i , j                                                                                  ;
        for(i = 0 ; i < 2 ; i ++) {
                printf("Please input the No. %d coordinate of the point:\n" , i + 1)               ;
                for(j = 0 ; j < 3 ; j ++) scanf("%f" , & GPS[i][j])                                ;
        }
        for(e = 0.0 , i = 0 ; i < 3 ; i ++) e += (GPS[1][i] - GPS[0][i]) * (GPS[1][i] - GPS[0][i]) ;
        d = sqrt(e)                                                                                ;
        printf("The distance of the two points is : %.6f\n" , d)                                   ;
}
        vc6.0 环境编译、运行实况
D:\0002.Exercise\C>cl x.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

x.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:x.exe
x.obj

D:\0002.Exercise\C>x
Please input the No. 1 coordinate of the point:
0 0 0
Please input the No. 2 coordinate of the point:
10 10 10
The distance of the two points is : 17.320508

D:\0002.Exercise\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 11:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表