Mar 19, 2007

C++ Program to input/output a matrices

this program will take input for two 3x3 matrices m and n and then print them in matrix format and then it will add two of the matrix and store the result in third matrix (o).
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int m[3][3]; // matrix delcaration
int n[3][3];
int o[3][3];

for (int i=0;i<=2;i++) // first matrix input
{
for (int j=0;j<=2;j++)
{
cout <<"enter any number";
cin >> m[i][j];
}
}

for (i=0;i<=2;i++) // first matrix output
{
for (int j=0;j<=2;j++)
{
cout << m[i][j]<<" ";
}
cout <<endl;
} // second matrix input

for (i=0;i<=2;i++)
{
for (int j=0;j<=2;j++)
{
cout <<"enter any number";
cin >> n[i][j];
}
}

for (i=0;i<=2;i++) // second matrix output
{
for (int j=0;j<=2;j++)
{
cout << n[i][j]<<" ";
}
cout <<endl;
}

for (i=0;i<=2;i++) // adding both of the matrix
{
for (int j=0;j<=2;j++)
{
o[i][j] = m[i][j] + n[i][j];
}
cout <<endl;
}

for (i=0;i<=2;i++) // outputing the answer
{
for (int j=0;j<=2;j++)
{
cout << o[i][j] << " ";
}
cout <<endl;
}

getch();
}

No comments:

Post a Comment

Infolinks In Text Ads

Total Pageviews

Powered by Blogger.

Dont Forget To Follow Us

Blog Archive