Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2010-04-06 16:07:17

kendricktamis
Member
Registered: 2010-04-06
Posts: 4

How to write a c++ code to solve any matrix problem?

I say you have 3 by 3 or 4 by 4 matrix. how do i write a c++ code to solve this matrix problem and also will be able to the program being able to compute any matrix problem.

Offline

#2 2010-04-06 16:53:55

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: How to write a c++ code to solve any matrix problem?

Hi kendricktamis;

Welcome to the forum!

I say you have 3 by 3 or 4 by 4 matrix.

That is not a matrix problem. That is just 2 matrices. What do you want to do with them? Add them, multiply them, subtract them, invert them, get the eigenvalues?


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#3 2011-06-06 02:04:08

Yonah Reuben Ochieng'
Guest

Re: How to write a c++ code to solve any matrix problem?

Use The following code to solve 3*3 matrix and systems
______________________________________________________
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

class elements
    {
        public:
            float det,a11,a12,a13,a21,a22,a23,a31,a32,a33,x1,x2,x3;
            int i,j;
            int input();
            //int determinant(int);
            void copyright();
        ~elements(){};

    }el;
void elements::copyright()
{
    cout<<"\t\tAll Rights Reserved\n\t\tThis S/W is Free_Source By yawhnie@gmail.com"<<endl;
}
int elements::input()
{
    cout<<"Enter no of Elements"<<endl;
    cin>>j;
if(j==9)
    {
            float a[j];//={'\0'};

            for(i=0;i < j;i++)
            {
                    cout<<"Enter elements"<<endl;
                    cin>>a[i];
            }

            cout<<"matrix\t"<<a[0]<<"\t"<<a[1]<<"\t"<<a[2]<<endl;
            cout<<"\t"<<a[3]<<"\t"<<a[4]<<"\t"<<a[5]<<endl;
            cout<<"\t"<<a[6]<<"\t"<<a[7]<<"\t"<<a[8]<<endl;

            if(a[1]==a[3] && a[2]==a[6] && a[5]==a[7])
                {
                    cout<<"This is a Symetric Matrix\n"<<endl;
                }
            if(a[1]== -a[3] && a[2]== -a[6] && a[5]== -a[7])
                {
                    cout<<"This is a Skew-Symetric Matrix\n"<<endl;
                }

            det =((a[0]*(a[8]*a[4]-a[7]*a[5]))-(a[1]*(a[8]*a[3]-a[6]*a[5]))+(a[2]*(a[7]*a[3]-a[6]*a[4])));

            cout<<"determinant = "<<det<<"\n"<<endl;
        if(det != 0)
            {
            /*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
            a11 = pow(-1,2)*(a[8]*a[4]-a[7]*a[5]);
            a12 = pow(-1,3)*(a[8]*a[3]-a[6]*a[5]);
            a13 = pow(-1,4)*(a[7]*a[3]-a[6]*a[4]);
            a21 = pow(-1,3)*(a[8]*a[1]-a[7]*a[2]);
            a22 = pow(-1,4)*(a[8]*a[0]-a[6]*a[2]);
            a23 = pow(-1,5)*(a[7]*a[0]-a[6]*a[1]);
            a31 = pow(-1,4)*(a[5]*a[1]-a[4]*a[2]);
            a32 = pow(-1,5)*(a[5]*a[0]-a[3]*a[2]);
            a33 = pow(-1,6)*(a[4]*a[0]-a[3]*a[1]);

            cout<<"Co-factor\n\t"<<a11<<"\t"<<a12<<"\t"<<a13<<endl;
            cout<<"\t"<<a21<<"\t"<<a22<<"\t"<<a23<<endl;
            cout<<"\t"<<a31<<"\t"<<a32<<"\t"<<a33<<endl;

            cout<<"Transpose\n\t"<<a11<<"\t"<<a21<<"\t"<<a31<<endl;
            cout<<"\t"<<a12<<"\t"<<a22<<"\t"<<a32<<endl;
            cout<<"\t"<<a13<<"\t"<<a23<<"\t"<<a33<<endl;

            cout<<"Inverse\n\t"<<a11<<"/"<<det<<"\t"<<a21<<"/"<<det<<"\t"<<a31<<"/"<<det<<"\n"<<endl;
            cout<<"\t"<<a12<<"/"<<det<<"\t"<<a22<<"/"<<det<<"\t"<<a32<<"/"<<det<<"\n"<<endl;
            cout<<"\t"<<a13<<"/"<<det<<"\t"<<a23<<"/"<<det<<"\t"<<a33<<"/"<<det<<"\n"<<endl;

/*-----------------------------------------------------------------------------------------------------------*/
            int b[3],x=1;
            cout<<"enter b1,b2,b2 i.e a11 + a12 + a13 = b1"<<endl;
            for(int b1=0;b1<3;b1++)
            {
                cout<<"Enter value for b"<<x<<endl;
                x++;
                cin>>b[b1];
            }
                x1 = (a11/det)*b[0]+(a21/det)*b[1]+(a31/det)*b[2];
                x2 = (a12/det)*b[0]+(a22/det)*b[1]+(a32/det)*b[2];
                x3 = (a13/det)*b[0]+(a23/det)*b[1]+(a33/det)*b[2];
                cout<<"______________________________________"<<endl;
                cout<<"x1 = "<<x1<<endl;
                cout<<"x2 = "<<x2<<endl;
                cout<<"x3 = "<<x3<<endl;
            }
        else
            {
                cout<<"This Matrix has no inverse"<<endl;
                return -1;
            }
        }
    else if(j==4)
        {
            cout<<"Gosh! 2 * 2 Matrix is so simple for life \v You don't nedd this to Calculate"<<endl;
            return input();
        }
    else if(j==0)
        {
            _exit -1;
        }
    else
        {
        cout<<"You've Entered a Non-square Matrix. Please use 9 or 4 for this program \n 0 to exit"<<endl;
       return input();
        }
}


int main()
{
    el.input();
    el.copyright();

}

#4 2012-02-02 16:54:04

Roy D.
Guest

Re: How to write a c++ code to solve any matrix problem?

can you help me make a program which would solve linear matrix where the user will decide what would be the dimension of the matrix? in simpler terms, the user will input the dimensions of the matrix and plug-in the values. please... i need it so badly. using c++ language. thank you so much!

Board footer

Powered by FluxBB