C++ A matrix is a rectangular array of numbers that is arranged in a two-dimensional table. Matrices have numerous applications in computer systems. Matrix structures are examined in computer memory, graphics, and cryptography. The Hill cipher, for example, makes use of matrix inversions to encrypt and decrypt data. The intent of this problem is to develop a C++ program that computes and displays the inversion of a 2 x 2 matrix. A matrix is said to have the dimensions m x n if it has m rows and n columns. A square matrix has the same number of rows as columns, and is therefore said to have m x m dimensions. Matrix M shown below is an example of a 2 x 2 matrix. The inverse of a square matrix, M-1, is defined by the equation M(M-1) = I, where I is called the identity matrix. I is a square matrix that is all zeros except for ones along the main diagonal from the upper left corner to the lower right corner. I for a 2 x 2 matrix is: Multiplying a square matrix by its inverse will always produce the identity matrix. Some square matrices do not have an inverse. These matrices are called singular, or noninvertible. In order to determine if a matrix is invertible, a value known as the determinant can be calculated. The determinant of a matrix is written as det M. If det M is equal to zero, then the matrix is singular and therefore has no inverse. For this discussion we will only consider 2 x 2 matrices. Larger matrices become increasingly complex. The general form of a 2 x 2 matrix is: We can calculate the determinate of M using the following formula: Now we can calculate the determinant of the above example matrix. Since det M ≠ 0, M is invertible. We can find the inverse of a 2 x 2 matrix using this formula: We simply multiply the inverse of the determinant by the augmented matrix where a and d are switched and b and c are replaced with –b and –c respectively. Since division-by-zero is undefined, it is now clear why a determinant of zero yields a noninvertible matrix. When multiplying a matrix by a single value, called a scalar, we multiply each entry in the matrix by the scalar. Using the same matrix example above, lets calculate its inverse. The Hill Cipher is a cryptographic algorithm that uses matrix inversions to decrypt ciphertext into plaintext. The original message, called the plaintext, is converted into matrix form. We will call this matrix P. We then multiply it by a key in matrix form, called matrix K. The product of P and K is the encrypted message. Assuming the intended recipient knows K, it can decrypt the message by multiplying the encrypted message by K-1. Plaintext Message = P Encrypted Message = PK Decrypted Message = (PK)K-1 = P Matrix multiplication is not covered in this discussion, but the curious student is encouraged to research matrix multiplication and work an example. You task is to write a C++ program that calculates the determinant and the inverse of an invertible 2 x 2 matrix. Your program should prompt the user for the matrix entries and display the determinant and the inverse entries. Much of the program is written for us. We need to fill in the blank and do some code analysis to see what is going. Complete the missing code segments that are highlighted. Compile and execute the program and provide a screenshot of your running program. #include using namespace std; int main() { int a, b, determinant, matrix[2][2], inv[2][2]; cout << "Enter the 2 x 2 Matrix in the form of four integers separated by a whitespace: \n"; for (a = 0; a < 2; a++) for (b = 0; b < 2; b++) cin >> matrix[a][b]; // Calculate the determinant: determinant = matrix[0][0] * matrix[1][1] - /*FINISH THE EQUATION*/; // Test if determinant is 0 to avoid divide by 0 errors at run-time: if (determinant == 0) { cout << "Matrix is singular.\n"; return 0; // This ends the program before any further action is taken } // Calculate the inverse matrix: for (a = 0; a < 2; a++) for ( /* ?? WHAT GOES HERE ?? */) { if (a == 0 && b == 0) inv[a][b] = matrix[1][1]; else if (a == 1 && b == 1) inv[a][b] = matrix[0][0]; else inv[a][b] = -matrix[a][b]; } cout << "\nDeterminant: " << determinant; cout << "\nInverse Matrix: " << "\n\n"; // Display inverse matrix: for (/* ?? WHAT GOES HERE ?? */) { for ( /* ?? WHAT GOES HERE ?? */) cout << (float)inv[a][b]/determinant << "\t"; cout << endl; } return 0; }
C++
A matrix is a rectangular array of numbers that is arranged in a two-dimensional table. Matrices have numerous applications in computer systems. Matrix structures are examined in
A matrix is said to have the dimensions m x n if it has m rows and n columns. A square matrix has the same number of rows as columns, and is therefore said to have m x m dimensions. Matrix M shown below is an example of a 2 x 2 matrix.
The inverse of a square matrix, M-1, is defined by the equation M(M-1) = I, where I is called the identity matrix. I is a square matrix that is all zeros except for ones along the main diagonal from the upper left corner to the lower right corner. I for a 2 x 2 matrix is:
Multiplying a square matrix by its inverse will always produce the identity matrix. Some square matrices do not have an inverse. These matrices are called singular, or noninvertible. In order to determine if a matrix is invertible, a value known as the determinant can be calculated. The determinant of a matrix is written as det M. If det M is equal to zero, then the matrix is singular and therefore has no inverse. For this discussion we will only consider 2 x 2 matrices. Larger matrices become increasingly complex. The general form of a 2 x 2 matrix is:
We can calculate the determinate of M using the following formula:
Now we can calculate the determinant of the above example matrix.
Since det M ≠ 0, M is invertible. We can find the inverse of a 2 x 2 matrix using this formula:
We simply multiply the inverse of the determinant by the augmented matrix where a and d are switched and b and c are replaced with –b and –c respectively. Since division-by-zero is undefined, it is now clear why a determinant of zero yields a noninvertible matrix.
When multiplying a matrix by a single value, called a scalar, we multiply each entry in the matrix by the scalar. Using the same matrix example above, lets calculate its inverse.
The Hill Cipher is a cryptographic
Plaintext Message = P
Encrypted Message = PK
Decrypted Message = (PK)K-1 = P
Matrix multiplication is not covered in this discussion, but the curious student is encouraged to research matrix multiplication and work an example.
You task is to write a C++ program that calculates the determinant and the inverse of an invertible 2 x 2 matrix. Your program should prompt the user for the matrix entries and display the determinant and the inverse entries. Much of the program is written for us. We need to fill in the blank and do some code analysis to see what is going.
Complete the missing code segments that are highlighted. Compile and execute the program and provide a screenshot of your running program.
#include <iostream>
using namespace std;
int main()
{
int a, b, determinant, matrix[2][2], inv[2][2];
cout << "Enter the 2 x 2 Matrix in the form of four integers separated by a whitespace: \n";
for (a = 0; a < 2; a++)
for (b = 0; b < 2; b++)
cin >> matrix[a][b];
// Calculate the determinant:
determinant = matrix[0][0] * matrix[1][1] - /*FINISH THE EQUATION*/;
// Test if determinant is 0 to avoid divide by 0 errors at run-time:
if (determinant == 0)
{
cout << "Matrix is singular.\n";
return 0; // This ends the program before any further action is taken
}
// Calculate the inverse matrix:
for (a = 0; a < 2; a++)
for ( /* ?? WHAT GOES HERE ?? */)
{
if (a == 0 && b == 0)
inv[a][b] = matrix[1][1];
else if (a == 1 && b == 1)
inv[a][b] = matrix[0][0];
else
inv[a][b] = -matrix[a][b];
}
cout << "\nDeterminant: " << determinant;
cout << "\nInverse Matrix: " << "\n\n";
// Display inverse matrix:
for (/* ?? WHAT GOES HERE ?? */)
{
for ( /* ?? WHAT GOES HERE ?? */)
cout << (float)inv[a][b]/determinant << "\t";
cout << endl;
}
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images