[C++} Implementing \"Finding the determinant and the inverse for 3x3 matrices\"
ID: 3885179 • Letter: #
Question
[C++} Implementing "Finding the determinant and the inverse for 3x3 matrices" into my class/functions
Hi so I need to write a class to perform the calculations for addition, subtraction, finding the determinant, and the inverse for 3x3 matrices.
I've already done the addition and subtraction part. I've also done the math parts for the finding the determinant and the inverse for the 3x3 matrices. (Although I can't really verify if it's right, because I am struggling to implement it into my code.) So here's the link to the code https://repl.it/Kg0S/12, alternatively:
//Matrix.cpp
#include
using namespace std;
class Matrix{
private :
double A[3][3];
double B[3][3];
double result[3][3];
public :
void input(){
cout << "Input 9 elements into your 3x3 matrix A: ";
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cin >> A[i][j];
// terminates function on failure
if(cin.fail()) {
cin.clear();
cin.ignore(100, ' ');
cerr << " ERROR: Please enter valid input! " << endl;
}
}
}
cout << "Input 9 elements into your 3x3 matrix B: ";
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cin >> B[i][j];
// terminates function on failure
if(cin.fail()) {
cin.clear();
cin.ignore(100, ' ');
cerr << " ERROR: Please enter valid input! " << endl;
}
}
}
}
void print(double A1[3][3]){
for(int i = 0; i < 3; i++) {
cout << "[";
for(int j = 0; j < 3; j++) {
cout << A1[i][j] << " ";
}
cout << "]" << endl;
}
}
void addition(){
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
print(result);
}
void subtraction(){
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
result[i][j] = A[i][j] - B[i][j];
}
}
print(result);
}
void determinant();
void inverse();
};
int main() {
bool choice;
char input;
Matrix obj;
double a[3][3], b[3][3];
cout << " When filling up matrices, separate individual elements by a space (e.g 2 4 1.4 56.3 ...) ";
obj.input();
//cout << " Matrix A: ";
//obj.print(a);
//cout << " Matrix B: ";
//obj.print(b);
//Displaying a menu, so that the user can choose what he wants to do
choice = false;
while(!choice) {
cout << " " << endl;
cout <<"** Choose from the following **"<< endl;
cout << " " << endl;
cout << "a - Addition" << endl;
cout << "s - Subtraction" << endl;
cout << "d - Determinant" << endl;
cout << "i - Inverse" << endl;
cout << "q - Quit" << endl;
cout << " " << endl;
cout << "Enter your choice: ";
cin >> input;
cout << endl;
switch(input){
case 'a': case 'A': obj.addition();
break;
case 's': case 'S': obj.subtraction();
break;
//case 'd'|'D': obj.determinant();
// break;
//case 'i': case 'I': obj.inverse();
// break;
case 'q': case 'Q': exit(0);
default:cout<<" ERROR: Please enter valid input! ";
}
}
return 0;
}
Now here's the math part done for finding the determinant and the inverse (I am not sure if I have done that right):
//finding the determinant
for(i = 0; i < 3; i++)
determin = determin + (A[0][i] * (A[1][(i+1)%3] * A[2][(i+2)%3] - A[1][(i+2)%3] * A[2][(i+1)%3]));
cout<<" determinant is : "<
cout<<" Inverse of matrix is: ";
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++)
cout<<((A[(j+1)%3][(i+1)%3] * A[(j+2)%3][(i+2)%3]) - (A[(j+1)%3][(i+2)%3] * A[(j+2)%3][(i+1)%3]))/ determin<<" ";
cout<<" ";
}
return 0;
}
So essentialy, my question boils down to "How can I implement this part of code above so that my functions/function call work?"
Also I don't mind if you find another way to find the determinant and the inverse of 3x3 as long as it kind of fits into my code and works.
Thank you! Also, please only answers that actually work...
Explanation / Answer
#include <iostream>
using namespace std;
class Matrix{
private :
double A[3][3];
double B[3][3];
double result[3][3];
public :
void input(){
cout << "Input 9 elements into your 3x3 matrix A: ";
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cin >> A[i][j];
// terminates function on failure
if(cin.fail()) {
cin.clear();
cin.ignore(100, ' ');
cerr << " ERROR: Please enter valid input! " << endl;
}
}
}
cout << "Input 9 elements into your 3x3 matrix B: ";
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cin >> B[i][j];
// terminates function on failure
if(cin.fail()) {
cin.clear();
cin.ignore(100, ' ');
cerr << " ERROR: Please enter valid input! " << endl;
}
}
}
}
void print(double A1[3][3]){
for(int i = 0; i < 3; i++) {
cout << "[";
for(int j = 0; j < 3; j++) {
cout << A1[i][j] << " ";
}
cout << "]" << endl;
}
}
void printAB(){
cout<<" Matrix A :"<<endl;
for(int i = 0; i < 3; i++) {
cout << "[";
for(int j = 0; j < 3; j++) {
cout << A[i][j] << " ";
}
cout << "]" << endl;
}
cout<<" Matrix B :"<<endl;
for(int i = 0; i < 3; i++) {
cout << "[";
for(int j = 0; j < 3; j++) {
cout << A[i][j] << " ";
}
cout << "]" << endl;
}
}
void addition(){
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
print(result);
}
void subtraction(){
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
result[i][j] = A[i][j] - B[i][j];
}
}
print(result);
}
int determinant()
{
int determin = 0;
//finding the determinant
for(int i = 0; i < 3; i++)
determin = determin + (A[0][i] * (A[1][(i+1)%3] * A[2][(i+2)%3] - A[1][(i+2)%3] * A[2][(i+1)%3]));
return determin;
}
void inverse()
{
cout<<" Inverse of matrix is: ";
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++)
cout<<((A[(j+1)%3][(i+1)%3] * A[(j+2)%3][(i+2)%3]) - (A[(j+1)%3][(i+2)%3] * A[(j+2)%3][(i+1)%3]))/ determinant()<<" ";
cout<<" ";
}
}
};
int main() {
bool choice;
char input;
Matrix obj;
cout << " When filling up matrices, separate individual elements by a space (e.g 2 4 1.4 56.3 ...) ";
obj.input();
obj.printAB();
//Displaying a menu, so that the user can choose what he wants to do
choice = false;
while(!choice) {
cout << " " << endl;
cout <<"** Choose from the following **"<< endl;
cout << " " << endl;
cout << "a - Addition" << endl;
cout << "s - Subtraction" << endl;
cout << "d - Determinant" << endl;
cout << "i - Inverse" << endl;
cout << "q - Quit" << endl;
cout << " " << endl;
cout << "Enter your choice: ";
cin >> input;
cout << endl;
switch(input){
case 'a': case 'A': obj.addition();
break;
case 's': case 'S': obj.subtraction();
break;
case 'd'|'D': cout<<" determinant is : "<<obj.determinant();
break;
case 'i': case 'I': obj.inverse();
break;
case 'q': case 'Q': exit(0);
default:cout<<" ERROR: Please enter valid input! ";
}
}
return 0;
}
Output:
When filling up matrices, separate individual elements by a space (e.g 2 4 1.4 56.3 ...)
Input 9 elements into your 3x3 matrix A: 1 2 3 1 2 4 3 4 5
Input 9 elements into your 3x3 matrix B: 1 1 2 3 1 2 2 4 3
Matrix A :
[1 2 3 ]
[1 2 4 ]
[3 4 5 ]
Matrix B :
[1 2 3 ]
[1 2 4 ]
[3 4 5 ]
** Choose from the following **
a - Addition
s - Subtraction
d - Determinant
i - Inverse
q - Quit
Enter your choice: a
[2 3 5 ]
[4 3 6 ]
[5 8 8 ]
** Choose from the following **
a - Addition
s - Subtraction
d - Determinant
i - Inverse
q - Quit
Enter your choice: s
[0 1 1 ]
[-2 1 2 ]
[1 0 2 ]
** Choose from the following **
a - Addition
s - Subtraction
d - Determinant
i - Inverse
q - Quit
Enter your choice: d
determinant is : 2
** Choose from the following **
a - Addition
s - Subtraction
d - Determinant
i - Inverse
q - Quit
Enter your choice: i
Inverse of matrix is:
-3 1 1
3.5 -2 -0.5
-1 1 0
** Choose from the following **
a - Addition
s - Subtraction
d - Determinant
i - Inverse
q - Quit
Enter your choice: q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.