Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how can i make a program to solve linear equation ? up to 10 equations ?

–1 vote
asked Jun 20, 2018 by Mariam Ashraf (110 points)

1 Answer

0 votes
answered Apr 5, 2019 by aditya chhaparia (150 points)
#include <iostream>
#include <stdlib.h>
#include <math.h>

using namespace std;

class matrix
{   
    private:
        float num_array[15][15];
        int m;
        int n;
    
    public:
        float num(int i,int j);
        int row();
        void setrow(int a);
        int col();
        void setcol(int a);
        void set(int i,int j,float f);
        void add(int i,int j,float f);
        void input();
        void output();
        void intitialize();
};

float matrix::num(int i,int j)
{
    return num_array[i][j];
}

int matrix::row()
{
    return m;
}

int matrix::col()
{
    return n;
}

void matrix::setrow(int a)
{
    m=a;
}

void matrix::setcol(int a)
{
    n=a;
}

void matrix::set(int i,int j,float f)
{
    num_array[i][j]=f;
}

void matrix::add(int i,int j,float f)
{
    num_array[i][j]+=f;
}

void matrix::intitialize()
{
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            num_array[i][j]=0;
}

void matrix::input()
{
    cout<<"enter rows and cols"<<endl;
    cin>>m>>n;
    cout<<"enter matrix"<<endl;
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            cin>>num_array[i][j];
}

void matrix::output()
{
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            cout<<num_array[i][j]<<"\t";
        }
        cout<<"\n";
    }
}

float determinant(matrix a)
{
    if(a.row()!=a.col())
    {
        cout<<"logic error 1";
        exit(0);
    }
    else
    {
        float det=0;
        if(a.row()==2)
        det=a.num(0,0)*a.num(1,1)-a.num(0,1)*a.num(1,0);
        else if(a.row()==1)
            return a.num(0,0);
        else
        {
            int i,j,k,i2=0,j2=0;
            
            
            for(i=0;i<a.row();i++)
            {
                matrix m;
                m.setrow(a.row()-1);
                m.setcol(a.col()-1);
                for(j=1;j<a.row();j++)
                {
                    for(k=0;k<a.row();k++)
                    {
                        if(k==i)
                            continue;
                            m.set(i2,j2,a.num(j,k));
                            j2++;
                    }
                    j2=0;
                    i2++;
                }
                i2=0;
                det+=pow(-1,i)*a.num(0,i)*determinant(m);
            }
        }
        return det;
    }
}

matrix multiply(matrix a,matrix b)
{
    matrix c;
    if(a.col()!=b.row())
    {
        cout<<"logic error 2";
        exit(0);
    }
    else
    {
        c.setrow(a.row());
        c.setcol(b.col());
        c.intitialize();
        for(int i=0;i<a.row();i++)
        {
            for(int j=0;j<b.col();j++)
            {
                for(int k=0;k<a.col();k++)
                c.add(i,j,a.num(i,k)*b.num(k,j));
            }
        }
        return c;   
    }
}

matrix s_multiply(matrix a,float b)
{
    matrix c;
    c.setrow(a.row());
    c.setcol(a.col());
    for(int i=0;i<a.row();i++)
        for(int j=0;j<a.col();j++)
            c.set(i,j,b*a.num(i,j));
    return c;
}

matrix adjoint(matrix a)
{
    if(a.row()!=a.col())
    {
        cout<<"logic error 3";
        exit(0);
    }
    else
    {   
        matrix c;
        c.setrow(a.row());
        c.setcol(a.col());
        int i,j,k,l,i2,j2;
        for(i=0;i<a.row();i++)
        {
            for(j=0;j<a.col();j++)
            {
                matrix m;
                m.setrow(a.row()-1);
                m.setcol(a.col()-1);
                i2=0;
                j2=0;
                for(k=0;k<a.row();k++)
                {
                    if(k==i)
                        continue;
                    for(l=0;l<a.col();l++)
                    {
                        if(l==j)
                            continue;
                        m.set(i2,j2,a.num(k,l));
                        j2++;
                    }
                    j2=0;
                    i2++;
                }
                float x;
                x=pow(-1,i+j)*determinant(m);
                c.set(j,i,x);
            }
        }
        return c;
    }
}

void readline(char var[],matrix &a,int x,int n)
{
    char line[50];
    cin>>line;
    int i=0,j=0,pos=0;
    int len;
    for(len=0;line[len]!='\0';len++);
    int flag;
    int sign;
    for(flag=0;line[flag]!='=';flag++);
    if(flag>len||flag==0)
    {
        cout<<"mistake in eqn. equal to sign missing or both sides are not complete";
        exit(0);
    }
    while(pos<len)
    {   
        if(pos<flag)
            sign=1;
        else if(pos==flag)
        {
            sign=-1;
            pos++;
        }
        else
            sign=-1;
        
        int varno,no;
        
        char ch;
        ch=line[pos];
        if(ch=='+')
        {
            pos++;
        }
        else if(ch=='-')
        {
            pos++;
            sign=-sign;
        }
        
        i=pos;

        ch=line[i];
        no=0;
        varno=n;
        while(ch>='0'&&ch<='9')
        {
            no=no*10+ch-48;
            i++;
            ch=line[i];
        }
        
        j=0;
        while(j<n-1)
        {
            if(ch==var[j])
                break;
            j++;
        }
        if(j<n-1)
        {
            varno=j;
            if(no==0&&pos==i)
                no=1;
            i++;
        }
        else if(ch=='=')
        {
            varno=n-1;
            i++;
        }
        else if(ch=='-'||ch=='+')
        {
            varno=n-1;
            i++;
        }
        else if(i==len-1||i==len)
            varno=n-1;
        else
        {
            cout<<"mistake in eqn. unidentified character";
            exit(0);
        }
        if(i==pos)
            pos++;
        else
            pos=i;
        float z;
        if(varno==n-1)
            sign=-sign;
        z=sign*no;
        a.add(x,varno,z);
    }
}

int main()
{
    int n;
    cout<<"enter no of variables 2 to 10"<<endl;
    cin>>n;
    char var[15];
    cout<<"enter variables (single alphabet)"<<endl;
    for(int i=0;i<n;i++)
        cin>>var[i];
    matrix a;
    a.setrow(n);
    a.setcol(n+1);
    a.intitialize();
    cout<<"enter "<<n<<" equations"<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<"enter equation number "<<i+1<<endl;
        readline(var,a,i,n+1);
    }
    matrix b;
    b.setrow(n);
    b.setcol(n);
    b.intitialize();
    matrix c;
    c.setrow(n);
    c.setcol(1);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            b.set(i,j,a.num(i,j));
    for(int i=0;i<n;i++)
        c.set(i,0,a.num(i,n));
    if(determinant(b)==0)
        cout<<"set of equations is inconsistent";
    else
    {
        float y;
        y=determinant(b);
        y=1/y;
        b=adjoint(b);
        c=multiply(b,c);
        c=s_multiply(c,y);
        for(int i=0;i<n;i++)
            cout<<var[i]<<" = "<<c.num(i,0)<<endl;
    }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...