Question

In: Computer Science

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 the inverse, and quit. I do not care if you use two separate matrices one for the original and one for the inverse or if you combine the two. Note: the matrix should be a float, and you will need to use the cmath round function to get the output below.

Sample Run:

./a.out
input row size 3
input the matrix to invert
-1 2 -3
2 1 0
4 -2 5
the inverse is:
-5  4  -3  
10  -7  6  
8  -6  5

Solutions

Expert Solution

#include <iostream> 
#include <vector> 
using namespace std; 
  
// Function to Print matrix. 
void PrintMatrix(float ar[][10], int n, int m) 
{ 
    for (int i = 0; i < n; i++) { 
        for (int j = 0; j < m; j++) { 
            cout << ar[i][j] << "  "; 
        } 
        printf("\n"); 
    } 
    return; 
} 
  
// Function to Print inverse matrix 
void PrintInverse(float ar[][10], int n, int m) 
{ 
    for (int i = 0; i < n; i++) { 
        for (int j = n; j < m; j++) { 
            printf("%.3f  ", ar[i][j]); 
        } 
        printf("\n"); 
    } 
    return; 
} 
  
// Function to perform the inverse operation on the matrix. 
void InverseOfMatrix(float matrix[][10], int order) 
{ 
    // Matrix Declaration. 
  
    float temp; 
  
    // PrintMatrix function to print the element 
    // of the matrix. 
    printf("=== Matrix ===\n"); 
    PrintMatrix(matrix, order, order); 
  
    // Create the augmented matrix 
    // Add the identity matrix 
    // of order at the end of original matrix. 
    for (int i = 0; i < order; i++) { 
  
        for (int j = 0; j < 2 * order; j++) { 
  
            // Add '1' at the diagonal places of 
            // the matrix to create a identity matirx 
            if (j == (i + order)) 
                matrix[i][j] = 1; 
        } 
    } 
  
    // Interchange the row of matrix, 
    // interchanging of row will start from the last row 
    for (int i = order - 1; i > 0; i--) { 
  
        // Swapping each and every element of the two rows 
        // if (matrix[i - 1][0] < matrix[i][0]) 
        // for (int j = 0; j < 2 * order; j++) { 
        // 
        //        // Swapping of the row, if above 
        //        // condition satisfied. 
        // temp = matrix[i][j]; 
        // matrix[i][j] = matrix[i - 1][j]; 
        // matrix[i - 1][j] = temp; 
        //    } 
  
        // Directly swapping the rows using pointers saves time 
  
        if (matrix[i - 1][0] < matrix[i][0]) { 
            // float* temp = matrix[i]; 
            // matrix[i] = matrix[i - 1]; 
            // matrix[i - 1] = temp; 
            swap(matrix[i], matrix[i-1]);
        } 
    } 
  
    // Print matrix after interchange operations. 
    printf("\n=== Augmented Matrix ===\n"); 
    PrintMatrix(matrix, order, order * 2); 
  
    // Replace a row by sum of itself and a 
    // constant multiple of another row of the matrix 
    for (int i = 0; i < order; i++) { 
  
        for (int j = 0; j < order; j++) { 
  
            if (j != i) { 
  
                temp = matrix[j][i] / matrix[i][i]; 
                for (int k = 0; k < 2 * order; k++) { 
  
                    matrix[j][k] -= matrix[i][k] * temp; 
                } 
            } 
        } 
    } 
  
    // Multiply each row by a nonzero integer. 
    // Divide row element by the diagonal element 
    for (int i = 0; i < order; i++) { 
  
        temp = matrix[i][i]; 
        for (int j = 0; j < 2 * order; j++) { 
  
            matrix[i][j] = matrix[i][j] / temp; 
        } 
    } 
  
    // print the resultant Inverse matrix. 
    printf("\n=== Inverse Matrix ===\n"); 
    PrintInverse(matrix, order, 2 * order); 
  
    return; 
} 
  
// Driver code 
int main() 
{ 
    int order; 
  
    // Order of the matrix 
    // The matrix must be a square a matrix 
    cout << "Enter the size of the matrix\n";
        cin >> order;    
    float matrix[10][10];
    for(int i = 0;i<order;i++){
        for(int j = 0;j<order;j++){
                cin >> matrix[i][j];
        }
    }
    matrix[order][0] = 0;
    // Get the inverse of matrix 
    InverseOfMatrix(matrix, order); 
  
    return 0; 
} 

Related Solutions

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 week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
(Write a program in C++) A local instructor wants you to write a program to calculate...
(Write a program in C++) A local instructor wants you to write a program to calculate the average score made on exams by her students. For simplicity, she always has only 12 students in each course she teaches. She teaches multiple subjects so she would like to enter the name of the exam. She wants the program to also determine the highest and lowest scores and the number of students who passed and failed the exam. A score of 60...
C++ Funcion For this lab you need to write a program that will read in two...
C++ Funcion For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using Euclidean algorithm) to a file. You will also need to demonstrate using ostream and ostringstream by creating 2 functions to output your print heading: one that uses ostream and the other uses ostringstream. Using ostream and ostringstream Write two PrintHeader functions that will allow you to output to the screen and to an...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
write a C++ Program for matrix operations which include: 1. constructor in the form-- matrix::matrix(int numRows,...
write a C++ Program for matrix operations which include: 1. constructor in the form-- matrix::matrix(int numRows, int numColumns) 2. Implement a setter method of the form: -- void matrix::setElement(int row, int col) 3 Implement a getter method of the form: -- float matrix::getElement(int row, int col) 4. Make a threaded version of the matrix/matrix addition method. 5. Make a threaded version of the matrix/scalar multiplication method. 6. Matrix/matrix addition methods of the form: -- matrix matrix::matrixAdd(matrix inMatrix) // non-threaded version...
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write a C# console program that fills the right to left diagonal of a square matrix...
Write a C# console program that fills the right to left diagonal of a square matrix with zeros, the lower-right triangle with -1s, and the upper left triangle with +1s. Let the user enter the size of the matrix up to size 21. Use a constant and error check. Output the formatted square matrix with appropriate values. Refer to the sample output below. Sample Run: *** Start of Matrix *** Enter the size of square (<= 21): 5 1 1...
Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of...
Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of a mod n. Your program should allow user to enter a and n. Note: For this question please make sure the code compiles and runs, it is not copied and pasted from elsewhere( I will be checking!). Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT