C 数组中的最小值、最大值和位置

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。使用 minLocationmaxLocation 代替


您遇到了括号问题:

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 以避免垃圾值也被包括在内。 也为最小和最大位置使用单独的变量

对于初学者,改变这个

 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);

}