Question: Reading data from a file is often done in order to process and aggregate it to get additional results. In this activity you will read

Reading data from a file is often done in order to process and aggregate it to get additional results. In this activity you will read in data from a file containing win/loss data from the 2011 Major League Baseball season. Specifically, the file mlb_nl_2011.txt contains data about each National League team. Each line contains a team name followed by the number of wins and number of losses during the 2011 season. You will open this file and process the information to output a list of teams followed by their win percentage (number of wins divided by the total number of games) from highest to lowest. Instructions Download the mlb_nl_2011.txt data file and the mlb.c C source files. Much of the program has already been provided for you, including a convenience function to sort the lists of teams and their win percentages as well as a function to output them

. 1. Add code to open the data file and read in the team names, wins and losses and populate the teams[] and winPercentages[] arrays with the appropriate data

2. Call the sort and output functions to sort and display your results

3. Answer the questions on your worksheet and demonstrate your working program to a lab instructor

>>> mlb_nl_2011.txt <<<

Braves 89 73 Phillies 102 60 Nationals 80 81 Mets 77 85 Marlins 72 90 Brewers 96 66 Cardinals 90 72 Reds 79 83 Pirates 72 90 Cubs 71 91 Astros 56 106 DBacks 94 68 Giants 86 76 Dodgers 82 79 Rockies 73 89 Padres 71 91

>>>mlb.c<<<

#include

#include

#include

void sortMLB(char teams[][20], double winPerc[], int numTeams);

void printMLB(char teams[][20], double winPerc[], int numTeams);

int main(void) {

int const size = 200;

int const numTeams = 16;

char fileName[] = "mlb_nl_2011.txt";

char tempBuffer[size];

char tmp[size];

char teams[numTeams][20];

double winPercentages[numTeams];

//TODO: open the file, read it line by line, tokenize it to get the

// team name, wins, and losses, and store the results into

// teams[] and winPercentages[]

//sort them

sortMLB(teams, winPercentages, numTeams);

//print them out

printMLB(teams, winPercentages, numTeams);

return 0;

}

/**

* A sorting function to sort the teams and their win percentages

* using the selection sort algorithm which successively finds the

* "maximum" element and places it at the front of the array

*/

void sortMLB(char teams[][20], double winPerc[], int numTeams) {

int i, j, max_index;

char tmp_str[100];

double tmp_dbl;

//for each element i

for(i=0; i

max_index = i;

//find the maximum element among elements i+1 thru n-2

for(j=i+1; j

if(winPerc[max_index] < winPerc[j]) {

max_index = j;

}

}

//swap the ith element and the maximum element

//in this case, elements from both arrays need to be swapped

//at the same time

tmp_dbl = winPerc[i];

winPerc[i] = winPerc[max_index];

winPerc[max_index] = tmp_dbl;

strcpy(tmp_str, teams[i]);

strcpy(teams[i], teams[max_index]);

strcpy(teams[max_index], tmp_str);

}

}

void printMLB(char teams[][20], double winPerc[], int numTeams) {

int i=0;

printf("%-12s %-10s ", "TEAM", "WIN PERC");

for(i=0; i

printf("%-12s %.3f%% ", teams[i], winPerc[i]*100.0);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!