Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could anyone please help with my C Program? I can't figure out why it has a segmentation fault. Also how could I create this program

Could anyone please help with my C Program? I can't figure out why it has a segmentation fault. Also how could I create this program without a global variable(static int j=0;)? I've included screenshots of my code and directions to the program and a sample output. Thank you

image text in transcribedimage text in transcribed

Directions:

Write this program Please use only the C language

No global variable are to be used in this.

you will be using the following: file input/output, pointer, structures and malloc. You will be given 3 input files all containing instances of players structures. You will scan the information from all 3 of the input files into the SAME array. After all of the players have been scanned in from each file, you will use the calculate_slugging function to figure out each players slugging percentage and store that in the Slugging_Percentage variable in the structure. Note that you may have to typecast the variable as a double when doing the calculations. (See link at bottom of document). When the Slugging Percentages have been calculated, you will use the sort_array function(bubble sort) to sort the players in DESCENDING order by their slugging percentage. After they have been sorted, you will output the array to the output file specified on the command line argv[5]. Note that the array SHOULD BE sorted when it is output to the file.

Command Line Arguments

argv[0] - ./a.out (executable file)

argv[1] the number of players in ALL the input files combined

argv[2] input file 1

argv[3] input file 2

argv[4] input file 3

argv[5] output file

Note that each input file have the same amount of players in them, so if you are trying to figure out how many to scan in from each file try argv[1]/3.

Functions

typedef struct player {

char Fname[25];

char Lname[25];

int Singles;

int Doubles;

int Triples;

int Homeruns;

int At_Bats;

float Slugging_Percentage;

} Player;

void read_from_file(char* filename, Player* players, int index, int size);

// This function will read in size struct players from filename and add these

// the players array. The function will use index to know where to start

// writing the players to in the array.

// Parameters

//

// filename The name of the input file

// players a pointer to the array of player structures

// index The index of the array to start placing players into

// size The number of players in the input file

// Return - Nothing

void calculate_slugging(Player* players, int size);

// This function will take in an array of players and calculate their slugging

// percentage using the other variables in the structure(Singles, Doubles,

// Triples, Homeruns).

// Parameters

//

// players a pointer to the array of Players structures

// size the size of the array of structures

void sort_array(Player* players, int size);

//This function takes in an array of players and will sort this array based on Slugging

// Percentage. The formula for calculating slugging percentage can be found toward the

// bottom of the document.

// Parameters

//

// players a pointer to the array of Player structures

// size the size of the array

// Return - Nothing

void write_to_file(char* filename, Player* players, int size);

// This function will take in a structure of players and print them into the

// given output file, filename

// Parameters

//

// filename the name of the output file

// players a pointer to the array of struct players to write to the file

// size the size of the players array

// Return - Nothing

How to calculate Slugging Percentage

To calculate slugging percentage, take the number of Total Bases (singles, doubles, triples and homers) and divide by At Bats. To add up Total Bases, each single counts for one, each double is two, etc.

For instance, in 1927, Babe Ruth had 192 hits. He had 29 doubles, 8 triples and 60 home runs, adding up to 97 extra-base hits; subtracting that from 192 leaves 95 singles. So you'd add 95 + (29 2) + (8 3) + (60 4) = 417 Total Bases. Divide that by the number of his At Bats that year, which was 540 and you get a slugging percentage of .772.

Additional Notes

To find out the number of hits a person has you simply need to add the number of singles, doubles, triples and homeruns.

Use val command to check for memory leaks

Programs that do not compile will not be accepted for submission

Programs with Segmentation faults will result in a minimum 50% deduction

Programs with memory leaks will result in a minimum of 10% deduction

image text in transcribedimage text in transcribed

S 6 7 #incl #include-stdlib.h> #include-: string.h 9 typedef struct player chor Fnane[25] chor Lnane[25] int Singles; int Doubles; int Triples; int Homeruns: int At Bats; 17 float Slugging Percentage 18 Player: 19 static int 1- 28 vold read.from.filecchar fienme, Player* players, int index, int stze): 21 void calculate slugging(Player players, int size); 2 void sort array Player players, int size); 3 void write to file(char filename, Players players, int size); int nain int arge, char "argv) int size-atoiCargv[]); Player players [size]; read from file(argv[1, players, , size); read fromfileCargv[ , players,, size); read fromfileCargvE, players, , size); calculate sluggingCplayers, size); sort arrcy(players, size); Mrite, to fileCargvD1, players, size): 43 void mrite tofile(char filenane, Player players, int size) int i; FILE *p 47 fp - fopen(filename, "w for(i-; isize; i) fprint f(fp, %svest dvtk tkatkatkan , players[i].Fra esplayers[i] Lane players[i] Singles players[i] bles players[i] Triples,players[i]. eru s players[1].AtLots), void calculate_slugging Player players, int size) int i; for(i-i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions