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 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...
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)...
Lab 1 Write a C program for a grade calculation to run on ocelot. The source...
Lab 1 Write a C program for a grade calculation to run on ocelot. The source file should have your name & PantherID included in it and it should have an affirmation of originality stating something like: “I affirm that I wrote this program myself without any help form any other people or sources from the internet.”. Code should be nicely indented using a consistent style and commented appropriately. An array should be used for each student, but it is...
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user...
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user for the number of GPAs to enter. The program should then prompt the user to enter the specified number of GPAs. Finally, the program should print out the graduation standing of the students based on their GPAs. Your program should behave like the sample output below. Sample #1: Enter the number of GPAs: 5 GPA #0: 3.97 GPA #1: 3.5 GPA #2: 3.499 GPA...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT