[英]Minimum, maximum and location in an array in C
我对C很新,请理解我的疑惑是否愚蠢但是我被卡住了。 我搜索了很多,但我找不到解决问题的答案。
我的程序用来询问用户一圈比赛的圈数,然后询问每圈的时间。
然后,它应该说明最快,最慢,平均的单圈时间和比赛的总时间。 现在,总时间和平均值正在发挥作用。 最小值和最大值及其位置不是。
这是我的代码:
#include<stdio.h>
#include<cs50.h>
int main()
{
int array[100], maximum, minimum, c, laps, location = 1;
float average;
int summation;
printf("how many laps did the race had?\n");
laps = GetInt();
printf("how many time each of the %i laps took in seconds?\n", laps);
for (c = 0; c < laps; c++)
{
scanf("%d", &array[c]);
maximum = array[0];
minimum = array[0];
}
for ( c = 1; c < laps; c++)
{
if (array[c] < minimum)
{
minimum = array[c];
location = c + 1;
}
else if (array[c] > maximum)
{
maximum = array[c];
location = c + 1;
}
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
}
printf("The fastest lap was %d and had the time of %d seconds.\n", location, minimum);
printf("The slowest lap was %d and had the time of %d seconds.\n", location, maximum);
printf("The race took %d seconds\n", summation);
printf("The avegare time for lap was %.2f seconds.\n", average);
}
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
应该
int summation = 0;
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
}
average = (summation / laps);
因为在你知道总和之前计算平均值是没用的
您可以使用相同location
的两个最大和最小的位置。 请改用minLocation
和maxLocation
你有一个括号问题:
for ( c = 1; c < laps; c++)
{
if (array[c] < minimum)
{
minimum = array[c];
location = c + 1;
}
else if (array[c] > maximum)
{
maximum = array[c];
location = c + 1;
}
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
}
应该
for ( c = 1; c < laps; c++)
{
if (array[c] < minimum)
{
minimum = array[c];
location = c + 1;
}
else if (array[c] > maximum)
{
maximum = array[c];
location = c + 1;
}
}
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
将for()
循环分别放在最小值和最大值
for (c = 0; c < laps; c++)
{
scanf("%d", &array[c]);
maximum = array[0];
minimum = array[0];
}
for ( c = 1; c < laps; c++)
{
if (array[c] < minimum)
{
minimum = array[c];
min_location = c + 1;//Change this also
}
else if (array[c] > maximum)
{
maximum = array[c];
max_location = c + 1;//use separate variable
}
}//Don't nest the summation inside this
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
初始化求和为0 int summation=0
in start以避免垃圾值也被包括在内。 还可以使用单独的变量来获得最小和最大位置
首先,改变这一点
for (c = 0; c < laps; c++)
{
scanf("%d", &array[c]);
maximum = array[0];
minimum = array[0];
}
对此:
for (c = 0; c < laps; c++)
{
scanf("%d", &array[c]);
}
maximum = array[0];
minimum = array[0];
它没有产生错误的输出,但我稍后会解释你的错误。
现在看看这个:
for ( c = 1; c < laps; c++)
{
if (array[c] < minimum)
{
minimum = array[c];
location_min = c + 1;
}
else if (array[c] > maximum)
{
maximum = array[c];
location_max = c + 1;
}
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
}
你有(并且我确定你没有注意到)一个for
循环的另一个,使用相同的计数器。 我不知道你为什么会这样做。 这将产生一个逻辑错误,因为c
在内部发生变化,这不应该发生在像这样的简单for循环中。 你可以通过每次打印c
看到迭代中发生的事情,你会看到奇怪的事情发生。
让我们尝试这种方法,我们改变这个:
for ( c = 1; c < laps; c++)
{
if (array[c] < minimum)
{
minimum = array[c];
location_min = c + 1;
}
else if (array[c] > maximum)
{
maximum = array[c];
location_max = c + 1;
}
for ( c = 0; c < laps; c++)
{
summation = summation + array[c];
average = (summation / laps);
}
}
对此:
summation = array[0];
for ( c = 1; c < laps; c++)
{
summation = summation + array[c];
if (array[c] < minimum)
{
minimum = array[c];
location_min = c + 1;
}
else if (array[c] > maximum)
{
maximum = array[c];
location_max = c + 1;
}
}
average = (summation / laps);
只有当您拥有整数总和以及可用的整圈数时,您才需要除以圈数。 这就是我把它放在循环之外的原因。 就像第一个评论一样,它不会产生错误,它只是不需要做的事情。
所以,最终的代码片段是:
#include<stdio.h>
#include<cs50.h>
int main()
{
int array[100], maximum, minimum, c, laps, location_min, location_max = 1;
float average;
int summation;
printf("how many laps did the race had?\n");
laps = GetInt();
printf("how many time each of the %i laps took in seconds?\n", laps);
for (c = 0; c < laps; c++)
{
scanf("%d", &array[c]);
}
maximum = array[0];
minimum = array[0];
summation = array[0];
for ( c = 1; c < laps; c++)
{
summation = summation + array[c];
if (array[c] < minimum)
{
minimum = array[c];
location_min = c + 1;
}
else if (array[c] > maximum)
{
maximum = array[c];
location_max = c + 1;
}
}
average = (summation / laps);
printf("The fastest lap was %d and had the time of %d seconds.\n", location_min, minimum);
printf("The slowest lap was %d and had the time of %d seconds.\n", location_max, maximum);
printf("The race took %d seconds\n", summation);
printf("The avegare time for lap was %.2f seconds.\n", average);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.