Answer to Question #100806 in C++ for zahra

Question #100806
Write a program that will find the inverse of any two dimensional array without the usage of
the built-in function. solve with do_while
1
Expert's answer
2020-01-06T05:05:08-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 i, k, j;
    int **array = new int *[M];
    i = 0;
    do {
        array[i] = new int[N];
    } while (++i < M);

    double **gauss = new double *[M], mult;
    i = 0;
    do {
        gauss[i] = new double[2 * N];
    } while (++i < M);

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

    i = 0;
    do {
        mult = 1 / gauss[i][i];
        j = i;
        do {
            gauss[i][j] *= mult;
        } while (++j < 2 * N);

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

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

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

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

    i = 0;
    do {
        delete[] array[i];
        delete[] gauss[i];
    } while (++i < M);
    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