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 !

 

 

Aralık 26, 2010 Posted by | C/C++ programming | , , , , , | Yorum bırakın

Pcb Design


Hi today ,

 

I will not share myknowledge and notes todaybut some useful links for PCB design . We all design  circuits and sometimes we want to make them live .

So we use PCB designers like multisim , proteus for this purpose .

But there are important steps while designing a PCB shematic .

We must learn these steps carefully to draw good PCBs .

First site is just for learning PCB design . They cover all of the details which are so important in daily life .

http://www.pcbdesign.org/pcb-design-guide/

 

They also share PCB softwares and Design services to users .

 

The other site is sparkfun . Sparkfun is a good electronics site , so they have some useful tricks for PCB design . They tell it with photos examples .

http://www.sparkfun.com/tutorials/115

 

The last site is fivemanconspiracy . The title is “Pcb basics and gEDA/Pcb tips and tricks “

http://www.fivemanconspiracy.com/node/14

 

I will also share useful Youtube videos for this purpose , See you !

 

 

Layout Process part-1

Layout Process part-2

 

 

Aralık 1, 2010 Posted by | Electronics | , , | 1 Yorum