Monday, August 28, 2006

CPL TW5

TW#5

#include
#include
#include
#include
class matrix
{
private:
int a[5][5],b[5][5],c[5][5];
int i,j,k,m,n,p,q;
public:
void getdata()
{

cout<<"Enter max. rows in matrix a:";
cin>>m;
cout<<"Enter max. columns in matrix a:";
cin>>n;
cout<<"Enter max. rows in matrix b:";
cin>>p;
cout<<"Enter max. columns in matrix b";
cin>>q;
if(n!=p)
{
cout<<"Multiplication not possible";
getch();
exit(0);
}
else
{
cout<<"\nMatrix A:";
i=0,j=0;
for(i=0;i for(j=0;j {
cout<<"enter element "< cin>>a[i][j];
}
cout<<"\nMatrix B:";
i=0,j=0;
for(i=0;i for(j=0;j {
cout<<"enter element "< cin>>b[i][j];
}
}


}
void cal()
{
for(i=0;i<5;i++)
for(j=0;j<5;j++)
c[i][j]=0;

for(i=0;i for(j=0;j for(k=0;k c[i][j]=c[i][j]+a[i][k]*b[k][j];
}

void display()
{
cout<<"\nMatrix A:" << endl;
for(i=0;i {
for(j=0;j {

cout< }
cout << endl;
}
cout<<"\nMatrix B:"<< endl;
for(i=0;i {
for(j=0;j {
cout< }
cout << endl;
}
cout<<"\nResultant Matrix is:"<< endl;
for(i=0;i {
for(j=0;j {
cout< }
cout << endl;
}
}

};
void main()
{
clrscr();
matrix m1;
ofstream f1("c:\\matrix.txt",ios::out);
if(!f1)
{
cout<<"Cannot open the file";
getch();
}
m1.getdata();
m1.cal();
f1.write((char *) & m1,sizeof(m1));
cout<<"\nFile Created";
f1.close();

ifstream f2("c:\\matrix.txt");
if(!f2)
{
cout<<"Cannot open file";
getch();
}
f2.read((char *) & m1,sizeof(m1));
m1.display();
f2.close();

getch();

}
/*
Enter max. rows in matrix a:3
Enter max. columns in matrix a:3
Enter max. rows in matrix b:3
Enter max. columns in matrix b3

Matrix A:enter element 1,1?1
enter element 1,2?0
enter element 1,3?0
enter element 2,1?0
enter element 2,2?1
enter element 2,3?0
enter element 3,1?0
enter element 3,2?0
enter element 3,3?1

Matrix B:enter element 1,1?1
enter element 1,2?2
enter element 1,3?3
enter element 2,1?4
enter element 2,2?5
enter element 2,3?6
enter element 3,1?7
enter element 3,2?8
enter element 3,3?9

File Created
Matrix A:
1 0 0
0 1 0
0 0 1

Matrix B:
1 2 3
4 5 6
7 8 9

Resultant Matrix is:
1 2 3
4 5 6
7 8 9
*/

No comments: