C PROGRAM #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_LI
ID: 3849209 • Letter: C
Question
C PROGRAM #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size); void sortPlayersByGoals(int* goals, int* assists, char** names, int size); void writeToFile(FILE* fPtr, int* goals, int* assists, char** names, int size); void readLinesFromFile(FILE* fPtr, int* goals, int* assists, char** names, int numLines); // Bonus function void bonusSortPlayersByBoth(int* goals, int* assists, char** names, int size); // Main function receives two command line arguments. The first is the name // of the input file; the second is the name of the output file. If either one // is missing, or the input file cannot be opened, the program should print an // error message and end. // // You are responsible for making the output of your program match the sample // output. That means you must determine the appropriate algorithm on your own; // TAs will not provide a step-by-step list of functions to call, or how to // achieve the proper output. It is recommended that you implement main after // implementing some of the other functions; this may help you to see the purpose // of main more clearly. int main(int argc, char** argv) { } // Counts how many lines are in the file provided. After counting, // this function should rewind the file for future use. // // INPUT: fPtr - A pointer to a file opened in read mode. // RETURNS: The number of lines in the file. int countLinesInFile(FILE* fPtr) { } // Finds a player by their name. // // INPUT: names - A pointer to the strings containing the player names. // target - The name of the player to find. // size - The number of players to search through. // RETURNS: The index of the player if found; -1 otherwise. int findPlayerByName(char** names, char* target, int size) { } // Finds the most valuable player (MVP) of the players. (The MVP has the greatest goals + assists total.) // // INPUT: goals - An array containing the number of goals scored per player. // assists - An array containing the number of assists per player. // size - The number of players in each array. // RETURNS: The index of the MVP. You may assume that size > 0 and therefore, there will always be // an MVP. In the event of a tie, you may return any of the players in the tie. int findMVP(int* goals, int* assists, int size) { } // Prints the players names along with their goals and assists. // YOU MUST USE FORMATTED OUTPUT IN THIS FUNCTION. Failing to do so // will result in a loss of half of the points for this function. // Players should be printed out as follows: // // Name Goals Assists // Backes 4 2 // Pietrangelo 3 1 // // You must include the column headers in your output. THIS FUNCTION, // AND MAIN, SHOULD BE THE ONLY OUTPUT-PRODUCING FUNCTIONS IN YOUR PROGRAM. // If other functions produce output, you will lose half of the points for // those functions. It is very important not to produce errant output! // // INPUT: goals - An array containing the number of goals per player. // assists - An array containing the number of assists per player. // names - An array containing the names of each player. // size - The number of players in each array. void printPlayers(int* goals, int* assists, char** names, int size) { } // Allocates memory for the player names, their goals, and their assists. // HINT: Allocating the memory for the goals and assists is straightforward; // allocating memory for the names is going to be very similar to lab 2, with // an extra dereference to account for the fact that we are using a char***. // // INPUT: goals - A pointer to a variable which should contain the malloced memory for goals. // assists - A pointer to a variable which should contain the malloced memory for assists. // names - A pointer to a variable which should contain the malloced memory for names. // size - The number of players to malloc memory for. void allocateMemory(int** goals, int** assists, char*** names, int size) { } // Sorts the players according to the number of goals they've scored, in descending order. // You must use insertion sort or selection sort for this function. // USE OF ANY OTHER SORTING FUNCTION WILL RESULT IN NO POINTS FOR THIS FUNCTION. // Keep in mind that moving an entry in one array will require you to // move entries in the other two arrays, to keep player information synchronized. // (There are a few ways to accomplish this with names, some easier than others.) // // INPUT: goals - An array containing the number of goals per player. // assists - An array containing the number of assists per player. // names - An array containing the names of each player. // size - The number of players in each array. void sortPlayersByGoals(int* goals, int* assists, char** names, int size) { } // Writes the player information to the given file, in the same order as the input file. // You should not call this function until after you have sorted the player information, // so that the information appears in sorted order in the output file. // // INPUT: fPtr - A pointer to a file to write to. // goals - An array containing the number of goals per player. // assists - An array containing the number of assists per player. // names - An array containing the names of each player. // size - The number of players in each array. void writeToFile(FILE* fPtr, int* goals, int* assists, char** names, int size) { } // Reads the player information from the given file. Information is in the following format: // // [name] [goals] [assists] // Backes 1 5 // Pietrangelo 2 7 // // YOU MAY NOT USE FSCANF IN THIS FUNCTION! Doing so will result in no points. Instead you // must use fgets in combination with the strtok function in order to break the line into // pieces. You can read about strtok online or in your textbook; email your TA with any // questions. // // INPUT: fPtr - A pointer to a file to read from. // goals - An array to be filled with the number of goals per player. // assists - An array to be filled with the number of assists per player. // names - An array to be filled with the names of each player. // size - The number of players in each array. void readLinesFromFile(FILE* fPtr, int* goals, int* assists, char** names, int numLines) { } // BONUS FUNCTION - worth 5 extra points. You may only receive points for the bonus // if you have implemented all other functions with no major flaws. This is up to the // discretion of your TA. // // This function sorts the players according to two fields - goals and assists. // Players should be sorted according to their goals scored first, then they will // be sorted by the number of assists they have scored as a second criteria. Example: // // Tarasenko 5 5 // Berglund 5 2 // Pietrangelo 2 7 // Perron 2 6 // McDonald 2 4 // Redden 2 0 // Backes 1 5 // // Note that within each grouping of players with the same number of goals, // they are subsequently sorted by the number of assists. If two players have // the same number in each category, you may place them in any order. // // Unlike the other sort function, you may use any type of sort algorithm to // implement this function. Note that this does not excuse you from implementing // the other sort function with the proper algorithm. It will probably be easiest // to simply call the other sort function from within this one, and then perform // the sub-sort afterwards. void bonusSortPlayersByBoth(int* goals, int* assists, char** names, int size) { }
Explanation / Answer
Here is the completed code with output .All functions are implemented and also bonus function.
Since you have not given the output format, I have implemented main() to call the functions using menu.
Output is shown below. Please post a comment if you need any help. I shall respond to comment.
Please don't forget to rate the answer if it helped. Thank you very much.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_LINE 100
#define MAX_NAME 30
int countLinesInFile(FILE* fPtr);
int findPlayerByName(char** names, char* target, int size);
int findMVP(int* goals, int* assists, int size);
void printPlayers(int* goals, int* assists, char** names, int size);
void allocateMemory(int** goals, int** assists, char*** names, int size);
void sortPlayersByGoals(int* goals, int* assists, char** names, int size);
void writeToFile(FILE* fPtr, int* goals, int* assists, char** names, int size);
void readLinesFromFile(FILE* fPtr, int* goals, int* assists, char** names, int numLines);
// Bonus function
void bonusSortPlayersByBoth(int* goals, int* assists, char** names, int size);
// Main function receives two command line arguments. The first is the name
// of the input file; the second is the name of the output file. If either one
// is missing, or the input file cannot be opened, the program should print an
// error message and end.
//
// You are responsible for making the output of your program match the sample
// output. That means you must determine the appropriate algorithm on your own;
// TAs will not provide a step-by-step list of functions to call, or how to
// achieve the proper output. It is recommended that you implement main after
// implementing some of the other functions; this may help you to see the purpose
// of main more clearly.
int main(int argc, char** argv)
{
FILE *input, *output;
int size;
int *goals, *assists;
char **names;
char playerName[MAX_NAME];
int choice = 0;
int index;
if(argc != 3)
{
printf(" Program needs 2 arguments : input and output filenames ");
exit(1);
}
input = fopen(argv[1], "r");
output = fopen(argv[2], "w");
if(input == NULL)
{
printf(" Could not open input file %s", argv[1]);
exit(1);
}
size = countLinesInFile(input);
allocateMemory(&goals, &assists, &names, size);
readLinesFromFile(input, goals, assists, names, size);
while(choice != 5)
{
printf(" 1. Find player by name");
printf(" 2. Find MVP");
printf(" 3. Sort players by goals");
printf(" 4. Sort players by goals and assists");
printf(" 5. Write to file and exit");
printf(" Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf(" Enter player name: ");
scanf("%s", playerName);
index = findPlayerByName(names, playerName, size);
if(index == -1)
printf(" Player not found ");
else
printf(" Found player - Name: %s Goals: %d Score: %d ", names[index], goals[index], assists[index]);
break;
case 2:
index = findMVP(goals, assists, size);
printf(" MVP - Name: %s Goals: %d Score: %d ", names[index], goals[index], assists[index]);
break;
case 3:
sortPlayersByGoals(goals, assists, names, size);
printf(" The players sorted by goals are ");
printPlayers(goals, assists, names, size);
break;
case 4:
bonusSortPlayersByBoth(goals, assists, names, size);
printf(" The players sorted by goals and assists are ");
printPlayers(goals, assists, names, size);
break;
case 5:
writeToFile(output, goals, assists, names, size);
break;
}
}
fclose(input);
fclose(output);
}
// Counts how many lines are in the file provided. After counting,
// this function should rewind the file for future use.
//
// INPUT: fPtr - A pointer to a file opened in read mode.
// RETURNS: The number of lines in the file.
int countLinesInFile(FILE* fPtr)
{
int lines=0;
char line[MAX_LINE];
while(fgets(line, MAX_LINE, fPtr))
{
lines++;
}
fseek(fPtr, 0, 0);
printf(" Finished counting %d",lines);
return lines;
}
// Finds a player by their name.
//
// INPUT: names - A pointer to the strings containing the player names.
// target - The name of the player to find.
// size - The number of players to search through.
// RETURNS: The index of the player if found; -1 otherwise.
int findPlayerByName(char** names, char* target, int size)
{
int i;
for(i = 0; i < size; i++)
{
if(strcmp(target, names[i]) == 0)
return i;
}
return -1;
}
// Finds the most valuable player (MVP) of the players. (The MVP has the greatest goals + assists total.)
//
// INPUT: goals - An array containing the number of goals scored per player.
// assists - An array containing the number of assists per player.
// size - The number of players in each array.
// RETURNS: The index of the MVP. You may assume that size > 0 and therefore, there will always be
// an MVP. In the event of a tie, you may return any of the players in the tie.
int findMVP(int* goals, int* assists, int size)
{
int total = goals[0] + assists[0]; // assume the 1st player to be mvp
int i, mvp = 0;
for(i = 1; i < size; i ++) //start from 2nd and loop
{
if(goals[i]+assists[i] > total)
{
total = goals[i] + assists[i];
mvp = i;
}
}
return mvp;
}
// Prints the players names along with their goals and assists.
// YOU MUST USE FORMATTED OUTPUT IN THIS FUNCTION. Failing to do so
// will result in a loss of half of the points for this function.
// Players should be printed out as follows:
//
// Name Goals Assists
// Backes 4 2
// Pietrangelo 3 1
//
// You must include the column headers in your output. THIS FUNCTION,
// AND MAIN, SHOULD BE THE ONLY OUTPUT-PRODUCING FUNCTIONS IN YOUR PROGRAM.
// If other functions produce output, you will lose half of the points for
// those functions. It is very important not to produce errant output!
//
// INPUT: goals - An array containing the number of goals per player.
// assists - An array containing the number of assists per player.
// names - An array containing the names of each player.
// size - The number of players in each array.
void printPlayers(int* goals, int* assists, char** names, int size)
{
int i;
printf(" %-40s %-10s %-10s", "Name", "Goals", "Assists");
for( i = 0; i < size; i++)
{
printf(" %-40s %-10d %-10d",names[i], goals[i], assists[i]);
}
}
// Allocates memory for the player names, their goals, and their assists.
// HINT: Allocating the memory for the goals and assists is straightforward;
// allocating memory for the names is going to be very similar to lab 2, with
// an extra dereference to account for the fact that we are using a char***.
//
// INPUT: goals - A pointer to a variable which should contain the malloced memory for goals.
// assists - A pointer to a variable which should contain the malloced memory for assists.
// names - A pointer to a variable which should contain the malloced memory for names.
// size - The number of players to malloc memory for.
void allocateMemory(int** goals, int** assists, char*** names, int size)
{
int i;
*goals = malloc(size * sizeof(int));
*assists = malloc(size * sizeof(int));
*names = malloc(size * sizeof(char *) );
for(i = 0 ; i < size; i++)
{
(*names)[i] = malloc(MAX_NAME);
}
}
// Sorts the players according to the number of goals they've scored, in descending order.
// You must use insertion sort or selection sort for this function.
// USE OF ANY OTHER SORTING FUNCTION WILL RESULT IN NO POINTS FOR THIS FUNCTION.
// Keep in mind that moving an entry in one array will require you to
// move entries in the other two arrays, to keep player information synchronized.
// (There are a few ways to accomplish this with names, some easier than others.)
//
// INPUT: goals - An array containing the number of goals per player.
// assists - An array containing the number of assists per player.
// names - An array containing the names of each player.
// size - The number of players in each array.
void sortPlayersByGoals(int* goals, int* assists, char** names, int size)
{
//sorting using selection sort in descending order of goals
int maxIdx = 0;
int i, j, temp;
char *tempName;
for(i = 0; i < size; i++)
{
maxIdx = i;
for(j = i+1; j < size; j++)
{
if(goals[j] > goals[maxIdx] )
maxIdx = j;
}
if(maxIdx != i)
{
//swap
temp = goals[i];
goals[i] = goals[maxIdx];
goals[maxIdx] = temp;
temp = assists[i];
assists[i] = assists[maxIdx];
assists[maxIdx] = temp;
tempName = names[i];
names[i] = names[maxIdx];
names[maxIdx] = tempName;
}
}
}
// Writes the player information to the given file, in the same order as the input file.
// You should not call this function until after you have sorted the player information,
// so that the information appears in sorted order in the output file.
//
// INPUT: fPtr - A pointer to a file to write to.
// goals - An array containing the number of goals per player.
// assists - An array containing the number of assists per player.
// names - An array containing the names of each player.
// size - The number of players in each array.
void writeToFile(FILE* fPtr, int* goals, int* assists, char** names, int size)
{
int i;
for(i = 0 ; i < size; i++)
{
fprintf(fPtr, "%s %d %d ",names[i], goals[i], assists[i]);
}
}
// Reads the player information from the given file. Information is in the following format:
//
// [name] [goals] [assists]
// Backes 1 5
// Pietrangelo 2 7
//
// YOU MAY NOT USE FSCANF IN THIS FUNCTION! Doing so will result in no points. Instead you
// must use fgets in combination with the strtok function in order to break the line into
// pieces. You can read about strtok online or in your textbook; email your TA with any
// questions.
//
// INPUT: fPtr - A pointer to a file to read from.
// goals - An array to be filled with the number of goals per player.
// assists - An array to be filled with the number of assists per player.
// names - An array to be filled with the names of each player.
// size - The number of players in each array.
void readLinesFromFile(FILE* fPtr, int* goals, int* assists, char** names, int numLines)
{
char line[MAX_LINE];
char *token;
int i;
char sep[2]=" ";
for(i = 0; i < numLines ; i++)
{
fgets(line,MAX_LINE, fPtr);
//printf(" %s",line);
token = strtok(line, sep);
strcpy(names[i], token);
token = strtok(NULL, sep);
goals[i] = atoi(token);
token = strtok(NULL, sep);
assists[i] = atoi(token);
}
}
// BONUS FUNCTION - worth 5 extra points. You may only receive points for the bonus
// if you have implemented all other functions with no major flaws. This is up to the
// discretion of your TA.
//
// This function sorts the players according to two fields - goals and assists.
// Players should be sorted according to their goals scored first, then they will
// be sorted by the number of assists they have scored as a second criteria. Example:
//
// Tarasenko 5 5
// Berglund 5 2
// Pietrangelo 2 7
// Perron 2 6
// McDonald 2 4
// Redden 2 0
// Backes 1 5
//
// Note that within each grouping of players with the same number of goals,
// they are subsequently sorted by the number of assists. If two players have
// the same number in each category, you may place them in any order.
//
// Unlike the other sort function, you may use any type of sort algorithm to
// implement this function. Note that this does not excuse you from implementing
// the other sort function with the proper algorithm. It will probably be easiest
// to simply call the other sort function from within this one, and then perform
// the sub-sort afterwards.
void bonusSortPlayersByBoth(int* goals, int* assists, char** names, int size)
{
int maxIdx = 0;
int i, j, temp;
char *tempName;
for(i = 0; i < size; i++)
{
maxIdx = i;
for(j = i+1; j < size; j++)
{
if(goals[j] > goals[maxIdx] )
maxIdx = j;
else if(goals[j] == goals[maxIdx])
{
if(assists[j] > assists[maxIdx])
maxIdx = j;
}
}
if(maxIdx != i)
{
//swap
temp = goals[i];
goals[i] = goals[maxIdx];
goals[maxIdx] = temp;
temp = assists[i];
assists[i] = assists[maxIdx];
assists[maxIdx] = temp;
tempName = names[i];
names[i] = names[maxIdx];
names[maxIdx] = tempName;
}
}
}
input file players.txt
Tarasenko 5 5
Perron 2 6
McDonald 2 4
Pietrangelo 2 7
Redden 2 0
Backes 1 5
Berglund 5 2
output
./a.out players.txt output.txt
Finished counting 7
1. Find player by name
2. Find MVP
3. Sort players by goals
4. Sort players by goals and assists
5. Write to file and exit
Enter your choice: 3
The players sorted by goals are
Name Goals Assists
Tarasenko 5 5
Berglund 5 2
McDonald 2 4
Pietrangelo 2 7
Redden 2 0
Perron 2 6
Backes 1 5
1. Find player by name
2. Find MVP
3. Sort players by goals
4. Sort players by goals and assists
5. Write to file and exit
Enter your choice: 4
The players sorted by goals and assists are
Name Goals Assists
Tarasenko 5 5
Berglund 5 2
Pietrangelo 2 7
Perron 2 6
McDonald 2 4
Redden 2 0
Backes 1 5
1. Find player by name
2. Find MVP
3. Sort players by goals
4. Sort players by goals and assists
5. Write to file and exit
Enter your choice: 1
Enter player name: Perron
Found player - Name: Perron Goals: 2 Score: 6
1. Find player by name
2. Find MVP
3. Sort players by goals
4. Sort players by goals and assists
5. Write to file and exit
Enter your choice: 1
Enter player name: Sam
Player not found
1. Find player by name
2. Find MVP
3. Sort players by goals
4. Sort players by goals and assists
5. Write to file and exit
Enter your choice: 5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.