Answered You can hire a professional tutor to get the answer.

QUESTION

Instructions: I'm looking to make it more efficient with less lines of codes if possible. I'm new at C# and want to see how I might be able to make...

Instructions:

Using C# Please help improve my code. I'm looking to make it more efficient with less lines of codes if possible. I'm new at C# and want to see how I might be able to make this better.

Assignment Instructions:

Program 2

Your instructor runs a lot of marathons. Create 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.

My 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)

{

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();

}

}

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question