C C++ code : power method - numerical method to find eigen value and vector

Working C C++  Source code program for finding eigen value and eigen vector by power method
/************* Eigen value and eigen vector by Power method ***********/

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
    float a[10][10],x[10],c[10],d=0,temp;
    int n,i,j;
    clrscr();
    cout<<"Enter the order of matrix ? ";
    cin>>n;
    cout<<"\nCoefficients of matrix ";
    for(i=0;i<n;i++)
    {
        cout<<endl<<"row  "<<i+1<<"  ";
        for(j=0;j<n;j++)
            cin>>a[i][j];
    }

    cout<<"Enter your starting vector ? ";
    for(i=0;i<n;i++)
    cin>>x[i];

    do
    {
        for(i=0;i<n;i++)
        {
            c[i]=0;
            for(j=0;j<n;j++)
                c[i]+=a[i][j]*x[j];
        }
        for(i=0;i<n;i++)
            x[i]=c[i];
            
        temp=d;
        d=0;
        
        for(i=0;i<n;i++)
        {
            if(fabs(x[i])>fabs(d))
                d=x[i];
        }
        for(i=0;i<n;i++)
            x[i]/=d;
            
    }while(fabs(d-temp)>0.00001);

    cout<<"Eigen value is : "<<d<<endl;
    
    cout<<"Eigenvector is: ";
    for(i=0;i<n;i++)
        cout<<endl<<x[i];

    getch();
    return 0;
}

    


2 comments :

Your Comment and Question will help to make this blog better...