Quarterback Rating Function with Arrays and Structs is acting strange

arrays, c++, function, loops, struct

Solution

This math is unlikely to do what you want:

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

Where you've put the cast will cast the result of the division to be `double` after the division has been performed. But, the division itself will be an integer division.

You want something more like this:

    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;

By casting one of the terms in the divide to `double`, the division itself upgrades to `double`.

Alternately, you could just declare all of these variables to be `double` and avoid the casts entirely. That would make the code much easier to follow. Or, just make `sumAttempts` into a `double`, as it is common to all of the four divides.

Problem

First of all, I should state that this is a homework assignment, so while questions that give a direct answer will give me a good grade, I would prefer to know why something doesn't work, and a reason why/how I should fix it with your solution. So here is the background for this function. I have a quarterback struct with the following information. There are ten games, which all are stored in the struct and its arrays: ``` struct QuarterBack{ string name; int completions[kNumGames]; int attempts[kNumGames]; int yards[kNumGames]; int touchdowns[kNumGames]; int interceptions[kNumGames]; }; ``` Now my goal for this problem is to use the information stored in these structs to compute the NFL Style passer ratings. For reference, wikipedia gives the following: So here is the code I am using. It has some excessive parenthesis that I was trying to use to make sure my control was correct, but other than that I am stumped as to why I am not getting more correct answers. Below the code I will post an example file and output. ``` /** * @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); } ``` Here is the example file. Ignore the 4, as it is for a separate part of the problem. Each row is a game, and it is listed as: completions, attempts, yards, touchdowns, then interceptions. ``` 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 ``` And here is the output that the function is giving me: Any help is much appreciated, and if you need more information to help me, feel free to comment and ask. Also, as this is a homework assignment, don't assume that I am just incompetent even if I make a silly mistake. I was told that Stack Overflow has no stupid questions, and I really hope that the community can live up to that.

Original source