功能代码

时间:2018-02-23 08:10:11

标签: c arrays function typedef

我正在攻读关于C的测试我正在完成这项练习,我不太确定我是否得到了正确的代码。伪代码是:显示函数
的代码 int determineBest(Player playerM [], int iPlayerCnt)传递了一系列玩家和玩家数量。 determineBest 返回具有最佳拍摄百分比的玩家的下标(最高拍摄/拍摄次数)。  例如:

Player playerM[] = { {"Lebron James", 10 ,30}, 
                     {"Tim Duncan", 17,20} ,
                     {"Kevin Durrant", 9,10}
                   };

对于数据,determinebest将返回下标2。

我不确定返回下标2是什么意思

代码

tydef struct
{ 
 char szName[30];
  int iShotMade;
  int iShotAttempt;
} Player;

int determineBest(Player playerM [], int iPlayerCnt)
{
   int i, iIndex= -1;
   double dCurrent, dBest = 0.0;

   for(i = 0; i < iPlayerCnt, i++)
   {
      if(Player[i].iShotAttempt == 0)
         {continue}

      dcurrent= (double)(PlayerM[i].iShotMade/PlayerM[i].iShotAttempt);

      if(dcurrent > dbest)
         dBest=dCurrent;

      iIndex= i;
   }

   return iIndex;
}

2 个答案:

答案 0 :(得分:0)

returning Subscript 2表示它在数组中找到最佳播放器的元素。

0 --> {"Lebron James", 10 ,30}, 
1 --> {"Tim Duncan", 17,20} ,
2 --> {"Kevin Durrant", 9,10}

您应该按以下方式呼叫

int best = determineBest(playerM [], iPlayerCnt);

注意,我们无法看到您计划的main(),因此iPlayerCnt实际上可能是main()中的另一个变量,无论如何跟踪数组的大小。

然后您可以通过

访问最佳玩家的信息
playerM[best]

,如

printf("%s is the best player\n", playerM[best].szName);

答案 1 :(得分:0)

  

determineBest()返回具有最佳拍摄百分比的玩家的下标(最高拍摄/拍摄次数)

Player playerM[] = { {"Lebron James", 10 ,30}, 
                     {"Tim Duncan", 17,20} ,
                     {"Kevin Durrant", 9,10}
                   };
  

对于数据,determinebest()将返回下标2。

     

我不确定返回下标2是什么意思

@MichaelWalz关于下标的例子。在这种情况下,函数determinebest()确定&#34;下标2&#34;应返回(意味着它返回整数值2),因为{"Kevin Durrant", 9,10}

  

投篮命中率最高的球员(最高投篮次数/   试图射击)

如果不清楚,请说出来&amp;我们可以进一步解释。

相关问题