Sunday, July 12, 2009

How to write a c++ code to solve any matrix problem?

say you have 3 by 3 or 4 by 4 matrix. how do i write a c++ code to solve this matrix problem and also will be able to the program being able to compute any matrix problem.

How to write a c++ code to solve any matrix problem?
/* example program to add two matrices %26amp; store the results in the 3rd matrix */


#include%26lt; iostream.h %26gt;


#include%26lt; math.h %26gt;


void main()


{


int a[10][10],b[10][10],c[10][10],i,j,m,n,p,...


clrscr();


cout%26lt;%26lt;“enter the order of the matrix”%26lt;%26lt;\n;


cin%26gt;%26gt;p%26gt;%26gt;q;


if(m==p %26amp;%26amp; n==q)


{


cout%26lt;%26lt;”matrix can be added\n”;


cout%26lt;%26lt;”enter the elements of the matrix a”;


for(i=0;i %26lt; m;i++)


for(j=0;j %26lt; n;j++)


cin%26gt;%26gt;a[i][j];


cout%26lt;%26lt;“enter the elements of the matrix b”;


for(i=0;i %26lt; p;i++)


for(j=0;j %26lt; q;j++)


cin%26gt;%26gt;b[i][j];


cout%26lt;%26lt;“the sum of the matrix a and b is”;


for(i=0;i %26lt; m;i++)


for(j=0;j %26lt; n;j++)


c[i][j]=a[i][j]+b[i][j];


for(i=0;i %26lt; m;i++)


{


for(j=0;j %26lt; n;j++)


cout%26lt;%26lt;a[i][j]%26lt;%26lt;\n;





}


}





this is how u perform arithmetic operations on matrices.
Reply:There is no standard implementation of a matrix in C++ so you need to create your own implementation. If you want to write a generic code then I would suggest to use an implementation using dynamic allocation with variable matrix size. All the operations (multiplication, addition, inversion etc.) you need to implement by yourself.





I did once find someone's implementation of a matrix class written for visual C++. I had adapted it for my requirements for academic projects while using gcc.

bloom

No comments:

Post a Comment