# Homework question having to do with arrays...

• June 3rd, 2013, 11:12 AM
TheoAnderson
Homework question having to do with arrays...
Hey guys, my name's Theo, or Thedore. I am currently doing the MIT open courseware java class, an am having a bit of trouble understanding how exactly to do this problem dealing with arrays. Any help would be appreciated!

A group of MIT friends decide to run the Boston Marathon. Their names and times (in minutes) are below:
Name Time (minutes)
Elena 341
Thomas 273
Hamilton 278
Suzie 329
Phil 445
Matt 402
Alex 388
Emma 275
John 243
James 334
Jane 412
Emily 393
Daniel 299
Neda 343
Aaron 317
Kate 265

Find the fastest runner. Print the name and his/her time (in minutes).

Optional: Find the second fastest runner. Print the name and his/her time (in minutes).
Write a method that takes as input an array of integers and returns the index corresponding to the person with the lowest
time. Run this method on the array of times. Print out the name and time corresponding to the returned index.
Write a second method to find the second-best runner. The second method should use the first method to determine the
best runner, and then loop through all values to find the second-best (second lowest) time.

Here is a program skeleton to get started:
class Marathon {
public static void main (String[] arguments){
String[] names = {
"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
"Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",
"Aaron", "Kate"
};
int[] times = {
341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,
343, 317, 265
};
for(int i = 0; i < names.length; i++){ System.out.println(names[i] + ": " + times[i]);
}
}
}
Submit your file Marathon.java via Stellar.
Good luck!
• June 3rd, 2013, 11:24 AM
KevinWorkman
Re: Homework question having to do with arrays...
Where exactly are you stuck? You've posted the question and some code, but not an actual technical question.

PS: When posting code, make sure you use the highlight tags to preserve formatting.
• June 3rd, 2013, 11:28 AM
TheoAnderson
Re: Homework question having to do with arrays...
Kevin,

I am pretty much brand new to Java and don't have hardly any experience.

What do you mean highlight tags?

My question is simply one of how do I go about finding out who has the lowest time and then print it out? Should I make another method to be called by the main method which gets this information? I am unsure how to figure out how I would go about finding the fastest time.

Thanks,

-Theo
• June 3rd, 2013, 11:48 AM
KevinWorkman
Re: Homework question having to do with arrays...
The highlight tags help preserve formatting. Check out another post that uses them or the link in my signature on using code tags for more information.

When approaching this kind of problem, take a step back from the computer and think about how you'd do this with just a piece of paper and a pencil. Say you have a really long list of numbers. How would you figure out which one is the largest? Pretend you have a friend who is really dumb, and write out instructions (not code) that he could follow to find the largest number in a big list. Remember how dumb he is, so make sure you break the instructions down into the smallest steps you can.

When you have those instructions written out, you'll have an algorithm that you can start thinking about coding.
• June 3rd, 2013, 12:53 PM
beansnbacon
Re: Homework question having to do with arrays...
Use Math.max(int x, int y) in a loop to figure out which is the ultimate highest of them all. By the way, if you don't have hardly any experience, this should be easy for you.

UPDATE: Use Math.min(int x, int y) since the lowest number is the fastest.
• June 3rd, 2013, 12:55 PM
KevinWorkman
Re: Homework question having to do with arrays...
Please note that beansnbacon's solution is only one approach of many that you can take. I strongly suggest you try coming up with whatever solution makes the most sense to you before implementing somebody else's algorithm.

Coding a given algorithm is easy. Coming up with an algorithm in the first place is the hard part, and it's a skill that's almost impossible to teach other than to have you think through problems on your own.