Question

Using c 3 File Input & Data Processing Reading data from a file is often done in order to pro...

using c

3 File Input & Data Processing

Reading data from a file is often done in order to process and aggregate it to get ad-

ditional 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

data/mlb_nl_2011.txt

contains data about each National League team. Each line contains a team name fol-

lowed 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

1. Open 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

3

percentages as well as a function to output them.

2. 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

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

4. Answer the questions on your worksheet and demonstrate your working program

to a lab instructor

mlb_nl_2011.txt file

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 <stdio.h>
#include <stdlib.h>
#include <string.h>


void sortMLB(char **teams, double *winPerc, int numTeams);
void printMLB(char **teams, double *winPerc, int numTeams);

int main(int argc, char **argv) {
int const maxSize = 50;
int const numTeams = 16;
char filePath[] = "data/mlb_nl_2011.txt";
char tempBuffer[100];

int i;
char **teams = (char **)malloc(sizeof(char *) * numTeams);
double *winPercentages = (double *)malloc(sizeof(double) * numTeams);
for (i = 0; i < numTeams; i++) {
teams[i] = (char *)malloc(sizeof(char) * maxSize);
}

// 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 winPercentagesp[]

// sort them
sortMLB(teams, winPercentages, numTeams);
// print them out
printMLB(teams, winPercentages, numTeams);

return 0;
}

void sortMLB(char **teams, double *winPerc, int numTeams) {
int i, j, max_index;
char tmp_str[100];
double tmp_dbl;
// for each element i
for (i = 0; i < numTeams - 1; i++) {
max_index = i;
// find the maximum element among elements i+1 thru n-2
for (j = i + 1; j < numTeams; 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; forgo swapping if it is in-place
if(i != max_index) {
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, double *winPerc, int numTeams) {
int i = 0;
printf("%-12s %-10s\n", "TEAM", "WIN PERC");
for (i = 0; i < numTeams; i++) {
printf("%-12s %.3f%%\n", teams[i], winPerc[i] * 100.0);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void sortMLB(char **teams, double *winPerc, int numTeams);

void printMLB(char **teams, double *winPerc, int numTeams);

int main(int argc, char **argv)

{

int const maxSize = 50;

int const numTeams = 16;

char filePath[] = "mlb_nl_2011.txt";

char tempBuffer[100];

FILE *fptr;

char* token;

int i;

char line[BUFSIZ];

int win,loss,total;

double per;

char **teams = (char **)malloc(sizeof(char *) * numTeams);

double *winPercentages = (double *)malloc(sizeof(double) * numTeams);

for(i = 0; i < numTeams; i++)

{

teams[i] = (char *)malloc(sizeof(char) * maxSize);

}

// 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 winPercentagesp[]

if ((fptr = fopen(filePath, "r")) == NULL)

{

printf("Error! opening file");

// Program exits if file pointer returns NULL.

exit(1);

}

i=0;

while (fgets(line, sizeof line, fptr) != NULL)

{

token = strtok(line, " ");

strcpy(teams[i], token);

token = strtok(NULL, " ");

win=atoi(token);

token = strtok(NULL, " ");

loss=atoi(token);

total=win+loss;

per=(double)win/(double)total;

winPercentages[i]=per;

i++;

}

// sort them

sortMLB(teams, winPercentages, numTeams);

// print them out

printMLB(teams, winPercentages, numTeams);

return 0;

}

void sortMLB(char **teams, double *winPerc, int numTeams)

{

int i, j, max_index;

char tmp_str[100];

double tmp_dbl;

// for each element i

for (i = 0; i < numTeams - 1; i++)

{

max_index = i;

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

for (j = i + 1; j < numTeams; 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; forgo swapping if it is in-place

if(i != max_index)

{

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, double *winPerc, int numTeams)

{

int i = 0;

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

for (i = 0; i < numTeams; i++)

{

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

}

}

output

CAWINDOWslsystem32 cmd.exe 59 . 259% 58-025% 55.556% 54.938% 53.886% 58.932% 49.689% 48 . 765% 47.531% 45.862% 44-444% 44 . 4

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Using c 3 File Input & Data Processing Reading data from a file is often done in order to pro...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT