Swordrock's Blog

Just another WordPress.com site

C++ matrix operations


Hi!

 

Today i will share some arithmetic operations with C++ like addition , multiplication , transpose and inverse

 

1)ADDITION

this program takes a 3×3 matrix as an input and gives the total value (2xN) .

 

—————————————————

 

#include <iostream>
using namespace std ;

int main ()
{
int i,j,b[3][3]={0},n[3][3]={0}; // definition of variables

cout<<“enter the values for 3×3 matrix : “<<endl;
// cout<<“entering to for …”<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++) {

cout<<“(“<<i<<“,”<<j<<“)=”;
cin>>n[i][j];
cout<<endl;

b[i][j]=2*n[i][j];

}
}

////////////////////////////////////////////////////////////////////////////////////
//addition printing
cout<<“Sum : “<<endl;
for(i=0;i<3;i++){
cout<<endl;
for(j=0;j<3;j++) {
if (i==2 && j==2) cout<<‘\t’;

cout<<“(“<<i<<“,”<<j<<“)=”<<b[i][j]<<‘\t'<<‘\t’;

}
}

//end of addition printing

////////////////////////////////////////////////////////////////////////////////////
cout<<endl;

system (“pause”);
}

 

—————————————————

 

2)MULTIPLICATION

In this program i take values of a 3×3 matirx and take square of it

—————————————————

 

#include <iostream>
using namespace std ;

int main ()
{

int i,j,c[3][3]={0},a[3][3]={0},n[3][3]={0};   // defition of variables

cout<<“enter values for 3×3 matrix : “<<endl;
// cout<<“entering to for …”<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++) {

cout<<“(“<<i<<“,”<<j<<“)=”;
cin>>n[i][j];
cout<<endl;

c[i][j]=n[i][j]*n[i][j];
}

}

////////////////////////////////////////////////////////////////////////////////////
//multiplication display

cout<<“carpim : “<<endl;
for(i=0;i<3;i++){
cout<<endl; cout<<‘\t’;
for(j=0;j<3;j++) {
if(i==2 && j==2) cout<<‘\t’;
cout<<“(“<<i<<“,”<<j<<“)=”<<c[i][j]<<‘\t’;

}
}

////////////////////////////////////////////////////////////////////////////////////

cout<<endl;
system (“pause”);
}

 

—————————————————

 

 

3)TRANSPOSE

In this program , i take values of a 3×3 matrix and display the transpose of it ,

(if you dont know about transpoze matrix :http://en.wikipedia.org/wiki/Transpose )

 

—————————————————

 

#include <iostream>
using namespace std ;

int main ()
{

int i,j,a[3][3]={0},n[3][3]={0};

cout<<“enter values for 3×3 matrix : “<<endl;
// cout<<“entering to for …”<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++) {

cout<<“(“<<i<<“,”<<j<<“)=”;
cin>>n[i][j];
cout<<endl;
a[j][i]=n[i][j]; // calculating the transpose

}

}

 

////////////////////////////////////////////////////////////////////////////////////
//transpose printing
cout<<“tranpose : “<<endl;
for(i=0;i<3;i++){
cout<<endl;
for(j=0;j<3;j++) {

cout<<“(“<<i<<“,”<<j<<“)=”<<a[i][j]<<‘\t’;

}
}

////////////////////////////////////////////////////////////////////////////////////

system (“pause”);
}

 

—————————————————

 

4)INVERSE

 

In this program  , i calculate the inverse of a 3×3 matrix , i use determinant method , you can also find inverse programs done by gauss-jordan method

—————————————————

 

#include <iostream>
using namespace std ;

int main ()
{

cout<<“enter values for 3×3 atrix : “<<endl;
// cout<<“entering to for …”<<endl;

int z,j,i,n[3][3];
double a[3][3];

for(i=0;i<3;i++){
cout<<endl;
for(j=0;j<3;j++) {

cout<<“(“<<i<<“,”<<j<<“)=”;
cin>>n[i][j];
cout<<endl;

}

}

// adjoint matrix calculation

a[0][0]=n[1][1]*n[2][2]-n[1][2]*n[2][1];
a[0][1]=n[1][2]*n[2][0]-n[1][0]*n[2][2];
a[0][2]=n[1][0]*n[2][1]-n[1][1]*n[2][0];
a[1][0]=n[0][2]*n[2][1]-n[0][1]*n[2][2];
a[1][1]=n[0][0]*n[2][2]-n[0][2]*n[2][0];
a[1][2]=n[0][1]*n[2][0]-n[0][0]*n[2][1];
a[2][0]=n[0][1]*n[1][2]-n[0][2]*n[1][1];
a[2][1]=n[0][2]*n[1][0]-n[0][0]*n[1][2];
a[2][2]=n[0][0]*n[1][1]-n[0][1]*n[1][0];

z=n[0][0]*a[0][0] + n[0][1]*a[0][1] + n[0][2]*a[0][2];

////////////////////////////////////////////////////////////////////////////////////

// inverse  printing

cout<<“determinant : “<<z<<endl;
cout<<“inverse of matrix :  : “<<endl;
for(i=0;i<3;i++){
cout<<endl; cout<<‘\t’;
for(j=0;j<3;j++) {
if(i==2 && j==2) cout<<‘\t’;
cout<<“(“<<i<<“,”<<j<<“)=”<<a[j][i]/z<<‘\t’;

}
}

cout<<endl;
////////////////////////////////////////////////////////////////////////////////////

system (“pause”);

}

 

—————————————————

 

 

We did some arithmetic calculations with C++ today , see you !

 

 

Reklamlar

Aralık 26, 2010 - Posted by | C/C++ programming | , , , , ,

Henüz yorum yapılmamış.

Bir Cevap Yazın

Aşağıya bilgilerinizi girin veya oturum açmak için bir simgeye tıklayın:

WordPress.com Logosu

WordPress.com hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap / Değiştir )

Connecting to %s

%d blogcu bunu beğendi: