Cumulative sum of an Array

27,914

Solution 1

Not reading the in array, partly, but also not updating the out array, and not returning it. This should work for you.

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

Solution 2

public class Sum {
    public static void main(String[] args) {
        int in[] = {1,2,3,4,5,6,7,8,9};
        int[] out = new int[in.length];
        out[0] = in[0];
        for (int i = 1; i < out.length; i++) 
            out[i] = out[i-1] + in[i];

        for (int i = 0; i < out.length; i++) 
            System.out.print(out[i]+" ");
    }
}

OUTPUT

1 3 6 10 15 21 28 36 45 

If you want to put this in a method, you can return the last element like this:

return out[out.length-1];

Solution 3

public static int[] makeCumul(int[] in) {
    int[] out = new int[in.length];
    int sum = 0;
    for(int i = 0; i < in.length; i++){
        sum += in[i];
        out[i] = sum;
    }
    return out;
}

I believe this is what you are looking for. Keep a cumulative sum, and update that sum with each element. After you update the sum, replace the element with the sum.

When you create a new array, to initialize it use

int[] out = new int[ARRAY SIZE HERE];

You should also note that in the method signature you are returning an array of integers, and the variable total is an integer, not an array of integers. So you want to return the variable out.

Gives me the output:

0
2
5
4
3
Share:
27,914
user1071777
Author by

user1071777

Updated on June 21, 2020

Comments

  • user1071777
    user1071777 almost 4 years

    So i'm working on a problem that focuses on taking the cumulative sum of an array so for example if i have an array of ({0,2,3,-1,-1}) it returns {0,2,5,4,3}... or if you have an array of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] it should return [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]...

    Right now i'm struggling with two problems one i have to use the method given and i'm struggling with what to return because total will not.. To my code i know it works for adding up the sum of an array but not the cumulative sum as in my examples.. any guidelines would be helpful.

    public int[] makeCumul(int[] in) {
        int[] out = { in.length };
        int total = 0;
        for (int i = 0; i < out.length; i++) {
            total += out[i];
        }
        return total;
    }
    
    • Louis Wasserman
      Louis Wasserman almost 10 years
      You're never reading any of the elements of in. That seems problematic.
    • Sean Bright
      Sean Bright almost 10 years
      This won't even compile. You say you're going to return an int[] but you're returning an int.
    • Admin
      Admin almost 10 years
      sorry i do i call it in my main method with a set up array..
    • azurefrog
      azurefrog almost 10 years
      Your code has several basic errors in it. 1) You are creating out as a size 1 array. 2) Your method sig says you return an int[] but you're actually returning an int. 3) You never actually read any of the values in the input array.