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;
    }
}
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. Commented Feb 26, 2021 at 11:31
  • Does this answer your question? What's the simplest way to print a Java array? Commented Feb 26, 2021 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". Commented Feb 26, 2021 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. Commented Feb 26, 2021 at 12:17

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

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?
I've edited the answer to include the full code that made it work on my machine. Please have a look.
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)));
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."
Well... you do need the proper import for that: import java.util.Arrays;.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.