功能代码

标签 c arrays function typedef

我正在学习 C 语言测试,我正在完成这个练习,但我不太确定我是否获得了正确的代码。伪代码是:显示函数的代码
int certainBest(Player playerM [], int iPlayerCnt) 传递一个玩家数组和玩家计数。 defineBest 返回投篮命中率最高的球员的下标(最高出手次数/出手次数)。 示例:

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

对于数据,definebest 将返回下标 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 --> {"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);

关于功能代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48943671/

相关文章:

c - 在 C 中调用其他较大函数的函数使用静态内联

c - 如何在C中使用一条printf语句多次打印变量

jquery - 使用 jquery 将哈希数组传递到服务器

c++ - 如果将 delete[] 应用于非数组指针会发生什么?

MySQL 查询显示下个月内的所有小时

c - tee 总是返回 EINVAL

c - 数据类型* <变量名> 和。数据类型 *<变量名>

c++ - 如何在 x86 程序集中递增数组?

javascript - 当您不知道属性是变量还是函数时的 javascript 设计模式

JavaScript:使用名称和默认值设置对象参数的正确方法