Question

Programming Language: Java Please write a program that uses a recursive algorithm to compute the determinant...

Programming Language: Java Please write a program that uses a recursive algorithm to compute the determinant of a matrix. It should read the order of the matrix, read the matrix, print it out, compute, and print the determinant. Your program should be able to evaluate multiple matrices on a single execution. For class purposes, your program should handle matrices up to and including those of order 6. You are required to use an array for this problem. Your solution must be recursive.

Justify your data structures. Consider an iterative implementation. Would it be more efficient? What data structures would you choose in that case?

As a minimum, use the following eight matrices to test your program, formatted as

shown below, with the order of the matrix preceding it.

1 x 1: [5]

2 x 2: 2 3

5 9

3 x 3: 3 -2 4

-1 5 2

-3 6 4

4 x 4: 2 4 5 6

0 3 6 9

0 0 9 8

0 0 0 5

4 x 4: 2 4 5 6

0 0 0 0

0 0 9 8

0 0 0 5

6 x 6:

6 4 6 4 6 4

1 2 3 4 5 6

6 5 4 3 2 1

3 2 3 2 3 2

4 6 4 6 4 6

1 1 1 1 1 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find the below java code which determines the Determinant of a given matrix.

----------------------------------------------------------------------------------------

MatrixDeterminantDemo.java

----------------------------------------------------------------------------------------

import java.util.Scanner;

public class MatrixDeterminantDemo {

   // Function to get cofactor of
   // mat[p][q] in temp[][]. n is
   // current dimension of mat[][]
   static void getCofactor(int mat[][], int temp[][], int p, int q, int n) {
       int i = 0, j = 0;

       // Looping for each element of
       // the matrix
       for (int row = 0; row < n; row++) {
           for (int col = 0; col < n; col++) {

               // Copying into temporary matrix
               // only those element which are
               // not in given row and column
               if (row != p && col != q) {
                   temp[i][j++] = mat[row][col];

                   // Row is filled, so increase
                   // row index and reset col
                   // index
                   if (j == n - 1) {
                       j = 0;
                       i++;
                   }
               }
           }
       }
   }

   /*
   * Recursive function for finding determinant of matrix. n is current dimension
   * of mat[][].
   */
   static int determinantOfMatrix(int mat[][], int n) {
       int D = 0; // Initialize result

       // Base case : if matrix contains single
       // element
       if (n == 1)
           return mat[0][0];

       // To store cofactors
       int temp[][] = new int[n][n];

       // To store sign multiplier
       int sign = 1;

       // Iterate for each element of first row
       for (int f = 0; f < n; f++) {
           // Getting Cofactor of mat[0][f]
           getCofactor(mat, temp, 0, f, n);
           D += sign * mat[0][f] * determinantOfMatrix(temp, n - 1);

           // terms are to be added with
           // alternate sign
           sign = -sign;
       }

       return D;
   }

   /* function for displaying the matrix */
   static void display(int mat[][], int row, int col) {
       for (int i = 0; i < row; i++) {
           for (int j = 0; j < col; j++)
               System.out.print(mat[i][j]);

           System.out.print("\n");
       }
   }

   // Driver code
   public static void main(String[] args) {
       // initialize here.
       int row, col, i, j;
       Scanner scan = new Scanner(System.in);
       // enter the order of matrix
       System.out.print("Enter the order of matrix : ");
       row = scan.nextInt();
       col = row;
       int arr[][] = new int[row][col];
       // enter array elements.
       System.out.println("Enter " + (row * col) + " Array Elements : ");
       for (i = 0; i < row; i++) {
           for (j = 0; j < col; j++) {
               arr[i][j] = scan.nextInt();
           }
       }

       // the 2D array is here.
       System.out.print("The Array is :\n");
       for (i = 0; i < row; i++) {
           for (j = 0; j < col; j++) {
               System.out.print(arr[i][j] + " ");
           }
           System.out.println();
       }

       int mat[][] = { { 1, 0, 2, -1 }, { 3, 0, 0, 5 }, { 2, 1, 4, -3 }, { 1, 0, 5, 0 } };

       System.out.print("Determinant " + "of the matrix is : " + determinantOfMatrix(mat, row));
   }
}

Sample output 1:

Thanks!!

Add a comment
Know the answer?
Add Answer to:
Programming Language: Java Please write a program that uses a recursive algorithm to compute the determinant...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should...

    Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should meet the following requirements: 1. The program shall graphically prompt the user for a file. 2. The program shall read the selected file which will contain 1 integer per line. 3. The program shall sort the values it reads from the file from largest to smallest. 4. The program shall write the values to an output file from largest to smallest in the same...

  • If you understand how the above recursive algorithm to compute yz works, you can turn it...

    If you understand how the above recursive algorithm to compute yz works, you can turn it into a more efficient iterative algorithm that basically uses the same strategy (though it is not a tail recursive algorithm). Some parts of this iterative algorithm is given below. Fill in the blanks: Power-iterative(y: number; z: non-negative integer) 1. answer=1 2. while z > 0 3.    if z is odd then answer=__________ 4.    z = ________ 5.    y = _________ 6....

  • Write a Java program that will - read two 2x3 matrices A and B. - compute...

    Write a Java program that will - read two 2x3 matrices A and B. - compute their sum (C=A+B) - print out the C matrix Please write out code. No screen shots please.

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • INTEL 80x86 ASSEMBLY LANGUAGE CODE Write a windows32 assembly language program that utilizes a recursive procedure....

    INTEL 80x86 ASSEMBLY LANGUAGE CODE Write a windows32 assembly language program that utilizes a recursive procedure. The main (_MainProc) procedure should: accept, from the user, a positive integer. Guard against non-positive integers being entered using a loop. call the sumseries sub-procedure using the cdecl protocol, receive the results of the sub-procedure, and display the results. The sumseries sub-procedure should: recursively find the sum of the series: 1*2 + 2*3 + 3*4 + ... + i*(i+1) (This is an iterative definition....

  • (1 point) Compute the determinant of the matrix -1 -2 -4 -6 -7 -7 7 7...

    (1 point) Compute the determinant of the matrix -1 -2 -4 -6 -7 -7 7 7 A= 0 0 0 0 -4 -5 7 det(A) (1 point) Find the determinant of the matrix 6 A- 6 -9 -7 det(A) (1 point) Find the determinant of the matrix 2 2 -2 B= 1 -1 2 3 -2 det (B)

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • The answer need to write in C programming. QUESTION 2 Write a program using array to...

    The answer need to write in C programming. QUESTION 2 Write a program using array to generate a multiplication table based on the user's input. For example if user enter 5 as input then a multiply table for 1 to 5 is printed as output as shown in Figure Q2. There are three main steps in this program which are: Print rows (a) (b) Print columns Print multiplication of data inside table (c) Select C:\example1 \bin\Debuglexample 1.exe enter the value...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT