I want to derive a program that calculates the eigen values and vectors for matrix upto order of 5 X 5. But im not finding a general program and i have to use the switch statement that makes my program extremely lengthy and also it is very difficult to solve it for 5 X 5 matrix. Here is my work.
CODE
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
int matrix;
printf ("Press 1 for eigen values of 2 X 2 Matrix\n");
printf ("Press 2 for eigen values of 3 X 3 Matrix\n");
scanf ("%d",&matrix);
printf("\t\n");
switch(matrix)
{ case 1:
int a,b,c,d;
printf ("Enter the values of Matrix\n");
scanf("%d",&a);
scanf("%d",&b);
printf("\n");
scanf("%d",&c);
scanf("%d",&d);
printf("\t\n");
int l,l1;
l=((a+d)+sqrt((a+d)*(a+d)-4*((a*d)-(b*c))))/2;
l1=((a+d)-sqrt((a+d)*(a+d)-4*((a*d)-(b*c))))/2;
printf("The First Eigen Value is %d\n",l);
printf("The Second Eigen Value is %d\n",l1);
break;
case 2:
int a1,b1,c1,d1,e1,f1,g1,h1,i1;
printf("Enter the values of Matrix\n");
scanf("%d\n",&a1);
scanf("%d\n",&b1);
scanf("%d",&c1);
printf("\n");
scanf("%d\n",&d1);
scanf("%d\n",&e1);
scanf("%d",&f1);
printf("\n");
scanf("%d\n",&g1);
scanf("%d\n",&h1);
scanf("%d" ,&i1);
printf("\n");
int p,q,r;
p = a1+e1+i1;
q = -(a1*e1)-(a1*i1)-(e1*i1)-(f1*h1)+(b1*d1)+(c1*g1);
r = (a1*e1*i1)-(a1*f1*h1)-(b1*d1*i1)+(b1*f1*g1)+(c1*d1*h1)-(c1*g1*e1);
int l2,l3,l4;
l2 = r;
l3 = ( p + sqrt(p*p+ 4*(q+r) ) )/2;
l4 = ( p - sqrt(p*p+ 4*(q+r) ) )/2;
printf(" First Eigen Value is %d\n",l2 );
printf(" Second Eigen Value is %d\n",l3 );
printf(" Third Eigen Value is %d\n",l4 );
break;
default:
printf("You Are Supposed To Enter the Values from 1 & 2 only");
break;
}
}