Assignment 5 Hints

This assignment, like most, is not easy.  It requires an organized approach and verifying that you are on-track.  These hints, hopefully will help with that effort.

First of all, here is the main function that I used for the assignment.

int main()
{
    NFL_Team teams[NumTeams];

    getNFL_Teams(TeamFile, teams);
    // printTeams(teams);          // output for verification only

    processGames(GameFile, teams);
    // printTeams(teams);          // output for verification only

    sortForStandings(teams);
    // printTeams(teams);          // output for verification only

    printStandings(teams);
}

Notice that I broke the problem down into 4 steps (functions).
  1. Read and store the teams file into the array of structs
  2. Read and process the scores file.  This requires parsing each line, determining the winner and loser (or tie) and updating the correct array element's won, loss, or tie members.
  3. Sort the teams array.  
  4. Print the final standings
Notice that I left in place, but commented out, a call to the printTeams function.  This is so I can verify the results of each step.  The verification function looks like this:

void printTeams(NFL_Team* teams)
{
    cout << setprecision(3) << fixed << left;
    for (int i = 0; i < NumTeams; i++)
    {
        cout << setw(25) << teams[i].name
             << setw(9) << teams[i].conference
             << setw(6) << teams[i].division
             << setw(2) << teams[i].wins
             << setw(2) << teams[i].losses
             << setw(2) << teams[i].ties
             << setw(8) << percent(teams[i]) << endl;
    }
}

The results from the first 3 steps should look something like this:

Buffalo Bills            American East  0 0 0 nan
Miami Dolphins           American East  0 0 0 nan
New England Patriots     American East  0 0 0 nan
New York Jets            American East  0 0 0 nan
Baltimore Ravens         American North 0 0 0 nan
Cincinnati Bengals       American North 0 0 0 nan
Cleveland Browns         American North 0 0 0 nan
Pittsburgh Steelers      American North 0 0 0 nan
Houston Texans           American South 0 0 0 nan
Indianapolis Colts       American South 0 0 0 nan
Jacksonville Jaguars     American South 0 0 0 nan
Tennessee Titans         American South 0 0 0 nan
Denver Broncos           American West  0 0 0 nan
Kansas City Chiefs       American West  0 0 0 nan
Las Vegas Raiders        American West  0 0 0 nan
Los Angeles Chargers     American West  0 0 0 nan
Dallas Cowboys           National East  0 0 0 nan
New York Giants          National East  0 0 0 nan
Philadelphia Eagles      National East  0 0 0 nan
Washington Football Team National East  0 0 0 nan
Chicago Bears            National North 0 0 0 nan
Detroit Lions            National North 0 0 0 nan
Green Bay Packers        National North 0 0 0 nan
Minnesota Vikings        National North 0 0 0 nan
Atlanta Falcons          National South 0 0 0 nan
Carolina Panthers        National South 0 0 0 nan
New Orleans Saints       National South 0 0 0 nan
Tampa Bay Buccaneers     National South 0 0 0 nan
Arizona Cardinals        National West  0 0 0 nan
Los Angeles Rams         National West  0 0 0 nan
San Francisco 49ers      National West  0 0 0 nan
Seattle Seahawks         National West  0 0 0 nan

Buffalo Bills            American East  4 2 0 0.667
Miami Dolphins           American East  3 3 0 0.500
New England Patriots     American East  2 3 0 0.400
New York Jets            American East  0 6 0 0.000
Baltimore Ravens         American North 5 1 0 0.833
Cincinnati Bengals       American North 1 4 1 0.250
Cleveland Browns         American North 4 2 0 0.667
Pittsburgh Steelers      American North 5 0 0 1.000
Houston Texans           American South 1 5 0 0.167
Indianapolis Colts       American South 4 2 0 0.667
Jacksonville Jaguars     American South 1 5 0 0.167
Tennessee Titans         American South 5 0 0 1.000
Denver Broncos           American West  2 3 0 0.400
Kansas City Chiefs       American West  5 1 0 0.833
Las Vegas Raiders        American West  3 2 0 0.600
Los Angeles Chargers     American West  1 4 0 0.200
Dallas Cowboys           National East  2 4 0 0.333
New York Giants          National East  1 5 0 0.167
Philadelphia Eagles      National East  1 4 1 0.250
Washington Football Team National East  1 5 0 0.167
Chicago Bears            National North 5 1 0 0.833
Detroit Lions            National North 2 3 0 0.400
Green Bay Packers        National North 4 1 0 0.800
Minnesota Vikings        National North 1 5 0 0.167
Atlanta Falcons          National South 1 5 0 0.167
Carolina Panthers        National South 3 3 0 0.500
New Orleans Saints       National South 3 2 0 0.600
Tampa Bay Buccaneers     National South 4 2 0 0.667
Arizona Cardinals        National West  4 2 0 0.667
Los Angeles Rams         National West  4 2 0 0.667
San Francisco 49ers      National West  3 3 0 0.500
Seattle Seahawks         National West  5 0 0 1.000

