gpt4 book ai didi

c - 为什么在运行我的 c 程序后总是得到 0?请帮帮我

转载 作者:行者123 更新时间:2023-12-04 08:14:02 25 4
gpt4 key购买 nike

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

float distance(float a,float b,float c,float d);


int main()

{
float x1,y1,x2,y2,dist;
printf("Input x1: ");
scanf("%f", &x1);
printf("Input y1: ");
scanf("%f", &y1);
printf("Input x2: ");
scanf("%f", &x2);
printf("Input y2: ");
scanf("%f", &y2);
distance(x1,x2,y1,y2);
printf("Distance between the given points is: %.2f",sqrt(dist));

return 0;
}
float distance(float a,float b,float c,float d)
{
float x1,x2,y1,y2,dist;
dist=((x2-x1)*(x2-x1) +(y2-y1)*(y2-y1));
return dist;
}
这里输出总是0,不知道为什么。我试过把浮点数和整数仍然得到 0。
忽略这部分问题。
写这篇文章是为了满足发布的标准。

最佳答案

`x1,x2,y1,y2` and `dist` not initialized that was the problem :)

#include<stdio.h>
#include<math.h>
#define _CRT_SECURE_NO_WARNINGS

float distance(float a,float b,float c,float d);


int main()

{
float x1,y1,x2,y2,dist;
printf("Input x1: ");
scanf("%f", &x1);
printf("Input y1: ");
scanf("%f", &y1);
printf("Input x2: ");
scanf("%f", &x2);
printf("Input y2: ");
scanf("%f", &y2);
dist=distance(x1,x2,y1,y2);//you didn't put the value of the func into a variable
printf("Distance between the given points is: %.2f",sqrt(dist));

return 0;
}
float distance(float a,float b,float c,float d)//since we already give a,b,c,d a value from calling the func in main
{
float dist;
dist=((b-a)*(b-a) +(d-c)*(d-c));//we just swapped you x1,x2,y1,y2 to the a,b,c,d
return dist;
}

关于c - 为什么在运行我的 c 程序后总是得到 0?请帮帮我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65799855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com