156k views
2 votes
Write a method with the signature "float sumArray(float[] data)" that takes an array of floats, and returns their sum.

1 Answer

5 votes

Answer:

I will code in JAVA.

Preconditions:

  • The float array of data are declared and initialized.

public float sumArray(float[] data) {

float sum = 0;

for(int i = 0; i < data.length; i++) {

sum = sum + data[i];

}

return sum;

}

Step-by-step explanation:

First, you have to declare a variable sum of type float and initialize with 0. In addition, this method has a for-loop to go through whole array of data and each element is added to the float value to sum. When the for loop ends, the sum variable is returned.

User Steampunkery
by
4.4k points