I want to divide the elements of my array for the first element of the array, the method is working ok, but the last element is not been divided.
I think I had to use <= array.length…, but it obviously gives me an array out of bound exception. How can I achieve this without <=length. PS: the first element should not be dived/taken into consideration as it is the dividend. My code is the following:
public class Ris
{
public static void main()
{
double[] testResult = {2.0, 3.6, 5.0, 2.0};
for(int element = 0; element < testResult.length; element++){
if( testResult[element] > testResult[0]){//excludes 1st element
testResult[element] = testResult[element] / testResult[0] ;// divides elements by first element 0
}
System.out.println(testResult[element]);
}
}
}
You’re skiping the first elemnt in a bad manner –
Skips everything that is not more than first element – more then 2.0
You probably ment to test for
But you can also just skip first element in the definition of for cycle