Question

In: Computer Science

Project: Given an array numbers. We define a running sum of an array as runningSum[i] =...

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:

1. Public static Int[] getArray() --- inside ArrayOperationclass, using loop generate a n items array.
2. Public static void runnungSum(int[] inputArray) --- inside ArrayOperation class, process the array to calculate the sums. An array is an object, passing an array to a method is passing by reference.
3. Public static void printArray(int[] result) --- inside ArrayOperation class, print the array in a [1, 2, 3, 4, 5] form.
4. Public static void main(string[] args) --- Prompt user to input a whole number between 5 and 10, using the user input number to generate the array, perform calculation, and print out the array before and after calculation,

Solutions

Expert Solution

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.


Related Solutions

Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[ ][ ] = { {1, 2, 3, 4},                        {5, 6, 7, 8},                        {9, 10, 11, 12} }; OUTPUT: Sum of the edges = 65
Write a program in Easy68K: a) Define an array of numbers in the memory.
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read two numbers from keyboard. The first number is the size of the array and the second number is what index of the array you want to access. The index you entered can be larger than the array. c) Display the element indexed by (index % size) in the array. 
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read...
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read two numbers from keyboard. The first number is the size of the array and the second number is what index of the array you want to access. The index you entered can be larger than the array. c) Display the element indexed by (index % size) in the array.
Suppose that all the numbers in an array are located in an interval [0,12], and we...
Suppose that all the numbers in an array are located in an interval [0,12], and we need to find the largest element with accuracy ε = 0.8. How many iterations will you need if we use the quantum optimization algorithm? How many times do we need to apply Grover's algorithm? Trace the quantum optimization algorithm for the case when the actual largest element is a5 = 3.14
We have an array of numbers, and we start at index 0. At every point, we're...
We have an array of numbers, and we start at index 0. At every point, we're allowed to jump from index i to i+3, i+4, or stop where we are. We'd like to find the maximum possible sum of the numbers we visit. For example, for the array [14, 28, 79, -87, 29, 34, -7, 65, -11, 91, 32, 27, -5], the answer is 140. (starting at the 14, we jump 4 spots to 29, then 3 spots to 65,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT