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:
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
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 :)
This program will determine the finals time for a swimmer completing the 100 yard freestyle race...