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  

Please, i have had two people answer this question but the output gives me Garbage. Please help solve this problem using C++.

Solutions

Expert Solution

#include <iostream>
#include <cmath>

using namespace std;

void inverse(float mat[][10], int order) 
{ 
    float temp; 
    // Creating the augmented matrix for gauss elemination method 
    for (int i = 0; i < order; i++) { 
  
        for (int j = 0; j < 2 * order; j++) { 
            if (j == (i + order)) 
                mat[i][j] = 1; 
        } 
    } 
   
    // Interchanging of rows of the matrix 
    for (int i = order - 1; i > 0; i--) { 
 
        if (mat[i - 1][0] < mat[i][0])
        {
            for (int j = 0; j < 2 * order; j++) 
            { 
                temp = mat[i][j]; 
                mat[i][j] = mat[i - 1][j]; 
                mat[i - 1][j] = temp; 
            } 
        }
    } 
 
    for (int i = 0; i < order; i++) { 
  
        for (int j = 0; j < order; j++) { 
  
            if (j != i) { 
  
                temp = mat[j][i] / mat[i][i]; 
                for (int k = 0; k < 2 * order; k++) { 
  
                    mat[j][k] -= mat[i][k] * temp; 
                } 
            } 
        } 
    } 
  
    // Multipling each row by a nonzero integer and then dividing a row element by the diagonal element 
    for (int i = 0; i < order; i++) { 
  
        temp = mat[i][i]; 
        for (int j = 0; j < 2 * order; j++) { 
  
            mat[i][j] = round(mat[i][j] / temp);    
        } 
    } 
  
    // print the resultant Inverse matrix. 
    cout<<"\nthe inverse is\n"; 
    for (int i = 0; i < order; i++)
    { 
        for (int j = order; j < 2*order; j++) 
        { 
            cout<<mat[i][j]<<" "; 
        } 
        cout<<"\n"; 
    }  
  
    return; 
} 

int main()
{
    int r;
    float mat[10][10];
    cout<<"input row size ";
    cin>>r;
    cout<<"\ninput the matrix to invert\n";
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<r;j++)
        {
            cin>>mat[i][j];
        }
    }
    inverse(mat,r);

    return 0;
}

output:


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 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
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT