Berbagi ilmu lebih baik daripada hanya sekedar kaya ilmu

ini contoh array 2 dimensi yang salah satu implementasinya adalah MATRIX
//* ———————————————————————————- *
//* Program Penjumlahan MATRIX *
//* User : Dulna2001 (dulna2001@gmail.com) *
//* ———————————————————————————- *

# include “iostream.h”
# include “conio.h”
# define Nmaks 10 // Deklarasi maksimum baris dan kolom Matrix adalah 10

main()
{
int kola,kolb,bara,barb,i,j,N,M;
typedef int matrix[Nmaks][Nmaks];
matrix A,B,C;

do
{
clrscr();
cout<<“Program penjumlahan 2 Matrix (jumlah baris dan kolom harus sama antar Matrix)”<<endl;
do
{
cout<<“Masukkan jumlah baris Matrix A : “;cin>>bara;
cout<<“Masukkan jumlah kolom Matrix A : “;cin>>kola;
}
while((bara>10)||(kola>10)); //Perulangan jika data yg di-input > 10
do
{
cout<<“Masukkan jumlah baris Matrix B : “;cin>>barb;
cout<<“Masukkan jumlah kolom Matrix B : “;cin>>kolb;
}
while((barb>10)||(kolb>10)); //Perulangan jika data yg di-input > 10
cout<<endl;
}
while((bara!=barb)||(kola!=kolb));

clrscr();
cout<<“Masukkan nilai-nilai Matrix A”<<endl;
for (i=1; i<=bara; i++)
{
for (j=1; j<=kola; j++)
{
cout << “A(” << i << “,” << j << “) = “;
cin >> A[i][j];
}
}

clrscr();
cout<<“Masukkan nilai-nilai Matrix B”<<endl;
for (i=1; i<=barb; i++)
{
for (j=1; j<=kolb; j++)
{
cout << “B(” << i << “,” << j << “) = “;
cin >> B[i][j];
}
}

clrscr();
((N=bara)||(N=barb));
((M=kola)||(M=kolb));
for (i=1; i<=N; i++)
{
for (j=1; j<=M; j++)
{
C[i][j] = A[i][j]+B[i][j];
}
}

clrscr();
cout << “Matriks A = \n”;
for (i=1; i<=bara; i++)
{
for (j=1; j<=kola; j++)
{
cout << “\t” << A[i][j];
}
cout << endl;
}
cout<<endl;
cout << “Matriks B = \n”;
for (i=1; i<=barb; i++)
{
for (j=1; j<=kolb; j++)
{
cout << “\t” << B[i][j];
}
cout << endl;
}
cout<<endl;
cout << “Matriks C (A + B) = \n”;
for (i=1; i<=N; i++)
{
for (j=1; j<=M; j++)
{
cout << “\t” << C[i][j];
}
cout << endl;
}

}
//Home work_Algoritma_Revisi(2)
//Date : 24 December 2007
//E-mail : dulna2001@gmail.com

About these ads