Buffalo Bills            American East  4 2 0 0.667
Miami Dolphins           American East  3 3 0 0.500
New England Patriots     American East  2 3 0 0.400
New York Jets            American East  0 6 0 0.000
Pittsburgh Steelers      American North 5 0 0 1.000
Baltimore Ravens         American North 5 1 0 0.833
Cleveland Browns         American North 4 2 0 0.667
Cincinnati Bengals       American North 1 4 1 0.250
Tennessee Titans         American South 5 0 0 1.000
Indianapolis Colts       American South 4 2 0 0.667
Jacksonville Jaguars     American South 1 5 0 0.167
Houston Texans           American South 1 5 0 0.167
Kansas City Chiefs       American West  5 1 0 0.833
Las Vegas Raiders        American West  3 2 0 0.600
Denver Broncos           American West  2 3 0 0.400
Los Angeles Chargers     American West  1 4 0 0.200
Dallas Cowboys           National East  2 4 0 0.333
Philadelphia Eagles      National East  1 4 1 0.250
New York Giants          National East  1 5 0 0.167
Washington Football Team National East  1 5 0 0.167
Chicago Bears            National North 5 1 0 0.833
Green Bay Packers        National North 4 1 0 0.800
Detroit Lions            National North 2 3 0 0.400
Minnesota Vikings        National North 1 5 0 0.167
Tampa Bay Buccaneers     National South 4 2 0 0.667
New Orleans Saints       National South 3 2 0 0.600
Carolina Panthers        National South 3 3 0 0.500
Atlanta Falcons          National South 1 5 0 0.167
Seattle Seahawks         National West  5 0 0 1.000
Los Angeles Rams         National West  4 2 0 0.667
Arizona Cardinals        National West  4 2 0 0.667
San Francisco 49ers      National West  3 3 0 0.500

Sorting the array of NFL_Teams

The Program Notes suggest using a "sortkey" function to sort the array of NFL_Teams.  Because the array of structs must be sorted on 3 levels, using such a function can greatly simplify this process.  In prior examples or assignments or exercises sorting of arrays was accomplished by making comparisons of elements in an array and swapping elements (or rows for a 2D array) if some condition was met.  Using a "sortkey" function means that you will make comparisons using the return of a function applied to elements of the teams array.  That comparison might look something like this:

 if (sortKey(array[i]) < sortKey(array[j]))

The following is an approximation of how you might write a "sortkey" function of the assignment.  This function takes one team as a argument and returns a float number.  That return value is used in the sorting process.

float sortKey(const NFL_Team& team)
{
    char firstCharOfConference = team.conference[0];
    char firstCharOfDivision = team.division[0];
    int conferenceAsAnInt = static_cast<int>(firstCharOfConference);
    int divisionAsAnInt = static_cast<int>(firstCharOfDivision);
    float pctInReverseOrder = 1 - percent(team);
    float key = 100 * conferenceAsAnInt + divisionAsAnInt + pctInReverseOrder;
    return key;
}

In the above function firstCharOfConference  represents the first character of the conference name.  firstCharOfDivision  represents the first character of the division name. Those two char values are converted to (ascii code) int values,  conferenceAsAnInt and divisionAsAnInt.  The local variable, pctInReverseOrder, yields a float value less than one that causes a larger percent value to be evaluated as a smaller number.  And finally, the key is the sum of conferenceAsAnInt multiplied by 100 and the divisionAsAnInt and the pctInReverseOrder.

The following output shows the array of struct values along with the return from the "sortkey" function.

Buffalo Bills            American East  4 2 0 0.667   6569.333
Miami Dolphins           American East  3 3 0 0.500   6569.500
New England Patriots     American East  2 3 0 0.400   6569.600
New York Jets            American East  0 6 0 0.000   6570.000
Pittsburgh Steelers      American North 5 0 0 1.000   6578.000
Baltimore Ravens         American North 5 1 0 0.833   6578.167
Cleveland Browns         American North 4 2 0 0.667   6578.333
Cincinnati Bengals       American North 1 4 1 0.250   6578.750
Tennessee Titans         American South 5 0 0 1.000   6583.000
Indianapolis Colts       American South 4 2 0 0.667   6583.333
Jacksonville Jaguars     American South 1 5 0 0.167   6583.833
Houston Texans           American South 1 5 0 0.167   6583.833
Kansas City Chiefs       American West  5 1 0 0.833   6587.167
Las Vegas Raiders        American West  3 2 0 0.600   6587.400
Denver Broncos           American West  2 3 0 0.400   6587.600
Los Angeles Chargers     American West  1 4 0 0.200   6587.800
Dallas Cowboys           National East  2 4 0 0.333   7869.667
Philadelphia Eagles      National East  1 4 1 0.250   7869.750
New York Giants          National East  1 5 0 0.167   7869.833
Washington Football Team National East  1 5 0 0.167   7869.833
Chicago Bears            National North 5 1 0 0.833   7878.167
Green Bay Packers        National North 4 1 0 0.800   7878.200
Detroit Lions            National North 2 3 0 0.400   7878.600
Minnesota Vikings        National North 1 5 0 0.167   7878.833
Tampa Bay Buccaneers     National South 4 2 0 0.667   7883.333
New Orleans Saints       National South 3 2 0 0.600   7883.400
Carolina Panthers        National South 3 3 0 0.500   7883.500
Atlanta Falcons          National South 1 5 0 0.167   7883.833
Seattle Seahawks         National West  5 0 0 1.000   7887.000
Los Angeles Rams         National West  4 2 0 0.667   7887.333
Arizona Cardinals        National West  4 2 0 0.667   7887.333
San Francisco 49ers      National West  3 3 0 0.500   7887.500

If the sorting is then performed on the "sortkey" value, it will result in the correct order for the final output.