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);
}
}
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
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.
Using c 3 File Input & Data Processing Reading data from a file is often done in order to pro...
The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...
In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...
I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...
I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...
Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...
Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...
Concurrent Key-Value Database Implement a non-persistent, concurrent key-value database using the Reader/Writers algorithm. - Use a hashmap as the underlying data structure. Inspiration is ok, however the implementation must be yours. The hashmap must be able to grow to fit new elements, but it does not need to reduce its size. -- Linked lists? Positional arrays? All are fine. - Keys and values are strings (char *) - Operations are: get, put Provided files: - Implement the contract set in...
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...