Skip to content

Instantly share code, notes, and snippets.

@jviereck
Created November 21, 2010 17:34
Show Gist options
  • Save jviereck/708938 to your computer and use it in GitHub Desktop.
Save jviereck/708938 to your computer and use it in GitHub Desktop.
// Informatik - Serie 8 - Skript-Aufgabe 81
// Program: inverse_matrix.cpp
// Invert a 3x3 matrix.
// Autor: Tim Menke Plestudent (Gruppe C)
#include <iostream>
int main()
{
// Initialization
float A[3][3];
// Input
std::cout << "Please input a 3x3 matrix A with real entries.\n";
// i: Zeilen
for (unsigned int i = 0; i < 3; ++i) {
// j: Spalten --> Aij
for (unsigned int j = 0; j < 3; ++j) {
std::cin >> A[i][j];
}
}
std::cout << "\n\nA=\n";
for (int i = 0; i < 3; i++) {
std::cout << " " << A[i][0] << "\t" << A[i][1] << "\t" << A[i][2] << "\n";
}
// Compute determinant
float det = 0;
std::cout << "0det(A) = " << det << "\n";
det += A[0][0]*A[1][1]*A[2][2];
std::cout << "1det(A) = " << det << "\n";
det += A[0][1]*A[1][2]*A[2][0];
std::cout << "2det(A) = " << det << "\n";
det += A[0][2]*A[1][0]*A[2][1];
std::cout << "3det(A) = " << det << "\n";
det -= A[2][0]*A[1][1]*A[0][2];
std::cout << "4det(A) = " << det << "\n";
det -= A[2][1]*A[1][2]*A[0][0];
std::cout << "5det(A) = " << det << "\n";
det -= A[2][2]*A[1][0]*A[0][1];
std::cout << "6det(A) = " << det << "\n";
//Output matrix A
for (unsigned int i = 0; i < 3; ++i) {
for (unsigned int j = 0; j < 3; ++j)
std::cout << A[i][j] << " ";
std::cout << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment