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