So I know the code below works but as I am very new to coding I don't understand why and how it works. If anyone could explain it to me it would help me forward in understanding programming.
The code is as follows:
public static double minValue(double[] times){
double minValue = times[0];
for(int i = 1; i < times.length; i++){
if (times[i] < minValue){
minValue = times[i];
}
}
minValue = minValue / 60;
return minValue;
}
What I don't understand here is: double minValue = times[0]; Why is this value 0? It works fine with other numbers too, below the length of the array. Using blank or a number greater than array length returns an error but there must be a reason why the code uses specifically 0.
And then this:
for(int i = 1; i < times.length; i++){
if (times[i] < minValue) {
minValue = times[i];
}
}
How exactly that determines the minimum value?
You have to set some initial value to minValue.
One convention is to initialize it to a value larger than all the numbers in the array (such as Double.MAX_VALUE).
Then the loop would have to iterate over all the elements of the array :
for(int i = 0; i < times.length; i++)
Another convention is to initialize it to one of the values of the array (any one will work). The first element of the array is often chosen, which allows the loop to iterate from the second element of the array :
for(int i = 1; i < times.length; i++)
There are a couple of things here:
Why start from times[0]/ Why is this value 0 ???
The main reason is that arrays in java are zero based. This is a term used to indicate that in order to access the first element in the array, you need to access the zero-th array element.
At the same time, this is why you are also unable to access the nth item in the array (so if the times has four elements, then the fourth element can only be accessed by times[3] and not times[4])
There is a very interesting discussion by E.W Dijkstra on why this 'should' be the case
Programming Example in the Question
So the programming example is basically, given an array/list of decimal numbers, find the minimum value within this list.
If you took the programming language out of the equation here and approached the problem; one of the ways to solve this might be the following:
Tying this Information Back to the Code
So after understanding how we're going to solve this, let's try it with some Java code:
Start with a default value as our minimum
double minValue = times[0];
Go through the rest of our list
for(int i = 1; i < times.length; i++){
Ask ourselves whether the next number in the list is less than the minimum
if (times[i] < minValue){
If it is, then this is our new minimum
minValue = times[i];
}
}
After doing this process with the entire list/array of numbers, you are left with the actual minimum
return minValue;
Once you have understood that, then you can essentially understand why your code works the way it works (I used your own code too :))
For me the most important takeaway here should be that you need to have an idea of how to solve the problem before attempting to use a programming language. A programming language is just used as a way to express the solution you have.
Happy Coding :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With