为什么在 运行 我的 c 程序之后总是得到 0?请帮帮我
Why am I always getting 0 always after running my c program? Kindly help me out
#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.
忽略这部分问题。
编写此文件以满足 POST.
的条件
您需要使用函数调用的return值
distance(x1,x2,y1,y2);
那就是你需要写
dist = distance(x1,x2,y1,y2);
否则变量dist
仍将未初始化并在此语句中使用它
printf("Distance between the given points is: %.2f",sqrt(dist));
导致未定义的行为。
函数 distance
不使用其参数 a
、b
、c
、d
。相反,它使用其未初始化的局部变量 x1
、x2
、y1
、y2
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;
}
至少把函数写成这样
float distance( float a, float b, float c, float d )
{
return ( b - a ) * ( b - a ) + ( d - c ) * ( d - c );
}
`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;
}
将您的函数(它不使用其参数)更改为:
'''float distance(float a,float b,float c,float d)
{
float dist;
dist=((a-b)*(a-b) +(c-d)*(c-d));
return dist;
}'''
在您的代码中:
printf("Distance between the given points is: %.2f",sqrt(distance(x1,x2,y1,y2)));
#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.
忽略这部分问题。 编写此文件以满足 POST.
的条件您需要使用函数调用的return值
distance(x1,x2,y1,y2);
那就是你需要写
dist = distance(x1,x2,y1,y2);
否则变量dist
仍将未初始化并在此语句中使用它
printf("Distance between the given points is: %.2f",sqrt(dist));
导致未定义的行为。
函数 distance
不使用其参数 a
、b
、c
、d
。相反,它使用其未初始化的局部变量 x1
、x2
、y1
、y2
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;
}
至少把函数写成这样
float distance( float a, float b, float c, float d )
{
return ( b - a ) * ( b - a ) + ( d - c ) * ( d - c );
}
`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;
}
将您的函数(它不使用其参数)更改为:
'''float distance(float a,float b,float c,float d)
{
float dist;
dist=((a-b)*(a-b) +(c-d)*(c-d));
return dist;
}'''
在您的代码中:
printf("Distance between the given points is: %.2f",sqrt(distance(x1,x2,y1,y2)));