Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling array method in main class

How can i call runningSum method in main with new array of intigers so it return result?

class Main {
   
    public static void main(String[] args) {
        
        
        
    }

    public int[] runningSum(int[] nums) {
        int[] result = new int[nums.length];
        result[0] = nums[0];

        for (int i = 1; i < nums.length; i++) {
            result[i] = result[i - 1] + nums[i];
        }

        return result;
    }
}

I have no idea how to call it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add static modifier to your runningSum method. example

public static void main(String[] args) {



int[] myNum = {10, 20, 30, 40};

    int[] result = runningSum(myNum);

    for (int item : result){
        System.out.println(item);
    }


}

public static int[] runningSum(int[] nums) {
    int[] result = new int[nums.length];
    result[0] = nums[0];

    for (int i = 1; i < nums.length; i++) {
        result[i] = result[i - 1] + nums[i];
    }

    return result;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading