Question

In: Computer Science

Write a recursive program in C++ to compute the determinant of an NxN matrix, A. Your...

Write a recursive program in C++ to compute the determinant of an NxN matrix, A. Your program should ask the user to enter the value of N, followed by the path of a file where the entries of the matrix could be found. It should then read the file, compute the determinant and return its value. Compile and run your program.

Solutions

Expert Solution

#include<iostream>
#include<math.h>
using namespace std;
//find the determinant
int deter( int mat[10][10], int size) {   
int det = 0,k,a,b; //set the determinant to 0
int m1[30][30],m1i,m1j;//declare the mtrix
if (size == 2)//compute the determinant for size 2
return ((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1])); //return the determinant
//compute determinant for the matrix
else {
for (k = 0; k < size; k++) {
m1i = 0; //set the row index of m1 to 0
for (a = 1; a < size; a++) {
m1j = 0; //set the column index of m1 to 0   
for (b = 0; b < size; b++) {
if (b == k)
continue;
m1[m1i][m1j] = mat[a][b]; //assign mat[a][b] to m1
m1j++;
}
m1i++;
}//recursively call to deter() to compute the determinant
det = det + (pow(-1, k) * mat[0][k] * deter( m1, size - 1 ));
}
}
return det; //return the determinant of the matrix
}
//driver program
int main()
{
int size, i, j;
int mat[30][30];   
//enter the size of matrix
cout << "Enter the size of the matrix:\n";
cin >> size;
//input the matrix elements
cout << "Enter the elements of the matrix:\n";
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
cin >> mat[i][j];
//display the given matrix
cout<<"The entered matrix is:"<<endl;
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++)
cout << mat[i][j] <<" ";
cout<<endl;
}
//print the determinant
cout<<"Determinant of the matrix is "<< deter(mat, size);
return 0;
}

OUTPUT


Related Solutions

Write a recursive program in C++ to compute the determinant of an NxN matrix, A. Your...
Write a recursive program in C++ to compute the determinant of an NxN matrix, A. Your program should ask the user to enter the value of N, followed by the path of a file where the entries of the matrix could be found. It should then read the file, compute the determinant and return its value. Compile and run your program.
Find the adjoint of matrix A, the determinant of matrix A, and the determinant of the...
Find the adjoint of matrix A, the determinant of matrix A, and the determinant of the adjoint A. A= 1 1 0 2 2 1 1 0 0 2 1 1 1 0 2 1
1. For each permutationσ of {1,2,··· ,6} write the permutation matrix M(σ) and compute the determinant...
1. For each permutationσ of {1,2,··· ,6} write the permutation matrix M(σ) and compute the determinant |m(σ)|, which equals sgn(σ). (a) The permutation given by 1 → 2, 2 → 4, 3 → 3, 4 → 1, 5 → 6, 6 → 5. (b)  The permutation given by 1 → 5, 2 → 1, 3 → 2, 4 → 6, 5 → 3, 6 → 4.  
Compute the determinant of A, where A= a 4x4 matrix [1 -3 0 0; 2 1...
Compute the determinant of A, where A= a 4x4 matrix [1 -3 0 0; 2 1 0 0; 0 0 1 2; 0 0 2 1] a 4x4 matrix [2 5 4 2; 0 0 0 2; 0 -3 0 -4; 1 0 -1 1] and a 4x4 matrix [1 -3 0 0; 2 1 0 0; 0 0 1 2; 0 0 2 1]^-1. a) det(A)= -36 b) det(A)= 5 c) det(A)= 0 d) det(A)= -13 e)det(A)= 36
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string b. Write the pseudocode for the program.
Program in C++ **********Write a program to compute the number of collisions required in a long...
Program in C++ **********Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing and double hashing. For simplicity, only integers will be hashed and the hash function h(x) = x % D where D is the size of the table (fixed size of 1001). The simulation should continue until the quadratic hashing fails.*********
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
Write a C or C++ program that uses pthreads to compute the number of values that...
Write a C or C++ program that uses pthreads to compute the number of values that are evenly divisible by 97 between a specified range of values (INCLUSIVE). The program should accept 3 command-line arguments: low value high value number of threads to create to perform the computation -Specifics for program order of input: 0 9000000000 2 this means 0 to 9 Billion (INCLUSIVE); 2 threads alarm(90); this should be the first executable line of the program to make sure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT