Question

In: Computer Science

Implement a method/function in java that produces running sum runningSum2DArray(int[][] array, int dir) across rows (left...

Implement a method/function in java that produces running sum

runningSum2DArray(int[][] array, int dir) across rows (left to right or right to left) or columns (top to bottom or bottom to top)

Input to the method: A 4x4 two dimensional int array and an integer (1, 2, 3 or 4 for left, right, up,down respectively).

Output: The modified array after producing the running sums according to the direction.

For example: If the input to the method is the same as the earlier array, and if the direction is 2 (right), the output array would be printed as:

10 25 55 95

15 20 28 30

20 22 26 28

1 5 10 10

Now, implement another function runningSum2DArrayList(ArrayList< ArrayList< Integer > > list, int dir) that performs the same functionality.

Solutions

Expert Solution

For the above things, we need to have the following things into consideration.

  1. We need to go according to the direction.
    1. One(1) indicates, going to left from right
    2. Two(2) indicates, going to the right from left
    3. Three(3) indicates, going to top from the bottom
    4. Four(4) indicates, going to bottom from top

Following is the code for the same.

import java.util.ArrayList;

public class FindingTheSums {
   public static void Sum2DArray(int arr[][], int dir) {
       if(dir == 1)
       {
           // Left
           for(int i=0;i<arr.length;i++) {
               for(int j = arr[0].length-2;j>=0;j--) {
                   arr[i][j] = arr[i][j+1] + arr[i][j];
               }
           }
       }
       else if(dir == 2)
       {
           // right
           for(int i = 0;i<arr.length;i++) {
               for(int j = 1;j<arr[0].length;j++) {
                   arr[i][j] = arr[i][j-1] + arr[i][j];
               }
           }
       }
       else if(dir == 3)
       {
           // top
           for(int j = 0;j<arr[0].length;j++) {
               for(int i = arr.length-2;i>=0;i--) {
                   arr[i][j] = arr[i][j] + arr[i+1][j];
               }
           }
       }
       else
       {
           // down
           for(int j = 0;j<arr[0].length;j++) {
               for(int i = 1;i<arr.length;i++) {
                   arr[i][j] = arr[i][j] + arr[i-1][j];
               }
           }
       }
   }
   public static void print(int arr[][]) {
       for(int i = 0 ;i<arr.length;i++) {
           for(int j = 0;j<arr[0].length;j++) {
               System.out.print(arr[i][j] + " ");
           }
           System.out.println();
       }
       System.out.println();
   }
   public static void main(String args[]) {
       int arr[][] = {{10, 15, 30, 40},{15, 5, 8, 2},{20, 2, 4, 2},{1, 4, 5, 0}};
       Sum2DArray(arr, 2);
       print(arr);
   }

}

Following is the output snippet of the above program.

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like if you think effort deserves like.
Thanks


Related Solutions

in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array....
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array. When the array contains {{10, 15, 30, 40},{15, 5, 8, 2}, {20, 2, 4, 2},{1, 4, 5, 0}}, Your output should look like: {10 15 30 40} {15 5 8 2}{ 20 2 4 2}{ 1450} Now, implement another function print2DList(ArrayList<ArrayList<Integer>> list) to print a formatted 2D list.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array...
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array A 2) an int number x to search for (b) determines whether x exists in A, and prints a message indicating the result (c) has the best worst case Big Oh complexity you can manage, using only your own thinking and the materials (the worst case growth rate for the number of items searched should be as low as possible, given that A contains...
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum...
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum of positive integers in an ADT List aList of integers using ADT List operations. ADT List operations: isEmpty(), size(), add(index, item), remove(index), get(index), and removeAll(). You should not assume how an ADT List is implemented. Using array indexing, head/tail references, or any operation not defined in ADT List is not allowed.
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
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 method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
JAVA programing language: What is printed when the following code is executed? int columns; int rows;...
JAVA programing language: What is printed when the following code is executed? int columns; int rows; for(rows = 1; rows < 2; ++rows) { for(columns = 1; columns < 3; ++columns) { System.out.print("x"); } System.out.println(): } select one: A) xx B) xxx xxx C) x x D) xx xx xx
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT