Sample outputs $ ./a.out input.txt output.txt Read the following 9 players from
ID: 3849325 • Letter: S
Question
Sample outputs
$ ./a.out input.txt output.txt
Read the following 9 players from the file.
Name Goals Assists
Redden 2 0
Berglund 5 2
Stewart 4 0
Oshie 3 5
McDonald 2 4
Pietrangelo 2 7
Perron 2 6
Tarasenko 5 5
Sorted player list.
Name Goals Assists
Berglund 5 2
Tarasenko 5 5
Stewart 4 0
Oshie 3 5
Redden 2 0
McDonald 2 4
Pietrangelo 2 7
Perron 2 6
MVP was Tarasenko with 10 points
Enter a player to find: Perron
Perron has 2 goals and 6 assists
------ BONUS ------
Name Goals Assists
Tarasenko 5 5
Berglund 5 2
Stewart 4 0
Oshie 3 5
Pietrangelo 2 7
Perron 2 6
McDonald 2 4
Redden 2 0
Explanation / Answer
int main(int argc, char** argv)
{
// 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.
if(argc != 3 ){
printf("*** ERROR *** Arguments passed to function is not correct Please give input file name and output file name to process further ");
exit(1);
}
char input_file[30],output_file[30];
strcpy(input_file,argv[1]);
strcpy(output_file,argv[2]);
}
int countLinesInFile(FILE* fPtr)
{
// 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.
char c;
int count = 0;
for (c = getc(fPtr); c != EOF; c = getc(fPtr))
if (c == ' '){
count = count + 1;
}
fseek( fp, 0, SEEK_SET );
return count;
}
int findPlayerByName(char** names, char* target, int size)
{
// 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 i;
for (i=0;i<size;i++){
if(strcmp(target,names[i]) == 0){
return i;
}
}
return -1;
}
int findMVP(int* goals, int* assists, 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 i;
int mvp;
int total, max_total;
max_total = goals[0]+assists[0];
mvp = 0;
for(i=1;i<size;i++){
total = goals[i] + assists[1];
if(total > max_total){
max_total = total;
mvp = i;
}
}
return mvp;
}
void printPlayers(int* goals, int* assists, char** names, int size)
{
int i;
printf("Name Goals Assists ");
for (i=0;i<size;i++){
printf("%s %d %d",names[i],goals[i],assists[0]);
}
}
void allocateMemory(int** goals, int** assists, char*** names, int size)
{
int i;
goals[0] = malloc(sizeof(int) * size);
assists[0] = malloc(sizeof(int) * size));
names[0] = malloc(sizeof(char *) * size);
for(i=0;i<size;i++){
names[0][i] = malloc(sizeof(char) * 20); // I assume size of names is 20 maximum
}
}
void sortPlayersByGoals(int* goals, int* assists, char** names, int size)
{
int i, j, min_idx;
for (i = 0; i < size-1; i++)
{
min_idx = i;
for (j = i+1; j < size; j++)
if (goals[j] > goals[min_idx])
min_idx = j;
int temp = goals[min_idx];
goals[min_idx] = goals[i];
goals[i] = temp;
temp = assists[min_idx];
assists[min_idx] = assists[i];
assists[i] = temp;
char temp1[30];
strcpy(temp1,names[min_idx]);
strcpy(names[min_idx],names[i]);
strcpy(names[i],temp1);
}
}
void writeToFile(FILE* fPtr, int* goals, int* assists, char** names, int size)
{
int i;
fprintf(fPtr,"Names Goals Assists");
for(i=0;i<size;i++){
fprintf(fp, "%s %d %d", names[i], goals[i], assists[i]);
}
}
void readLinesFromFile(FILE* fPtr, int* goals, int* assists, char** names, int numLines)
{
char buf[200];
int i;
for(i=0;i<size;i++){
while (fgets(buf,200, ptr_file)!=NULL){
sscanf(line,"%s %d %d",names[i], &goals[i], &assists[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.