0

I can't get this program to work. It's supposed to take an array as input and output a new array with the accumulative sum of the input array. I used a function for this (bottom part).

For example:

Input: 1 2 3 4
Output: 1 3 6 10

Here's my program:

import java.util.Scanner;

public class Accumulate {
    public static void main(String[] args) {
        int n, sum = 0;
        Scanner s = new Scanner(System.in);

        System.out.print("Enter the size of the array:");
        n = s.nextInt();
        int a[] = new int[n];

        System.out.println("Enter all the elements:");
        for (int i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }
        System.out.println(Arrays.toString(accSum(a)));
        s.close();
    }

    public static int[] accSum(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;
    }
}
Community
  • 1
  • 1
Martin
  • 49
  • 4
  • Please explain in what way the program does not work. Do you get a compile erorr. If not, do you get an Exception. Try putting a breakpoint at the entry of the accSum function to check that the array contains the data that was input. – jr593 Feb 26 '21 at 11:31
  • Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Janez Kuhar Feb 26 '21 at 11:36
  • Example: (Size of array) Input: 5; (Enter all the elements) Input: 1 2 3 4 5. I get a ArrayIndexOutOfBounds exception with "Index 5 out of bounds for length 5". – Martin Feb 26 '21 at 11:39
  • Check how your curly brackets match. In particular, look at the curly bracket just before the definition of accSum. Make sure your indentation is correct, otherwise you'll get confused. Also, it's conventional (but not required) to use 4 spaces for indentation. – k314159 Feb 26 '21 at 12:17

2 Answers2

0

Use Arrays.toString() to print an array.

You can't call System.out.println(accSum(a)); directly as that will print the memory address of the array and not the contents. Wrap the call with Arrays.toString(accSum(a)) and it will print the expected output.

PS: The code in your post doesn't compile:

  • scanner.close(); should be s.close();
  • accSum() should be static.

Addendum: So your full code became this:

public static void main(String[] args) {
    int n, sum = 0;

    Scanner s = new Scanner(System.in);

    System.out.print("Enter the size of the array:");
    n = s.nextInt();
    int a[] = new int[n];

    System.out.println("Enter all the elements:");
    for (int i = 0; i < n; i++) {
        a[i] = s.nextInt();
    }
    System.out.println(Arrays.toString(accSum(a)));

    s.close();
}
public static int[] accSum(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;
}
Community
  • 1
  • 1
  • I edited my code in the question, when i compile it get an error in the first line of my function at int[], any idea? – Martin Feb 26 '21 at 11:55
  • I've edited the answer to include the full code that made it work on my machine. Please have a look. – Wouter van der Linde Feb 26 '21 at 12:01
  • Oh wait, I see you edited the code in the question. :D (Just like you said) You did `Arrays.toString(Sytem.out.print(accSum(a)));` but it should be `System.out.println(Arrays.toString(accSum(a)));` – Wouter van der Linde Feb 26 '21 at 12:03
  • Hey, i changed the code in the question, now when i compile i get an error at `System.out.println(Arrays.toString(accSum(a)));` it says `"error: cannot find symbol"` with a marker at the "A" of "`Arrays.`" – Martin Feb 26 '21 at 12:30
  • Well... you do need the proper import for that: `import java.util.Arrays;`. – Wouter van der Linde Feb 26 '21 at 12:40
0

You can use Arrays.stream(int[],int,int) method to get the sum of the previous array elements:

public static void main(String[] args) {
    int[] arr1 = {1, 2, 3, 4};
    int[] arr2 = accSum(arr1);
    System.out.println(Arrays.toString(arr2));
    // [1, 3, 6, 10]
}
public static int[] accSum(int[] arr) {
    return IntStream
            // iterate over the indices of the array
            .range(0, arr.length)
            // sum of the current element and all previous elements
            .map(i -> arr[i] + Arrays.stream(arr, 0, i).sum())
            // return an array
            .toArray();
}

See also: Modifying a 2D array