Wednesday, 15 January 2014

java - Cumulative sum of an Array -



java - Cumulative sum of an Array -

so i'm working on problem focuses on taking cumulative sum of array illustration if have array of ({0,2,3,-1,-1}) returns {0,2,5,4,3}... or if have array of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] should homecoming [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]...

right i'm struggling 2 problems 1 have utilize method given , i'm struggling homecoming because total not.. code know works adding sum of array not cumulative sum in examples.. guidelines helpful.

public int[] makecumul(int[] in) { int[] out = { in.length }; int total = 0; (int = 0; < out.length; i++) { total += out[i]; } homecoming total; }

not reading in array, partly, not updating out array, , not returning it. should work you.

public int[] makecumul(int[] in) { int[] out = new int[in.length]; int total = 0; (int = 0; < in.length; i++) { total += in[i]; out[i] = total; } homecoming out; }

java arrays loops for-loop

No comments:

Post a Comment