#include <iostream>
using namespace std;
int main(){
int x,y;
//menentukan ordo matrixnya
cout<<"masukkan baris ordo matrix : ";
cin>>x;
cout<<"masukkan kolom ordo matrix : ";
cin>>y;
cout<<endl;
cout<<"ordo matrix = "<<x<<" x "<<y<<endl;
//deklarasi 2
int matrix1[x][y];
int matrix2[x][y];
int i,j;
//input matrix pertama
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<"masukkan matrix pertama baris ke-"<<i+1<<" kolom ke-"<<j+1<<" : ";
cin>>matrix1[i][j];
}
}
cout<<endl;
//input matrix pertama
cout<<"matrix pertama : "<<endl;
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<matrix1[i][j]<<" ";
}
cout<<"\n";
}
cout<<endl;
//input matrix ke 2
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<"masukkan matrix kedua baris ke-"<<i+1<<" kolom ke-"<<j+1<<" : ";
cin>>matrix2[i][j];
}
}
cout<<endl;
//output matrix ke 2
cout<<"matrix kedua : "<<endl;
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<matrix2[i][j]<<" ";
}
cout<<"\n";
}
cout<<endl;
//output penjumlahan
cout<<"Jumlah matrix pertama + matrix ke 2 : "<<endl;
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<matrix1[i][j]+matrix2[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
dan hasil yang akan keluar seperti ini :
using namespace std;
int main(){
int x,y;
//menentukan ordo matrixnya
cout<<"masukkan baris ordo matrix : ";
cin>>x;
cout<<"masukkan kolom ordo matrix : ";
cin>>y;
cout<<endl;
cout<<"ordo matrix = "<<x<<" x "<<y<<endl;
//deklarasi 2
int matrix1[x][y];
int matrix2[x][y];
int i,j;
//input matrix pertama
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<"masukkan matrix pertama baris ke-"<<i+1<<" kolom ke-"<<j+1<<" : ";
cin>>matrix1[i][j];
}
}
cout<<endl;
//input matrix pertama
cout<<"matrix pertama : "<<endl;
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<matrix1[i][j]<<" ";
}
cout<<"\n";
}
cout<<endl;
//input matrix ke 2
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<"masukkan matrix kedua baris ke-"<<i+1<<" kolom ke-"<<j+1<<" : ";
cin>>matrix2[i][j];
}
}
cout<<endl;
//output matrix ke 2
cout<<"matrix kedua : "<<endl;
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<matrix2[i][j]<<" ";
}
cout<<"\n";
}
cout<<endl;
//output penjumlahan
cout<<"Jumlah matrix pertama + matrix ke 2 : "<<endl;
for(i=0;i<x;i++){
for(j=0;j<y;j++){
cout<<matrix1[i][j]+matrix2[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
dan hasil yang akan keluar seperti ini :