C# Question: Your teacher runs a lot of marathons. Write a program to allow him
ID: 3804277 • Letter: C
Question
C# Question:
Your teacher runs a lot of marathons. Write a program to allow him to find his fastest, slowest and average time of up to 10 races.
Your program should ask for times for each race in hours, minutes and seconds. After 10 races have been entered or the user has typed -1 (to indicate the end of input) you will provide the following output:
Race 1: {time}
Race 2: {time} **FASTEST**
....
....
Race {n-1}: {time} **SLOWEST**
Race {n}: {time}
Average time: {time}
You will need to store the input in an array so that you can output them at the end. Obviously the words FASTEST and SLOWEST should be on the correct line. You may not use any built in functions on the arrays to calculate the fastest, slowest and average. Average should be rounded to the closest second.
Starting C# Code~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TeacherRace
{
class Program
{
static void Main(string[] args)
{
int hours, min, sec;
double dis, mil;
double val, val2;
double pace1, paceKm;
Console.WriteLine("The time in hours minutes and seconds : ");
hours = int.Parse(Console.ReadLine());
min = int.Parse(Console.ReadLine());
sec = int.Parse(Console.ReadLine());
Console.WriteLine("The time is" + hours + ":" + min + ":" + sec);
Console.WriteLine("The distance of the race in mile:");
dis = double.Parse(Console.ReadLine());
Console.WriteLine("The distance of the race in kilometers:");
mil = dis * (1.60934);
Console.WriteLine(mil);
Console.WriteLine("The pace of the race in minutes and seconds per miles");
val = ((hours * 60 * 60) + (min * 60) + sec);
//formula to caluculate pace in min per miles is time/distance and divide the result by 60
val2 = val / dis;
pace1 = val2 / 60;
Console.WriteLine(pace1);
Console.WriteLine("The pace of the race in minutes and seconds per kilometers");
paceKm = (pace1 * 0.621371192);
Console.WriteLine(paceKm);
Console.ReadLine();
}
}
}
Need to update this code to reflect the comparison outlined in the question. Thanks!
Sample output: file:///c/users/203470/documents/visual studio 2015/Projects/ConsoleApplication eAppl X The time in hours minutes and seconds 23 43 The time isi :23:43 The distance of the race in nile 13.1 The distance of the race in kilometers: 21.082354 The pace of the race in ninutes and seconds per miles 6.39058524173028 The pace of the race in ninutes and seconds per kiloneters 3.97092556923155Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TeacherRace
{
class Program
{
static void Main(string[] args)
{
long hours, min, sec;
long time;
long total = 0;
int n;
int i;
long[] races = new long[10];
for (i = 0; i < 10; i = i + 1)
{
Console.WriteLine("Race " + (i+1) + ": The time in hours minutes and seconds : ");
hours = int.Parse(Console.ReadLine());
if (hours == -1)
{
break;
}
min = int.Parse(Console.ReadLine());
sec = int.Parse(Console.ReadLine());
time = sec + min*60 + hours*3600;
races[i] = time;
total = total + time;
}
n = i;
double average = ((double)total)/n;
long min_time = races[0];
int min_index = 0;
for(i = 1; i < n; i++)
{
if (races[i] < min_time)
{
min_time = races[i];
min_index = i;
}
}
long max = races[0];
int max_index = 0;
for(i = 1; i < n; i++)
{
if (races[i] > max)
{
max = races[i];
max_index = i;
}
}
for(i = 0; i < n; i++)
{
hours = races[i]/3600;
races[i] = races[i]%3600;
min = races[i]/60;
races[i] = races[i] % 60;
sec = races[i];
Console.Write("Race " + (i+1) + ": " + hours + ":" + min + ":" + sec);
if (i == min_index)
{
Console.Write(" **FASTEST**");
}
if (i == max_index)
{
Console.Write(" **SLOWEST**");
}
Console.WriteLine();
}
Console.WriteLine();
long avg = (long)average;
hours = avg/3600;
avg = avg%3600;
min = avg/60;
avg = avg%60;
sec = avg;
if (average > (hours*3600 + min*60 + sec))
{
sec = sec + 1;
}
Console.WriteLine("Average time: " + hours + ":" + min + ":" + sec);
Console.ReadLine();
}
}
}
// Sample run
Race 1: The time in hours minutes and seconds :
1
23
43
Race 2: The time in hours minutes and seconds :
2
36
69
Race 3: The time in hours minutes and seconds :
12
36
58
Race 4: The time in hours minutes and seconds :
sh-4.3$ mono main.exe
Race 1: The time in hours minutes and seconds :
sh-4.3$ mono main.exe
Race 1: The time in hours minutes and seconds :
1
23
43
Race 2: The time in hours minutes and seconds :
2
25
36
Race 3: The time in hours minutes and seconds :
3
54
59
Race 4: The time in hours minutes and seconds :
2
3
3
Race 5: The time in hours minutes and seconds :
-1
Race 1: 1:23:43 **FASTEST**
Race 2: 2:25:36
Race 3: 3:54:59 **SLOWEST**
Race 4: 2:3:3
Average time: 2:26:51
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.