Sunday, March 31, 2019

Assignment : Matrix Theorem (From Hanf sir)

#include<iostream>
#include<fstream>
#include<stdlib.h>
#define maximum 1000

using namespace std;

int input(int x[][maximum] , int y)
{
    for(int i = 0 ; i < y ; i++)
        for(int j = 0 ; j < y ; j++)
            scanf("%d",(*(x + i) + j));
}

int add(int z[][maximum] , int x[][maximum] , int y[][maximum] , int w)
{
    for(int i = 0 ; i < w ; i++)
        for(int j = 0 ; j < w ; j++)
            *(*(z + i) + j) = *(*(x + i) + j) + *(*(y + i) + j);
}

int mult(int w[][maximum] , int x[][maximum], int y[][maximum], int z)
{
    int p = 0;
    for (int i = 0 ; i < z ; i++)
    {
        for (int j = 0 ; j < z ; j++)
        {
            for (int k = 0 ; k < z ; k++)
                p += (*(*(x + k) + j)) * (*(*(y + i) + k));
            *(*(w + i) + j) = p;
            p = 0;
        }
    }
}

int output(int x[][maximum] , int y)
{
    for(int i = 0 ; i < y ; i++)
        {
            for(int j = 0 ; j < y ; j++)
                cout << x[i][j] << '\t';
            cout << endl;
        }
}


main(void)
{
    int n;
    cout << "Enter Matrix(s) order < Not more than 1,000 >: ";
    cin >> n;
    int a[n][maximum] , b[n][maximum] , c[n][maximum] , asq[n][maximum] , bsq[n][maximum] , ab[n][maximum] , sabsq[n][maximum] , sasbs[n][maximum] , ba[n][maximum] , two_ab[n][maximum] , rhs[n][maximum];
    cout << "Enter Matrix A's elements : \n";
    input(a , n);
    cout << "Enter Matrix B's elements : \n";
    input(b , n);
    add(c , a , b , n);
    mult(sabsq , c , c , n);
    cout << "(A + B)^2 = \n";
    output(sabsq , n);
    cout << endl;
    mult(asq , a , a , n);
    mult(bsq , b , b , n);
    add(sasbs , asq , bsq , n);
    mult(ab , a , b , n);
    mult(ba , b , a , n);
    add(two_ab , ab , ba , n);
    add(rhs , two_ab , sasbs , n);
    cout << "A^2 + 2*A*B + B^2 = \n";
    output(rhs , n);
    cout << endl;
    cout << "Hence, proven that (A + B)^2 = A^2 + 2 * A * B + B^2 where A and B are both homo-ordered metrics.\n";
}

/*    Code using structure   */
/*Logic stays the same though*/

#include<iostream>
#include<fstream>
#include<stdlib.h>
#define maximum 100

using namespace std;

struct mat
{
    int x[maximum][maximum] , y[maximum][maximum];
};

int input(int x[][maximum] , int y)
{
    for(int i = 0 ; i < y ; i++)
        for(int j = 0 ; j < y ; j++)
            scanf("%d",(*(x + i) + j));
}

int add(int z[][maximum] , int x[][maximum] , int y[][maximum] , int w)
{
    for(int i = 0 ; i < w ; i++)
        for(int j = 0 ; j < w ; j++)
            *(*(z + i) + j) = *(*(x + i) + j) + *(*(y + i) + j);
}

int mult(int w[][maximum] , int x[][maximum], int y[][maximum], int z)
{
    int p = 0;
    for (int i = 0 ; i < z ; i++)
    {
        for (int j = 0 ; j < z ; j++)
        {
            for (int k = 0 ; k < z ; k++)
                p += (*(*(x + k) + j)) * (*(*(y + i) + k));
            *(*(w + i) + j) = p;
            p = 0;
        }
    }
}

int output(int x[][maximum] , int y)
{
    for(int i = 0 ; i < y ; i++)
        {
            for(int j = 0 ; j < y ; j++)
                cout << x[i][j] << '\t';
            cout << endl;
        }
}


main(void)
{
    int n;
    cout << "Enter Matrix order < Not more than 100 >: ";
    cin >> n;
    struct mat a[20];
    cout << "Enter Matrix A's elements : \n";
    input(a[0].x , n);
    cout << "Enter Matrix B's elements : \n";
    input(a[1].x , n);
    add(a[2].y , a[0].x , a[1].x , n);
    mult(a[3].y , a[2].y , a[2].y , n);
    cout << "(A + B)^2 = \n";
    output(a[3].y , n);
    cout << endl;
    mult(a[4].y , a[0].x , a[0].x , n);
    mult(a[5].y , a[1].x , a[1].x , n);
    add(a[6].y , a[4].y , a[5].y , n);
    mult(a[7].y , a[0].x , a[1].x , n);
    mult(a[8].y , a[1].x , a[0].x , n);
    add(a[9].y , a[7].y , a[8].y , n);
    add(a[10].y , a[9].y , a[6].y , n);
    cout << "A^2 + 2*A*B + B^2 = \n";
    output(a[10].y , n);
    cout << endl;
    cout << "Proven.\n";
}

No comments:

Post a Comment