Rapster is back!!!.. Its been a long time since I blogged in my favourite blog and an even longer time though since I mailed. But right now, this is the program of the moment.
This program is a multiple variable equation solver. And when I say MULTIPLE, i mean MULTIPLE!!! This program also includes inside the transpose program, the inverse program and the finding of value of a determinant program. (Essentially, thats why it took me so long to finish!!!). So, without further ado, I present to you, my most prestigious program yet!
#include <iostream.h>
#include <math.h>
struct matrix
{
double* x;
matrix()
{
x=NULL;
}
~matrix()
{
delete []x;
}
};
double find_val(matrix* x, int size);
void multiply_matrix(matrix* x, int size, double value);
void transpose(matrix* x, int size);
void find_adjoint(matrix* x, matrix* y, int size);
void multiply_matrices(matrix* x, matrix* y, matrix* z,int size);
int check_for_singularity(matrix* x, int size);
int main()
{
matrix *coeffs, *consts, *inverse, *soln, *temp;
int size;
char* variables=new char [size];
cout << “Enter no of variables: “;
cin >> size; cout << endl;
coeffs=new matrix [size];
inverse=new matrix [size];
variables=new char [size];
consts=new matrix [size];
soln=new matrix [size];
temp=new matrix [size];
for(int j=0; j<size; j++)
{
temp[j].x=new double [1];
soln[j].x=new double [1];
consts[j].x=new double [1];
}
for(int i=0; i<(2*size)-1; i++)
{
coeffs[i].x=new double [size];
inverse[i].x=new double [size];
}
cout << “Enter variable names: “;
for(int i=0; i<size; i++)
cin >> variables[i];
for(int i=0; i<size; i++)
{
cout << “Equation: ” << i+1 << endl;
for(int j=0; j<size; j++)
cin >> coeffs[i].x[j];
cout << endl << “Enter constant: “;
cin >> consts[i].x[0]; cout << endl;
}
find_adjoint(coeffs,inverse,size);
if(!find_val(coeffs,size))
{
multiply_matrices(inverse,consts,temp,size);
if(check_for_singularity(temp,size))
cout << “No solution!!!” << endl;
else
cout << “Infinite solutions!!!” << endl;
goto end;
}
multiply_matrix(inverse,size,1/find_val(coeffs,size));
multiply_matrices(inverse,consts,soln,size);
for(int i=0; i<size; i++)
cout << variables[i] << ” = ” << soln[i].x[0] << endl;
end:
delete []coeffs;
delete []variables;
delete []inverse;
delete []consts;
delete []soln;
delete []temp;
cin.get(); cin.get();
}
double find_val(matrix* x, int size)
{
double value=0;
if(size>2)
{
for(int i=0; i<size; i++)
{
matrix* y=new matrix [size-1];
for(int k=0; k<size-1; k++)
y[k].x=new double [size-1];
for(int j=1; j<size; j++)
{
for(int k=0, l=0; k<size; k++)
{
if(k!=i)
{
y[j-1].x[l]=x[j].x[k];
l++;
}
}
}
value+=(pow(-1,i)*find_val(y,size-1)*x[0].x[i]);
delete []y;
}
}
else if(size==2)
{
value=((x[0].x[0]*x[1].x[1])-(x[0].x[1]*x[1].x[0]));
}
return value;
}
void find_adjoint(matrix* x, matrix* y, int size)
{
matrix* z=new matrix [size-1];
for(int i=0; i<size; i++)
z[i].x=new double [size-1];
for(int a=0; a<size; a++)
{
for(int b=0; b<size; b++)
{
for(int i=0, j=0; i<size; i++)
{
if (i!=a)
{
for(int k=0, l=0; k<size; k++)
{
if(k!=b)
{
z[j].x[l]= x[i].x[k];
l++;
}
}
j++;
}
}
y[a].x[b]=(pow(-1,a+b)*find_val(z,size-1));
}
}
transpose(y,size);
}
void multiply_matrix(matrix* x, int size, double value)
{
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
x[i].x[j]*=value;
}
}
void transpose(matrix* x, int size)
{
for(int i=0; i<size-1; i++)
{
for(int j=i; j<size; j++)
{
float z;
z=x[i].x[j]; x[i].x[j]=x[j].x[i];
x[j].x[i]=z;
}
}
}
void multiply_matrices(matrix* x, matrix* y, matrix* z, int size)
{
for(int i=0; i<size; i++)
{
z[i].x[0]=0;
for(int j=0; j<size; j++)
z[i].x[0]+=(x[i].x[j]*y[j].x[0]);
}
}
int check_for_singularity(matrix* x, int size)
{
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
if(x[i].x[j])
return 0;
}
return 1;
}
Phew!.. That was one HUGE program. At least it works. I also made it recognise infinite and no solution situations. That’s one program finished.
Till next time,
Rapster!!!