Question

In: Computer Science

Describe a way to use recursion to add all the elements in a n × n...

Describe a way to use recursion to add all the elements in a n × n (two dimensional) array of integers

(java)

Solutions

Expert Solution

Code:

public class Main
{
   public int getSum(int[][] mat,int row,int column,int rowMax,int columnMax)
   {
       if(row < 0 && column == 0)
           return 0;
      
       if(row < 0)
       {
           row = rowMax - 1;
           column = column - 1;
       }
          
      
       return mat[row][column] + getSum(mat,row - 1,column,rowMax,columnMax);
   }
  
   public static void main(String[] args)
   {
       int[][] mat = new int[][]{
           {1,2},
           {3,4}
       };
      
       Main sum = new Main();
      
       int row = 2,column = 2,rowMax = 2,columnMax = 2;
       int totalSum = sum.getSum(mat,row - 1,column - 1,rowMax,columnMax);
       System.out.println("Sum : " + totalSum);
      
   }
  
}

OUTPUT:


Related Solutions

Recursion java: 1. Write a recursive algorithm to add all the elements of an array of...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C .4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
Use recursion tree to solve the recurrence: T(n) = T(n/15) + T(n/10) + 2T(n/6) + n^(1/2)
Use recursion tree to solve the recurrence: T(n) = T(n/15) + T(n/10) + 2T(n/6) + n^(1/2)
Use the recursion tree method to determine the asymptoticupper bound of T(n).T(n) satisfies the recurrence T(n)=2T(n-1)+...
Use the recursion tree method to determine the asymptoticupper bound of T(n).T(n) satisfies the recurrence T(n)=2T(n-1)+ c, where c is a positive constant, andT(0)=0.
Use a recursion tree to determine a good asymptotic upper bound on the recurrence T(n) =...
Use a recursion tree to determine a good asymptotic upper bound on the recurrence T(n) = 2T(n/3) + 2n. Use the substitution method to verify your answer
. Discuss when N-way ANOVA is appropriate to use
. Discuss when N-way ANOVA is appropriate to use
My add method is not working to add elements into an arrayList in java. This is...
My add method is not working to add elements into an arrayList in java. This is the error message I keep getting: Exception in thread "main" java.lang.NullPointerException    at assignment1.ArrayBag.add(ArrayBag.java:50)    at assignment1.Main.main(Main.java:21) BUILD FAILED (total time: 0 seconds) The following are all the methods I've created. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package...
Describe all elements of (Z10xZ15) / <(2,3)> and their respective orders.
Describe all elements of (Z10xZ15) / <(2,3)> and their respective orders.
Using the method of recursion, compute y[n] for n = 0 to 20, when x[n]=u[n] and...
Using the method of recursion, compute y[n] for n = 0 to 20, when x[n]=u[n] and y[-1]=2: ?[? + 1] − 0.8?[?] = ?[?] Find a closed-form expression for your result.
Describe the similarities between using recursion versus iteration. Describe the difference between using recursion versus iteration.
Describe the similarities between using recursion versus iteration. Describe the difference between using recursion versus iteration.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT