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<cmath>
using namespace std;
int main()
{
int row;
float mat[10][10], temp;
cout << "Input row size\n";
cin >> row; // read the row
cout << "Input the matrix to invert" << endl;
for (int i = 1; i <= row; i++)
for (int j = 1; j <= row; j++)
cin >> mat[i][j]; // read the matrix

for (int i = 1; i <= row; i++)
for (int j = 1; j <= 2 * row; j++)
if (j == (i + row))
mat[i][j] = 1;
  
for (int i = row; i > 1; i--)
{
if (mat[i - 1][1] < mat[i][1])
for (int j = 1; j <= row * 2; j++)
{
temp = mat[i][j];
mat[i][j] = mat[i - 1][j];
mat[i - 1][j] = temp;
}
}
  

for (int i = 1; i <= row; i++) // converting them into the diagonal matrix
{
for (int j = 1; j <= row * 2; j++)
if (j != i)
{
temp = mat[j][i] / mat[i][i];
for (int k = 1; k <= row * 2; k++)
mat[j][k] -= mat[i][k] * temp;
}
}
  
for (int i = 1; i <= row; i++) // converting them into the unit matrix
{
temp = mat[i][i];
for (int j = 1; j <= row * 2; j++)
mat[i][j] = mat[i][j] / temp;
}

cout << "the inverse is: " << endl; // print the inverse
for (int i = 1; i <= row; i++)
{
for (int j = row + 1; j <= row * 2; j++)
cout << round(mat[i][j]) << " ";
cout << endl;
}

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 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...
C++ The program you write for this lab will read in the number of nodes and...
C++ The program you write for this lab will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT