Question

This program will determine the finals time for a swimmer completing the 100 yard freestyle race...

This program will determine the finals time for a swimmer completing the 100 yard freestyle race

The data file consists of the initials of the swimmer followed by the 4 "split" times in seconds. A 100-yard freestyle race is broken in to 4 25-yard lengths (AKA "splits").

Your program will:

  • read the initials of the swimmers
  • read the 4 split times -- the split times are in seconds
  • compute the total of all 4 splits
  • display the initials of the swimmer and the total in minutes and seconds (including the decimal portion)
    for example: if the swimmer's total was 110.34 seconds, this would be written as 1:50.34 (1 minute, 50.34 seconds --- the colon (:) separates the minutes from the seconds.
  • determine the winner of this race (the winner of the race is the one with the lowest time)

DATA FILE

PT 24.73 26.07 27.08 27.05

YA 24.55 26.09 26.52 25.98

SY 24.96 26.26 26.71 27.00

PROGRAM LANGUAGE C

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

This is the resulting program

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

int main(void)
{
//declairing variables to access file
FILE * fp;
size_t len = 0;
ssize_t read;
//declairing variables that store the lines of the file and words of the line
char * line = NULL;
char * word = NULL;
//declairing variable that will hold the data of the winner
char * winner_name=NULL;
double winner_time=0;

//opening file to read
fp = fopen("data.txt", "r");

//reading file line by line untill no line is left in the file
while ((read = getline(&line, &len, fp)) != -1)
{
//every time strtok function is called it takes next word
//here strtok divides the words of the line on the basis of space
word = strtok (line," ");
  
//since the first word is name we enter is value to a name variable
char * name=word;
  
//declairing time values of the player
double total_time = 0;
  
//taking next one word at a time which are all double type numbers
for(int i=0; i<4;i++)
{
//taking word
word = strtok (NULL, " ");
//converting it to double type
double times = atof(word);
//and adding in total
total_time = total_time +times;
  
}
  
//storing name of the player and its time in winner entries if he has less time than the current winner or if he is the first entry
if(winner_time == 0 || total_time<winner_time){
winner_name = name;
winner_time = total_time;
}
}
  
//converting time to minutes
int minutes = ((int)winner_time)/60;
//getting the remaining seconds by subtracting seconds that are already covered in miutess
double seconds = winner_time - (minutes*60);
  
printf("Winner Name: %s and Winning time: %d:%.2f\n", winner_name, minutes, seconds);
}

Output:

Rate if it helps :)

Add a comment
Know the answer?
Add Answer to:
This program will determine the finals time for a swimmer completing the 100 yard freestyle race...
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