In: Computer Science
Project:
Given an array numbers. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
Return the running sum of numbers.
Example:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
You need to do: Create a class called RunningSumArray to perform the main method. Create a class called ArrayOperationto hold the actions for array. In this project, you will need 4methods:
Code
ArrayOperation class
import java.util.Scanner;
public class ArrayOperation {
public static int[] getArray()
{
int n;
int inputArray[];
Scanner scnr=new Scanner(System.in);
System.out.print("Enter size of array between 5 to 10: ");
n=scnr.nextInt();
inputArray=new int[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter "+(i+1)+" element: ");
inputArray[i]=scnr.nextInt();
}
return inputArray;
}
public static void runningSum(int[] inputArray)
{
int runningSumArray[]=new int[inputArray.length];
for(int i=0;i<inputArray.length;i++)
{
int sum=0;
for(int j=0;j<=i;j++)
sum+=inputArray[j];
runningSumArray[i]=sum;
}
System.out.print("After Running sum Array is: ");
ArrayOperation.printArray(runningSumArray);
}
public static void printArray(int[] result)
{
int i;
System.out.print("[");
for(i=0;i<result.length-1;i++)
System.out.print(result[i]+", ");
System.out.println(result[i]+"]");
}
}
RuunningSumArray class
public class RunningSumArray {
public static void main(String[] args) {
int inputArray[]=ArrayOperation.getArray();
System.out.print("Origingal array is: ");
ArrayOperation.printArray(inputArray);
ArrayOperation.runningSum(inputArray);
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.