Monday, April 8, 2019

Assignment : Adding and substracting out of range numbers in C++ (Assigned by : Hanif sir)

// Hello stranger. ^_^


#include<iostream>

#include<algorithm>

#include<string>


using namespace std;


main(void)

{

    int g = 0 , ca = 0 , si;

    string a , b , c , blank;

    cin >> a >> b;

    si = max(a.size() , b.size());

    if(a < b)

        swap(a , b);

    if(a.size() < b.size())

    {

        blank.resize(si - a.size());

        a = blank + a;

    }

    else

    {

        blank.resize(si - b.size());

        b = blank + b;

    }

    c.resize(si + 1);

    for(int i = si - 1 , k = 0 ; i >= 0 ; i-- , k++)

    {

        if(isdigit(a.at(i)) == true and isdigit(b.at(i)) == true)

            c.at(k) = a.at(i) + b.at(i) - 48 + ca;

        else if(isdigit(a.at(i)) == true and isdigit(b.at(i)) == false)

            c.at(k) = a.at(i) + ca;

        else if(isdigit(b.at(i)) == true and isdigit(a.at(i)) == false)

            c.at(k) = b.at(i) + ca;

        ca = 0;

        if(c.at(k) - 48 >= 10)

        {

            c.at(k) = c.at(k) - 10;

            ca = 1;

            g++;

        }

    }

    reverse(c.begin() , c.end());

    if(ca == 1)

        c.at(0) = '1';

    else

        c.erase(c.begin());

    if(a < b)

        swap(a , b);

    cout << ' ' << a << endl << '+' << b << endl;

    for(int i = 0 ; i <= c.size() ; i++)

        cout << '-';

    cout << endl << c << endl << "Number of Carries: " << g << endl;

}


// Minor changes for subtractions.

//You may want to combine both of them together.

//But I reckon making a project will help since linkers work faster than a gigantic diver code.


#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

main(void)
{
    int g = 0 , ca = 0 , si , t = 0;
    string a , b , c , blank;
    cin >> a >> b;
    if(a < b)
    {
        swap(a , b);
        t = 1;
    }
    si = max(a.size() , b.size());
    if(a.size() < b.size())
    {
        blank.resize(si - a.size());
        a = blank + a;
    }
    else
    {
        blank.resize(si - b.size());
        b = blank + b;
    }
    c.resize(si + 1);
    for(int i = si - 1 , k = 0 ; i >= 0 ; i-- , k++)
    {
        if(isdigit(a.at(i)) == true and isdigit(b.at(i)) == true)
            c.at(k) = a.at(i) - b.at(i) + 48 - ca;
        else if(isdigit(a.at(i)) == true and isdigit(b.at(i)) == false)
            c.at(k) = a.at(i) - ca;
        else if(isdigit(b.at(i)) == true and isdigit(a.at(i)) == false)
            {
                c.at(k) = b.at(i);
                ca = 1;
                continue;
            }
        ca = 0;
        if(c.at(k) < 48)
        {
            c.at(k) += 10;
            ca = 1;
            g++;
        }
    }
    reverse(c.begin() , c.end());
    if(ca == 1 or t == 1)
        c.at(0) = '-';
    else
        c.erase(c.begin());
    if(a < b)
        swap(a , b);
    cout << ' ' << a << endl << '-' << b << endl;
    for(int i = 0 ; i <= c.size() ; i++)
        cout << '-';
    cout << endl << c << endl << "Number of Carries: " << g << endl;
}

// Don't follow my code. It's completely composed out of brute force. Learn algorithm for these type of programs , which I'm still learning. ^_^

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";
}