【问题标题】:Quarterback Rating Function with Arrays and Structs is acting strange具有数组和结构的四分卫评分函数表现得很奇怪
【发布时间】:2013-12-17 19:43:18
【问题描述】:

首先,我应该声明这是一项家庭作业,所以虽然给出直接答案的问题会给我一个好成绩,但我更愿意知道为什么 i> 某些东西不起作用,以及我应该/如何用您的解决方案修复它的原因。

所以这里是这个函数的背景。我有一个包含以下信息的四分卫结构。共有十个游戏,它们都存储在结构及其数组中:

struct QuarterBack{
    string name;
    int completions[kNumGames];
    int attempts[kNumGames];
    int yards[kNumGames];
    int touchdowns[kNumGames];
    int interceptions[kNumGames];
};

现在我解决这个问题的目标是使用存储在这些结构中的信息来计算 NFL 风格的传球手评分。作为参考,维基百科给出了以下内容:


这是我正在使用的代码。它有一些过多的括号,我试图用它来确保我的控制是正确的,但除此之外,我很难理解为什么我没有得到更多正确的答案。在代码下面我将发布一个示例文件并输出。

/**
 * @brief printPasserRating prints the passer rating of all players
 * @param players is the array holding all the players
 */
void printPasserRating(QuarterBack *players, int numPlayers){
    for(int player = 0; player < numPlayers; player++){
        double passerRating = 0;

        int sumCompletions = 0, sumAttempts = 0, sumYards = 0,
                sumTouchdowns = 0, sumInterceptions = 0;


        for(int game = 0; game < kNumGames; game++){
            sumCompletions += players[player].completions[game];
            sumAttempts += players[player].attempts[game];
            sumYards += players[player].yards[game];
            sumTouchdowns += players[player].touchdowns[game];
            sumInterceptions += players[player].interceptions[game];
        }


        double a = 0, b = 0, c = 0, d = 0;
        double nums[4] = {a, b, c, d};


        nums[0] = static_cast<double>((sumCompletions / sumAttempts) - 0.3) * 5;
        nums[1] = static_cast<double>((sumYards / sumAttempts) - 3) * 0.25;
        nums[2] = static_cast<double>(sumTouchdowns / sumAttempts) * 20;
        nums[3] = 2.375 - (static_cast<double>(sumInterceptions / sumAttempts) * 25);


        for(int letter = 0; letter < 4; letter++){
            nums[letter] = mm(nums[letter]);
        }

        passerRating = (nums[0] + nums[1] + nums[2] + nums[3]) / 0.06;
        cout << players[player].name << "\t" << passerRating << endl;
    }

    showMenu(players, numPlayers);
}

这是示例文件。忽略 4,因为它是问题的一个单独部分。每一行都是一场比赛,它被列为:完成、尝试、码数、达阵,然后是拦截。

4
Peyton Manning              
27  42  462 7   0
30  43  307 2   0
32  37  374 3   0
28  34  327 4   0
33  42  414 4   1
28  42  295 2   1
29  49  386 3   1
30  44  354 4   3
25  36  330 4   0
24  40  323 1   0
Tom Brady               
29  52  288 2   1
19  39  185 1   0
25  36  225 2   1
20  31  316 2   0
18  38  197 0   1
25  43  269 1   1
22  46  228 0   1
13  22  116 1   1
23  33  432 4   0
29  40  296 1   1
Drew Brees              
26  35  357 2   1
26  46  322 1   2
29  46  342 3   1
30  39  413 4   0
29  35  288 2   0
17  36  236 2   1
26  34  332 5   0
30  51  382 2   2
34  41  392 4   0
30  43  305 1   1
Eli Manning             
24  35  360 1   2
25  46  340 2   3
26  44  350 3   1
34  35  460 1   2
25  36  240 2   3
16  34  250 3   1
24  35  360 1   0
35  56  340 2   2
36  44  350 3   0
34  45  360 1   1

这是函数给我的输出:


非常感谢任何帮助,如果您需要更多信息来帮助我,请随时发表评论和提问。另外,由于这是一项家庭作业,即使我犯了一个愚蠢的错误,也不要认为我只是无能。有人告诉我,Stack Overflow 没有愚蠢的问题,我真的希望社区能够做到这一点。

【问题讨论】:

  • 期望输出是什么?
  • @JoeZ,虽然我的老师没有给出测试用例的具体答案,但他给出了接近 100 的近似值。另外,我的最后两个答案不应该相同。
  • 提示:您可以使用 std::accumulate 进行求和:std::accumulate(players[player].completions, players[player].completions + kNumGames, 0)。我还建议更喜欢 std::vecotr 而不是数组。
  • @MichaWiedenmann 非常感谢您提供的建议。关于数组,因为这是一个介绍性的编程课程,我们还没有介绍向量,但我相信我们会在下学期。连我的导师都说他不喜欢阵列,但既然阵列存在,他就必须教给我们。

标签: c++ arrays function loops struct


【解决方案1】:

我认为问题在于这样的代码:

static_cast<double>((sumCompletions / sumAttempts) - 0.3)

这里,sumCompletionssumAttemptsints。当您尝试对双精度进行强制转换以避免整数除法时,强制转换是在表达式的完整值上而不是在分子或分母上。这意味着执行的除法是整数除法,然后减去 0.3,然后将已经是 double 的结果转换为 double

要解决此问题,请转换分子或分母,而不是商本身:

static_cast<double>(sumCompletions) / sumAttempts - 0.3

希望这会有所帮助!

【讨论】:

  • 虽然两个答案都是正确的,但乔兹先回答了,所以我要把正确的答案给他。不过感谢您的帮助!总是很高兴看到乐于助人的用户做他们最擅长的事情。
  • @zsherman- 也许我弄错了,但我想我在他之前回答了。 :-)
【解决方案2】:

这个数学不太可能达到你想要的效果:

    nums[0] = static_cast<double>((sumCompletions / sumAttempts) - 0.3) * 5;
    nums[1] = static_cast<double>((sumYards / sumAttempts) - 3) * 0.25;
    nums[2] = static_cast<double>(sumTouchdowns / sumAttempts) * 20;
    nums[3] = 2.375 - (static_cast<double>(sumInterceptions / sumAttempts) * 25);

执行除法后,您放置演员表的位置会将除法结果转换为double。但是,除法本身将是一个整数除法。

你想要更像这样的东西:

    nums[0] = (static_cast<double>(sumCompletions) / sumAttempts - 0.3) * 5.0;
    nums[1] = (static_cast<double>(sumYards) / sumAttempts - 3) * 0.25;
    nums[2] = (static_cast<double>(sumTouchdowns) / sumAttempts) * 20.0;
    nums[3] = 2.375 - (static_cast<double>(sumInterceptions) / sumAttempts) * 25.0;

通过将分界线中的一项转换为double,分界线本身升级为double

或者,您可以将所有这些变量声明为 double 并完全避免强制转换。这将使代码更容易理解。或者,只需将sumAttempts 变为double,因为它是所有四个划分的共同点。

【讨论】:

  • 我犯这个错误太频繁了,总是错过它。谢谢提醒,我现在就去试试。
  • 看起来解决了我的问题。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 2014-02-05
  • 1970-01-01
  • 2023-01-07
  • 2020-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多