I have difficulties with transcripting the following two functions written in mathematical notation into Java code (the input of both functions is an array with D elements):
Can somebody take a look at the code below and tell me if something is wrong with it?
public double firstFunction(double[] arrayOfElements) {
double sum = 0;
double sumTwo = 0;
for(int i = 0; i < arrayOfElements.length; i++) {
for(int j = 0; j < i; j++){
sumTwo = sumTwo + arrayOfElements[j];
}
sum = sum + Math.pow(sumTwo, 2);
}
return sum;
}
public double secondFunction(double[] arrayOfElements) {
double maximum = Math.abs(arrayOfElements[0]);
for (int i = 0; i < arrayOfElements.length; i++) {
if (Math.abs(arrayOfElements[i]) > maximum) {
maximum = Math.abs(arrayOfElements[i]);
}
}
return maximum;
}