+ 1

What is an array of doubles?

double sum = 0.0; for (int x = 0; x < 4; x++) { sum += myArray[x]; } System.out.println(sum); They said this was an array of doubles; why is x++ the correct answer? I put x*x

30th May 2018, 8:44 AM
Li Ying Choo
Li Ying Choo - avatar
3 ответов
+ 4
"Array of doubles" just means that the array is declared as type double. double[4] myArray;
30th May 2018, 10:30 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Like how int refers to integer values, double refers to floating-point values of double precision. E.g. 4.98, 7.56, 6.669, 12.62816... Without more context, there is no telling to whether or not it is necessary. However, one can tell that this code snippet is supposed to print the sum of all elements in a double array. A for loop is used to iterate through the array and add all elements to sum. The variable sum is then printed.
30th May 2018, 2:38 PM
Hatsy Rei
Hatsy Rei - avatar
0
But how would it affect the code, and why is such a function necessary?
30th May 2018, 2:14 PM
Li Ying Choo
Li Ying Choo - avatar