Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中数组中的最小值、最大值和位置_C_Arrays_Cs50 - Fatal编程技术网

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

C中数组中的最小值、最大值和位置,c,arrays,cs50,C,Arrays,Cs50,我对C很陌生,如果我的怀疑看起来很愚蠢,但我被卡住了,请理解。我搜索了很多,但我找不到解决我问题的答案 我的程序可以询问用户一场比赛的圈数,然后询问每圈的时间 然后,它应该说明最快、最慢、每圈的平均时间和比赛的总时间。现在,总时间和平均值都在起作用。最小值和最大值及其位置不相同 这是我的密码: #include<stdio.h> #include<cs50.h> int main() { int array[100], maximum, minimum, c, laps

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

}
#包括
#包括
int main()
{
整数数组[100],最大值,最小值,c,圈数,位置=1;
浮动平均;
整数求和;
printf(“比赛进行了多少圈?\n”);
laps=GetInt();
printf(“每%i圈用了多少秒?\n”,圈数);
对于(c=0;c最大值)
{
最大值=数组[c];
位置=c+1;
}
对于(c=0;c
应该是

 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最大值)
{
最大值=数组[c];
位置=c+1;
}
}
对于(c=0;c
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);
 }
for(c=0;c最大值)
{
最大值=数组[c];
max_location=c+1;//使用单独的变量
}
}//不要将总和嵌套在这个
对于(c=0;c
在开始时将求和初始化为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];
 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);
求和=数组[0];
对于(c=1;c最大值)
{
最大值=数组[c];
位置_max=c+1;
}
}
平均值=(总和/圈);
你只需要除以圈数,只有当你有完整的总和和完整的圈数可用时。这就是为什么我把它放在一个循环之外。就像第一条评论一样,它不会产生错误,只是一些不需要做的事情

因此,最后的代码段是:

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

}
#包括
#包括
int main()
{
整数数组[100],最大值,最小值,c,圈数,位置最小值,位置最大值=1;
浮动平均;
整数求和;
printf(“比赛进行了多少圈?\n”);
laps=GetInt();
printf(“每%i圈用了多少秒?\n”,圈数);
对于(c=0;c最大值)
{
最大值=数组[c];
位置_max=c+1;
}
}
平均值=(总和/圈);
printf(“最快的圈数为%d,时间为%d秒。\n”,位置\u min,最小值);
printf(“最慢的圈数为%d,时间为%d秒。\n”,位置\u max,最大值);
printf(“比赛花费了%d秒\n”,总和);
printf(“每圈的avegare时间为%.2f秒。\n”,平均值);
}

它也应该在另一个环井之外!谢谢现在程序成功地指向了最大值和最小值!但仍然存在位置问题。还是找不到正确的答案才意识到你说的话!有两个变量!为maxlocation打开,为inlocation打开!非常感谢大家!程序正在运行!:)@托马斯和杰克也一样(看来我只能标记一个人)@LuisFernandes,是的,你只能标记一个人,但因为这是我的帖子,我会得到通知的。(哦,别忘了投票支持所有帮助你的答案)我需要声誉才能投票。。。转到它的wotk,我将在我能够完成后返回。您忘记将
求和初始化为零。为了防止使用
scanf
重复读取
10
,请在
%d
之前添加一个空格(例如
scanf(“%d”)和数组[c]);
)因此,您没有将输入缓冲区中的
换行符作为
数组[c]
读取。(空格导致
scanf
在读取数字之前跳过所有空格,包括
换行符)。。。我的总和和平均值是唯一有效的,而现在它们不起作用了。。。所以,我不确定你建议的代码。但是谢谢你的帮助!很抱歉,我已经修复了它,我没有注意到您的循环从
c=1开始
我已经解决了for-in-a-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];
          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 = 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);

}