#include <stdio.h>
int main ()
{
int distance;
float avg_speed;
printf ("Enter the race distance (m): ");
scanf ("%d", &distance);
int Num_lanes;
printf ("Enter number of lanes in use:");
scanf ("%d", &Num_lanes);
float Finish_times[Num_lanes];
int i;
for (i = 0; i < Num_lanes; i++)
{
printf ("Enter finish time for lane %d (sec): ", i + 1);
scanf ("%f", &(Finish_times));
}
int Worst_time = 0;
for (i = 0; i < Num_lanes; i++)
{
if (Finish_times[i] < Worst_time)
{
Worst_time = Finish_times[i];
}
}
avg_speed = (distance / 1000) / (Worst_time / 3600);
printf ("Lane wins in %d seconds, with an average speed of %f km/h",
Worst_time, avg_speed);
return 0
}
如果有人能指导我一些错误,我可以改进,我会很感激
答案 0 :(得分:1)
此:
scanf ("%f", &(Finish_times));
错误,每次都传递整个数组的地址。相反,你的意思是&#34;将其扫描到i
:th元素&#34;,所以你需要提供第i个元素的地址:
scanf ("%f", &Finish_times[i]);
此外,请注意,如果您要循环n
个数字来计算最小值和平均值,您通常不需要自己存储数字。只需保持运行最小值和运行总和,并在完成后将后者除以n
。更简单。
答案 1 :(得分:0)
首先,您应该使用动态内存分配创建数组,即使用malloc()
或calloc()
函数或者您应该在编译时定义数组元素,即在编写代码时,决定数组大小。
第二件事,在读入数组时,您应该使用带有数组名称的下标运算符([]),如下所示:
scanf ("%f", &Finish_times[i])