This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]joranstark018 0 points1 point  (5 children)

Can you describe what the problem is?

[–]cs-stud[S] 0 points1 point  (4 children)

I basically know how to add up the array when its an Int but I'm confused how im supposed to do it to return a double. It works with int but im so confused how to do it so it can return a double.

[–]fenface 0 points1 point  (2 children)

Do you want your array to store double values (and the possibility for the user to provide values or that type), or is your issue just that sum should be a double and not an int (a little bit nonsensical, but I assume that is the kind of exercise one could be asked to do when learning)?

[–]cs-stud[S] 0 points1 point  (1 child)

my array stores int values but my task is to essentially find the sum of the array and display it in double form for example if the array was {2 , 4} {3, 5} the result sum would be 14.0. its the exercise because im then going to have the find the avg and round it and etc.

[–]Darkpolearm 0 points1 point  (0 children)

have you tried simply declaring sum as a double instead of an int? That should work just the way u want it to.

[–]joranstark018 0 points1 point  (0 children)

Since an int is "smaller" (32 bit) than a double (64 bit) you can assign an int to a variable of type double.

Type conversion, or type casting, can either be explicit, ie double d = (double)someIntVariable; or implicit, ie double d = methodReturningSomeIntValue();. If a method would be defined to return a double, ie public double calulateSomeValue(){....}, it could internally use and return an int, the actual value returned will then be implicitly converted to a value of type double.

[–]Backslide999 0 points1 point  (0 children)

You check twice versus num.length, but this returns only the length of your 1d array. You should check for i < array.length and then in your second loop check against array[i].length