Answer to Question #100804 in C++ for zahra

Question #100804
Write a program that will find the inverse of any two dimensional array without the usage of
the built-in function. solve with loop for
1
Expert's answer
2019-12-27T02:13:33-0500
#include <iostream>

int main() {
    int M = 0;
    std::cout << "Enter M: ";
    std::cin >> M;

    int N = 0;
    std::cout << "Enter N: ";
    std::cin >> N;

    if (M <= 0 || M < N || N < M) {
        std::cout << "Oops, what a mess..." << std::endl;
        return 1;
    }

    int **array = new int *[M];
    for (int i = 0; i < M; i++)
        array[i] = new int[N];

    double **gauss = new double *[M], mult;
    for (int i = 0; i < M; i++)
        gauss[i] = new double[2 * N];

    for (int i = 0; i < M; i++) {
        std::cout << "Enter " << i + 1 << "-th row (" << N
            << " numbers separated by a whitespace): ";
        for (int j = 0; j < N; j++) {
            std::cin >> array[i][j];
            gauss[i][j] = array[i][j];
            gauss[i][j + N] = double(i == j);
        }
    }

    for (int i = 0; i < M; i++) {
        mult = 1 / gauss[i][i];
        for (int j = i; j < 2 * N; j++)
            gauss[i][j] *= mult;

        for (int k = i + 1; k < M; k++) {
            mult = gauss[k][i] / gauss[i][i];
            for (int j = i; j < 2 * N; j++)
                gauss[k][j] -= gauss[i][j] * mult;
        }
    }
    // The left matrix is now in echelon form

    for (int i = M - 1; i >= 0; i--) {
        for (int k = i - 1; k >= 0; k--)  {
            mult = gauss[k][i] / gauss[i][i];
            for (int j = i; j < 2 * N; j++)
                gauss[k][j] -= gauss[i][j] * mult;
        }
    }
    // The left matrix is now in reduced echelon form

    std::cout << "\nInput matrix:\n";
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++) {
            std::cout.width(7);
            std::cout << array[i][j] << ((j + 1 != N) ? ' ' : '\n');
        }

    std::cout << "\nInverse matrix:\n";
    std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
    std::cout.precision(4);
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++) {
            std::cout.width(7);
            std::cout << gauss[i][j + N] << ((j + 1 != N) ? ' ' : '\n');
        }

    for (int i = 0; i < M; i++) {
        delete[] array[i];
        delete[] gauss[i];
    }
    delete[] array;
    delete[] gauss;
}

/*
Algorithm:
intmath.com/matrices-determinants/inverse-matrix-gauss-jordan-elimination.php

Input:
3
3
4 6 7
8 10 5
12 11 9

Output:
Enter M: 3
Enter N: 3
Enter 1-th row (3 numbers separated by a whitespace): 4 6 7
Enter 2-th row (3 numbers separated by a whitespace): 8 10 5
Enter 3-th row (3 numbers separated by a whitespace): 12 11 9

Input matrix:
      4       6       7
      8      10       5
     12      11       9

Inverse matrix:
-0.2244 -0.1474  0.2564
 0.0769  0.3077 -0.2308
 0.2051 -0.1795  0.0513
*/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS