This is for C programming 1 Your teacher enjoys running race
This is for C# programming:
1. Your teacher enjoys running races of different distances. Write a program to allow him to calculate his pace for a race.
Your program should ask for following:
The time in hours minutes and seconds
The distance of the race
If that distance is in km or miles
and should output the following:
The time input
The distance in miles
The distance in km
The pace of the race in minutes and seconds per mile
The pace of the race in minutes and seconds per km.
Paces should be displayed to the nearest seconds
Distances should be displayed to 1 decimal place.
Your calculations should be done at full precision.
e.g.
1:53:30 (1 hour, 53 minutes, 30 seconds)
for 13.1 miles (a half marathon)
is 8:40 per mile (8 minute and 40 seconds)
Your output should look like this
Time: 1:53:30
Distance in miles: 13.1
Distance in km: 21.1
Pace in minutes per mile: 8:40
Pace in minutes per km: 5:23
2. 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.
Solution
using System;
public class Test
{
public static void Main()
{
double miles,kms;
miles = kms = 0;
int totalmins,paceMinutes,paceSeconds;
paceMinutes = paceSeconds = 0;
Console.WriteLine(\"Enter time in hours minutes and seconds\");
int hours= Convert.ToInt32(Console.ReadLine()); //input hours,minutes and seconds
int minutes = Convert.ToInt32(Console.ReadLine());
int seconds = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(\"Time: \"+hours+\":\"+minutes+\":\"+seconds);
Console.WriteLine(\"Enter the distance of the race\"); //input distance
double distance = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(\"If distance is in km or miles <enter miles or kms>\");
string unit = Console.ReadLine();
if(unit == \"miles\")
{
miles = distance;
kms = 1.60934 * miles;
}
else if(unit == \"kms\")
{
kms = distance;
miles = 0.621371 * kms;
}
else
Console.WriteLine(\"Invalid option\");
Console.WriteLine(\"Distance in miles: \"+miles);
Console.WriteLine(\"Distance in km: \"+kms);
totalmins = hours*60+minutes+seconds/60;
if(unit == \"miles\")
{
paceMinutes = (int)(totalmins/miles);
paceSeconds = (int)((totalmins-paceMinutes)*60);//convert to seconds
}
else if(unit == \"kms\")
{
paceMinutes = (int)(totalmins/kms); //compute pace in minutes and seconds
paceSeconds = (int)((totalmins-paceMinutes)*60);
}
else
Console.WriteLine(\"Wrong unit for distance\");
Console.WriteLine(\"Pace in minutes per \"+unit+\": \"+ paceMinutes+\" minutes\"+paceSeconds+\"seconds\");
}
}
output:
2.
using System;
public class Test
{
public static void Main()
{
int race,hours,minutes,seconds,fastIndex,slowIndex;
fastIndex=slowIndex = 0;
double[] timeSec= new double[10];
double totalTimeSec = 0;
double fastest = 0;
double slowest = 999999;
for(race = 0;race < 10; race++) // enter hours,minutes and seconds for 10 races
{
Console.WriteLine(\"Enter time in hours,minutes and seconds\");
hours = Convert.ToInt32(Console.Read());
minutes = Convert.ToInt32(Console.Read());
seconds = Convert.ToInt32(Console.Read());
timeSec[race] = hours*60*60+minutes+minutes*60+seconds;
totalTimeSec = totalTimeSec + timeSec[race];
}
for(race = 0;race < 10; race++)
{
if(timeSec[race] < slowest)
{
slowest = timeSec[race]; //find lowest time or fastest race
fastIndex = race;
}
}
//Console.WriteLine(\"fastest :\"+fastIndex);
for(race = 0;race < 10; race++)
{
if(timeSec[race] > fastest) //find higest time or slowest eace
{
fastest = timeSec[race];
slowIndex = race;
}
}
for(race = 0;race < 10; race++)
{
if(race == fastIndex)
Console.WriteLine(\"Race \"+(race+1)+\":\"+ timeSec[race]+\" **FASTEST**\");
else if(race == slowIndex)
Console.WriteLine(\"Race \"+(race+1)+\":\"+ timeSec[race]+\" **SLOWEST**\");
else
Console.WriteLine(\"Race \"+(race+1)+\":\"+ timeSec[race]);
}
Console.WriteLine(\"Average time of 10 race : \"+totalTimeSec/10+\" Seconds\");
}
}
output:



