matrix

its my project...any problems that you can find?

Page 1 of 1

2 Replies - 699 Views - Last Post: 31 December 2008 - 04:19 PM

#1 dan_ram  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 15-August 07

matrix

Posted 27 December 2008 - 07:31 AM

#include <iostream.h>
void do_again(),direction(int ans);
void det(){
	cout<<endl<<"TO FIND DETERMINANT"<<endl;
	int n = 0,val;
	cout<<"enter no. of columns/rows of square matrix"<<endl;
	cin>>n;
	int **p;
	p = new int*[n];
	for (int i = 0; i< n; i++)
	{
		p[i] = new int[n];
	}
	int a  = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			p[i][j] = a++;
		}
	}
	cout<<"enter the values of matrix row-wise"<<endl;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin>>p[i][j];
			if (n==1)
				val=p[i][j];
		}
	}
	if (n==2)
		val=p[0][0]*p[1][1]-p[0][1]*p[1][0];
	else if (n==3)
		val=(p[0][0]*((p[1][1]*p[2][2])-(p[1][2]*p[2][1])))-
			(p[0][1]*((p[1][0]*p[2][2])-(p[1][2]*p[2][0])))+
			(p[0][2]*((p[1][0]*p[2][1])-(p[1][1]*p[2][0])));
	cout<<"determinant value is:";
	cout<<val<<endl;
	system("PAUSE");
}
void transpose(){
	int n = 0,m=0;
	cout<<"TO FIND TRANSPOSE"<<endl;
	cout<<"enter no. of rows and columns of matrix"<<endl;
	cin>>m>>n;
	int **p;
	p = new int*[m];
	for (int i = 0; i< m; i++)
	{
		p[i] = new int[n];
	}
	int a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			p[i][j] = a++;
		}
	}
	cout<<"enter the numbers of matrix row-wise"<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin>>p[i][j];
		}
	}
	cout<<"matrix you have entered is"<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << p[i][j] << "\t";
		}
		cout << "\n";
	}
	int **q;
	q = new int*[m];
	cout<<endl;
	for (int i = 0; i< m; i++)
	{
		q[i] = new int[n];
	}
	a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			q[i][j] = a++;
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			q[i][j] = p[j][i];
		}
	}
	cout<<"transpose is"<<endl;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << q[i][j] << "\t";
		}
		cout << "\n";
	}
	for (int i = 0; i < m; i++)
	{
		delete((int*)q[i]);
	}
	delete [] q;
	system("PAUSE");
}
void add(){
	int n = 0,m=0;
	cout<<"TO FIND THE SUM"<<endl;
	cout<<"enter no. of rows and columns of matrix"<<endl;
	cin>>m>>n;
	int **t,**q;
	t = new int*[m];
	for (int i = 0; i< m; i++)
	{
		t[i] = new int[n];
	}
	int a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			t[i][j] = 0;
		}
	}
	char chk='y';
	while (chk=='y'||chk=='Y'){
		q = new int*[m];
		for (int i = 0; i< m; i++)
		{
			q[i] = new int[n];
		}
		a  = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				q[i][j] = a++;
			}
		}
		cout<<"enter the numbers of matrix to be added"<<endl;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin>>q[i][j];
			}
		}
		cout<<"matrix you have entered"<<endl;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cout << q[i][j] << "\t";
			}
			cout << "\n";
		}
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				t[i][j] = t[i][j]+q[i][j];
			}
		}
		for (int i = 0; i < m; i++)
		{
			delete((int*)q[i]);
		}
		delete [] q;
		cout<<"wish to add another matrix to this? y/n"<<endl;
		cin>>chk;
	}
	cout<<"sum is "<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << t[i][j] << "\t";
		}
		cout << "\n";
	}
	for (int i = 0; i < m; i++)
	{
		delete((int*)t[i]);
	}
	delete [] t;
	system("PAUSE");
}
void subtract(){
	cout<<"TO SUBTRACT"<<endl;
	int n = 0,m=0;
	cout<<"enter no. of rows and columns of matrix"<<endl;
	cin>>m>>n;
	int **t,**q;
	char chk;
	t = new int*[m];
	for (int i = 0; i< m; i++)
	{
		t[i] = new int[n];
	}
	int a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			t[i][j] = 0;
		}
	}
	cout<<"enter the numbers of matrix"<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin>>t[i][j];
		}
	}
	cout<<"matrix you have entered"<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << t[i][j] << "\t";
		}
		cout << "\n";
	}
	chk='y';
	while (chk=='y'||chk=='Y'){
		q = new int*[n];
		for (int i = 0; i< m; i++)
		{
			q[i] = new int[n];
		}
		a  = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				q[i][j] = a++;
			}
		}
		cout<<"enter the numbers of matrix to be subtracted"<<endl;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin>>q[i][j];
			}
		}
		cout<<"matrix you have entered"<<endl;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cout << q[i][j] << "\t";
			}
			cout << "\n";
		}
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				t[i][j] = t[i][j]-q[i][j];
			}
		}
		for (int i = 0; i < m; i++)
		{
			delete((int*)q[i]);
		}
		delete [] q;
		cout<<"wish to subtract another matrix from the result? y/n"<<endl;
		cin>>chk;
	}
	cout<<"difference is "<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << t[i][j] << "\t";
		}
		cout << "\n";
	}
	for (int i = 0; i < n; i++)
	{
		delete((int*)t[i]);
	}
	delete [] t;
	system("PAUSE");
}
void multiply(){
	int n = 0,m=0,l=0;
	cout<<"TO FIND THE PRODUCT"<<endl;
	cout<<"enter no. of rows and columns of matrix"<<endl;
	cin>>m>>n;
	int **t,**q;
	t = new int*[m];
	for (int i = 0; i< m; i++)
	{
		t[i] = new int[n];
	}
	int a  = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			t[i][j] = 0;
		}
	}
	cout<<"enter the numbers of matrix "<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin>>t[i][j];
		}
	}
	cout<<"matrix you have entered"<<endl;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << t[i][j] << "\t";
		}
		cout << "\n";
	}
	cout<<"enter no. of columns of 2nd matrix"<<endl;
	cin>>l;
	q = new int*[n];
	for (int i = 0; i< n; i++)
	{
		q[i] = new int[l];
	}
	a  = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < l; j++)
		{
			q[i][j] = a++;
		}
	}
	cout<<"enter the numbers of matrix to be multiplied"<<endl;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < l; j++)
		{
			cin>>q[i][j];
		}
	}
	cout<<"matrix you have entered"<<endl;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < l; j++)
		{
			cout << q[i][j] << "\t";
		}
		cout << "\n";
	}
	cout<<"product is"<<endl;
	int k=0;
	for (int i=0;i<m;i++)
	{
		for (int j=0;j<l;j++)
		{
			for (int r=1;r<=n;r++)
			{
				k=k+(t[i][r-1]*q[r-1][j]);
			}
			cout<<k<<"\t";
			k=0;
		}
		cout<<endl;
	}
	system("PAUSE");
}
void inverse3(){
	int  p[3][3];// the matrix that is entered by user
	int q[3][3];//the transpose of a matrix p
	int C[3][3];//the adjunct matrix of transpose of a matrix p not adjunct of p
	double X[3][3];//the inverse
	int i,j;
	float x,n=0;
	cout<<"enter matrix"<<endl;
	for (i=0;i<3;i++)
	{
		for (j=0;j<3;j++)
		{
			cin>>p[i][j];
			q[i][j]=0;
			C[i][j]=0;
		}
	}
	for (i=0,j=0;j<3;j++)
	{
		if (j==2)
			n+=p[i][j]*p[i+1][0]*p[i+2][1];
		else if (j==1)
			n+=p[i][j]*p[i+1][j+1]*p[i+2][0];
		else
			n+=p[i][j]*p[i+1][j+1]*p[i+2][j+2];
	}
	for (i=2,j=0;j<3;j++)
	{
		if (j==2)
			n-=p[i][j]*p[i-1][0]*p[i-2][1];
		else if (j==1)
			n-=p[i][j]*p[i-1][j+1]*p[i-2][0];
		else
			n-=p[i][j]*p[i-1][j+1]*p[i-2][j+2];
	}
	cout<<"matrix is"<<endl;
	for (i=0;i<3;i++)
	{
		cout<<"\n";
		for (j=0;j<3;j++)
		{
			cout<<p[i][j]<<"\t";
		}
	}
	if (n!=0) x=1.0/n;
	else
	{
		cout<<"Division by 0, not good!\n";
		do_again();
	}
	for (i=0;i<3;i++)
	{
		for (j=0;j<3;j++)
		{
			q[i][j]=p[j][i];
		}
	}
	C[0][0]=q[1][1]*q[2][2]-(q[2][1]*q[1][2]);
	C[0][1]=(-1)*(q[1][0]*q[2][2]-(q[2][0]*q[1][2]));
	C[0][2]=q[1][0]*q[2][1]-(q[2][0]*q[1][1]);
	C[1][0]=(-1)*(q[0][1]*q[2][2]-q[2][1]*q[0][2]);
	C[1][1]=q[0][0]*q[2][2]-q[2][0]*q[0][2];
	C[1][2]=(-1)*(q[0][0]*q[2][1]-q[2][0]*q[0][1]);
	C[2][0]=q[0][1]*q[1][2]-q[1][1]*q[0][2];
	C[2][1]=(-1)*(q[0][0]*q[1][2]-q[1][0]*q[0][2]);
	C[2][2]=q[0][0]*q[1][1]-q[1][0]*q[0][1];
	for (i=0;i<3;i++)
	{
		for (j=0;j<3;j++)
		{
			X[i][j]=C[i][j]*x;
		}
	}
	cout<<endl;
	cout<<" The inverse matrix of the matrix \n";
	for (i=0;i<3;i++)
	{
		for (j=0;j<3;j++)
		{
			cout<<X[i][j]<<"\t";
		}
		cout<<endl;
	}
	cout<<"\n\n";
	system("pause");
}
void inverse2(){
	int p[2][2];
	float q[2][2];
	int d,k;
	cout<<"enter the matrix"<<endl;
	for (int i=0;i<2;i++)
	{
		for (int j=0;j<2;j++)
		{
			cin>>p[i][j];
		}
	}
	cout<<"matrix is"<<endl;
	for(int i=0;i<2;i++)
	{
	   for(int j=0;j<2;j++)
	   {
		  cout<<p[i][j]<<"\t";
	   }
	   cout<<endl;
	}
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			q[i][j] = p[j][i];
		}
	}
	k=q[0][0];
	q[0][0]=q[1][1];
	q[1][1]=k;
	k=q[0][1];
	q[0][1]=q[1][0];
	q[1][0]=k;
	q[0][1]=(-1)*q[0][1];
	q[1][0]=(-1)*q[1][0];
	d=q[0][0]*q[1][1]-q[1][0]*q[0][1];
	cout<<"inverse of matrix is"<<endl;
	for (int i=0;i<2;i++)
	{
		for (int j=0;j<2;j++)
		{
			  q[i][j]=q[i][j]/d;
			  cout<<q[i][j]<<"\t"; 
		}
		cout<<endl;
	}
	system("pause");
}
int main()
{
	do_again();
	return 0;
}
void do_again()
{
	int ans;
	do
	{
		cout<<endl;
		cout << "What would you like to do?\n\n";
		cout <<"1: find transpose\n\n";
		cout << "2: find determinant(only for 2x2 and 3x3)\n\n";
		cout << "3: add matrices\n\n";
		cout << "4: subtract matrices\n\n";
		cout << "5: multiply 2 matrices\n\n";
		cout<<"6: find inverse(only for 2x2 and 3x3)\n\n";
		cout<<"7: about\n\n";
		cout<<"8: exit\n\n";
		cin >>ans;
		cin.ignore(80,'\n');
		direction(ans);
	}while (ans!=8);
}
void direction(int ans)
{
	switch (ans)
	{
	case 1:
		transpose();
		break;
	case 2:
		det();
		break;
	case 3:
		add();
		break;
	case 4:
		subtract();
		break;
	case 5:
		multiply();
		break;
	case 6:
	{
		int n;
		cout<<"enter no. of columns/rows of square matrix"<<endl;
		cin>>n;
		if (n==3)
			inverse3();
		if (n==2)
			inverse2();
		else {
			cout<<"sorry! cannot be found"<<endl;
			do_again();
		}
	}
	break;
	case 7:
	{
		for (int i=0;i<25;i++) cout<<endl;
		cout
		<<"			 This is a FREEWARE intended to help students(& others)"<<endl
		<<"	 solve various problems like matrix addition,subtraction,multiplication"<<endl
		<<"		  and also to find the transpose and determinant of a matrix."<<endl
		<<"	there are no limitations for the size of the matrix except in the case of"<<endl
		<<"finding the determinant and inverse where only 2x2 and 3x3 matrices can be used."<<endl<<endl
		<<"this program is meant only to be a helping tool using which answers can be"
		<<"					  verified and SHOULD NOT BE MISUSED"<<endl<<endl;
		cout<<endl<<endl<<endl<<endl<<endl;
		system("pause");
	}
	break;
	case 8:
		exit(0);
		break;
	default:
		cout << "Please read and enter a valid value\n";
		do_again();
		break;
	}
}

 


Is This A Good Question/Topic? 0
  • +

Replies To: matrix

#2 dan_ram  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 15-August 07

Re: matrix

Posted 31 December 2008 - 05:01 AM

why no reply?? do i have to upload the applocation???
Was This Post Helpful? 0
  • +
  • -

#3 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: matrix

Posted 31 December 2008 - 04:19 PM

I think this would be more useful if it was written as a library. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1