Question

In: Computer Science

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

  1. 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 <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

using namespace std;

int ReadNumbers( const string & s, vector <double> & v );
void import_matrix_from_txt_file(const char* filename_X, vector <double>& v, int& rows, int& cols);

int main(){
vector <double> v;
int rows=0;
int cols=0;

import_matrix_from_txt_file("x.txt",v,rows,cols);
}

int ReadNumbers( const string & s, vector <double> & v ) {
istringstream is( s );
double n;
while( is >> n ) {
v.push_back( n );
}
return v.size();
}

void import_matrix_from_txt_file(const char* filename_X, vector <double>& v, int& rows, int& cols){
  
ifstream file_X;
string line;
  
file_X.open(filename_X);
if (file_X.is_open())
{
int i=0;
getline(file_X, line);
  
  
cols =ReadNumbers( line, v );
cout << "cols:" << cols << endl;
  
  
for ( i=1;i<32767;i++){
if ( getline(file_X, line) == 0 ) break;
ReadNumbers( line, v );
  
}
  
rows=i;
cout << "rows :" << rows << endl;
if(rows >32766) cout<< "N must be smaller than MAX_INT";
  
file_X.close();
}
else{
cout << "file open failed";
}
  
cout << "v:" << endl;
for (int i=0;i<rows;i++){
for (int j=0;j<cols;j++){
cout << v[i*cols+j] << "\t" ;
}
cout << endl;
}
}

// Finding Determinant

int determinantOfMatrix(int mat[N][N], int n)

{

    int D = 0; // Initialize result

  

    // Base case : if matrix contains single element

    if (n == 1)

        return mat[0][0];

  

    int temp[N][N]; // To store cofactors

  

    int sign = 1; // To store sign multiplier

  

     // 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;

}


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