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

时间:2015-04-02 08:26:47

标签: c arrays cs50

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

}

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

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

答案 2 :(得分:1)

对于初学者,请更改此

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

}