gpt4 book ai didi

c - 声明的变量未显示在调试器的局部变量中

转载 作者:行者123 更新时间:2023-11-30 18:01:55 25 4
gpt4 key购买 nike

这是一个计算行长度的简单程序。程序的所有计算方面都已完成并正在运行。但我在一个非常简单的方面遇到了麻烦,变量“percent_detoured”在第 10 行声明并在第 83 行使用,并在第 84 行打印。问题是执行时,其他一切都运行良好,但 percent_detoured 始终返回 0.000。调试时,percent_detoured 甚至不会在“Locals”中显示为变量。如果有帮助的话,我正在使用 Pelles C IDE。

感谢您的帮助!

{
float line_value;
int x_1, y_1, x_2, y_2;
float y_intercept;
float distance, greatest_distance, total;
float percent_detoured; // <-- DECLARED HERE (SCROLL ALL THE WAY DOWN TO SEE WHERE IT IS USED)
int count, detour_count;

x_1=1; y_1=1; x_2=1; y_2=1;
greatest_distance = 0; total = 0; count = 0; detour_count = 0;

/* Ask for user input in setting the coordinates of the line */
printf("Enter in the length of the line\n");
scanf("%f", &line_value);


/* If user enters in y coordinates for the line such that the line length is greater
than 10 units, then enter loop and continue looping until the length is smaller or
equal to 10 */
while ( line_value > 10 || line_value < 0)
{
printf("Try Again. Value should be smaller or equal to 10 units\n");
scanf("%f", &line_value);
}

line_value = line_value / 2; //Define the top of the line

printf("Enter x and y coordinates for point 1 and 2\n");
scanf("%d %d %d %d", &x_1, &y_1, &x_2, &y_2);

if ( x_1 == 0 && x_2 == 0 && y_1 == 0 && y_2 == 0 ) //If user enters in 0 0 0 0 right away, then immediately end the program
{
printf("No path lengths have been calculated\n");
}

else
{
/* While the user does not enter in the values 0 0 0 0 for all coordinates, continue while loop */
while ( (x_1 != 0) || (y_1 != 0) || (x_2 != 0) || (y_2 != 0) )
{
y_intercept = y_1 - ( (y_2 - y_1) / (x_2 - x_1) * x_1 ); //Rearrange (y=mx+b) to get (b=y-mx) where b is the y intercept

if ( y_intercept <= line_value && y_intercept >= 0 ) //If the y-intercept of the line from point a to b is between
{ //the top of L and 0, then calculate distance between a and top of L
distance = sqrt( pow(x_1, 2) + pow((y_1 - line_value), 2) )
+ sqrt( pow(x_2, 2) + pow((line_value - y_2), 2) );
detour_count++; //Updating the detour counter
}
else if ( y_intercept >= ( line_value * -1) && y_intercept < 0 ) //If the y-intercept of the line from point a to b is between
{ //the bottom of L and 0, then calculate distance between a and bottom of L
distance = sqrt( pow(x_1, 2) + pow( (y_1 + line_value), 2) )
+ sqrt( pow(x_2, 2) + pow(( y_2 - (line_value * -1) ), 2) );
detour_count++; //Updating the detour counter
}
else
{
distance = sqrt( pow((x_2 - x_1), 2) + pow((y_2 - y_1), 2)); //If shortest path between point a and b does not cross L,
} //then the distance is just the sum of squares of rise and run

if (distance > greatest_distance)
{
greatest_distance = distance; //If the current distance is greater than the previous, set it as the greatest distance
}

total = total + distance; //Total count of distance
count++; //Update general counter

printf("The shortest path is: %f\n", distance);

printf("Enter x and y coordinates for point 1 and 2\n");
scanf("%d %d %d %d", &x_1, &y_1, &x_2, &y_2);
}

}

printf("\nThe average path length: %f\n", (total/count));
printf("The length of the longest path: %f\n", greatest_distance);

percent_detoured = ((detour_count/count) * 100); // <-- USED IT HERE
printf("The percentage of paths involving a detour: %f\n", percent_detoured ); // <-- SHOULD BE PRINTED AS ITS VALUE HERE

return 0;

}

最佳答案

你应该使用

percent_detoured = ((float)detour_count/count) * 100.0);

否则,detour_count 和 count 都是整数,int 除以 int 会得到一个 int。因此,假设 detour_count < count,detour_count/count 将四舍五入为 0,乘​​以 100(仍然为 0),最后四舍五入为 float ,因为它存储为 float (percent_detoured)。

我从未使用过你提到的 IDE,所以我不知道为什么它可能没有显示在调试器中。 :)

关于c - 声明的变量未显示在调试器的局部变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9547937/

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