C++ Program To Add Two Matrices Using Multi-dimensional Arrays
#include<iostream>#include<cstdlib>
using namespace std;
int main() {
int A[10][10],B[10][10],c[10][10];
int i,j,m,n,p,q;
cout << "Number Of Rows & Columns Of Matrix A: " << endl;
cin >> m >> n;
cout << "\nNumber Of Rows & Columns Of Matrix B: " << endl;
cin >> p >> q;
if(m==p&&n==q)
cout << "\n\nMatrices Can Be Added.";
else {
cout << "\nMatrices can Not Be Added Because They're Not The Same Size..\n";
exit(0);
}
cout<<"\nEnter Matrix A: " << endl;
for(i=0;i<m;i++) {
for(j=0;j<n;j++)
cin >> A[i][j];
}
cout<<"\nMatrix A:\n";
for(j=0;j<n;j++)
cin >> A[i][j];
}
cout<<"\nMatrix A:\n";
for(i=0;i<m;i++) {
for(j=0;j<n;j++)
cout << A[i][j] <<" ";
cout << "\n";
}
cout << "\nEnter Matrix B: " << endl;
for(i=0;i<m;i++) {
for(j=0;j<n;j++)
cin >> B[i][j];
}
cout << "\nMatrix B: \n";
for(i=0;i<m;i++) {
for(j=0;j<n;j++)
cout << B[i][j] << " ";
cout << "\n";
}
for(i=0;i<m;i++) {
for(j=0;j<n;j++)
c[i][j]=A[i][j]+B[i][j];
}
cout << "\nSum Of Matrices A and B: \n";
for(i=0;i<m;i++) {
for(j=0;j<n;j++)
cout << c[i][j] << " ";
cout << "\n";
}
return 0;
}
C++ Program To Find The Transpose Of A Matrix
#include <iostream>using namespace std;
int main() {
int a[10][10], trans[10][10], r, c, i, j;
cout << "Enter The No. Rows & Columns Of Matrix: " << endl;
cin >> r >> c;
cout << endl << "Enter The Elements Of Matrix: " << endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j) {
cout << "Enter The Element a" << "[" << i+1 << j+1 << "]" << " : ";
cin >> a[i][j];
}
cout << endl << "Entered Matrix: " << endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j) {
cout << " " << a[i][j];
for(j=0; j<c; ++j) {
cout << " " << a[i][j];
if(j==c-1)
cout << endl << endl;
}
cout << endl << endl;
}
for(i=0; i<r; ++i)
for(j=0; j<c; ++j) {
trans[j][i]=a[i][j];
}
cout << endl << "Transpose Of Matrix: " << endl;
trans[j][i]=a[i][j];
}
cout << endl << "Transpose Of Matrix: " << endl;
for(i=0; i<c; ++i)
for(j=0; j<r; ++j) {
cout << " " << trans[i][j];
for(j=0; j<r; ++j) {
cout << " " << trans[i][j];
if(j==r-1)
cout << endl << endl;
}
return 0;
}
cout << endl << endl;
}
return 0;
}