【问题标题】:Code for Function功能代码
【发布时间】:2018-02-23 08:10:11
【问题描述】:

我正在学习 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;
}

【问题讨论】:

  • 首先缩进你的代码。不可读的代码是许多问题的根源。
  • “Lebron James”`有下标 0,“Tim Duncan”有下标 1 等等。
  • 我为您格式化了您的代码。请在以后的帖子中自行格式化。如果它难以阅读和理解,您可能会获得较少的帮助

标签: c arrays function typedef


【解决方案1】:

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

【讨论】:

  • 是的,我实际上没有代码的 main 我只是询问了函数的说明,但是您的回答消除了我的疑问。谢谢!
【解决方案2】:

determineBest() 返回投篮命中率最高的球员的下标(投篮次数/投篮次数)

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

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

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

@MichaelWalz 以下标为例。在这种情况下,函数determinebest() 确定应该返回“下标2”(意味着它返回整数值2),因为{"Kevin Durrant", 9,10}

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

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

【讨论】:

  • 谢谢我现在明白了!
  • 那么,请随意接受这个作为答案,因为这样做会对以后阅读这个问题的其他人有所帮助。很高兴能帮到你:-)
猜你喜欢
  • 2018-02-13
  • 2021-10-12
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多