我有以下代碼說'距離'在此函數中未初始化使用。圓的區域使用笛卡爾平面的點
這是一個代碼,它接受來自笛卡爾平面的兩個座標,並使用它們之間的距離作爲半徑來查找圓的面積。這是代碼
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Point {
int x, y;
};
double getDistance(struct Point a, struct Point b)
{
double distance;
distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
return distance;
}
int main()
{
float Area;
double distance;
struct Point a, b;
printf("Enter coordinate of point a: ");
scanf("%d %d", &a.x, &a.y);
printf("Enter coordinate of point b: ");
scanf("%d %d", &b.x, &b.y);
printf("Distance between a and b: %lf\n", getDistance(a, b));
Area= 3.14 * distance * distance;
printf("\nArea of Circle : %f", Area);
return 0;
}
難道這應該是C或C++?什麼是實際的錯誤信息,包括文件名和行號? – melpomene
getDistance()中的變量'distance'處於不同的範圍內,因此與'main()'中的變量'distance'完全無關。 – Peter
當然是的。看看'main'函數。你聲明一個名爲'distance'的變量,但是不會初始化它。然後,你做'Area = 3.14 * distance * distance;'Oops。 –