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

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 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
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.
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 all elements of (Z10xZ15) / <(2,3)> and their respective orders.
Describe all elements of (Z10xZ15) / <(2,3)> and their respective orders.
This function uses a curious mix of iteration and recursion: function F(n) if n < 1...
This function uses a curious mix of iteration and recursion: function F(n) if n < 1 return 1 t <- 0 for i <- 0 to n for j <- i to n t <- t + j return t + F(n-1) What is the number of basic operations (additions and subtractions) performed? Answer: Θ(n³) Could you tell me how I can solve this problem??
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function...
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function that takes in a positive integer n and returns the nth Jacobsthal number. >>> J(8) 85 >>> J(9) 171 #3 Use recursion to write a function that takes in a positive integer n and returns all n digit numbers containing only odd digits. >>> f(1) [1, 3, 5, 7, 9] >>> f(2) [11, 13, 15, 17, 19, 31, 33, 35, 37, 39, 51, 53,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT