sumber gambar: sdtimes.com
Untuk lebih lanjutnya berikut ini syntax programnya:
#include <iostream> using namespace std; void masukdata(int matrix1[][10], int matrix2[][10], int baris1, int kolom1, int baris2, int kolom2); void gabmatrix(int matrix1[][10], int matrix2[][10], int gab[][10], int baris1, int kolom1, int baris2, int kolom2); void tampil(int dua[][10], int baris1, int kolom2); int main() { int matrix1[10][10], matrix2[10][10], dua[10][10], baris1, kolom1, baris2, kolom2, i, j, k; cout << "Masukkan baris dan kolom matrix pertama: "<<endl; cout << "baris :";cin >> baris1; cout << "kolom :";cin>> kolom1; cout << "Masukkan baris dan kolom matrix kedua: "<<endl; cout << "baris :";cin >> baris2; cout << "kolom :";cin >> kolom2; while (kolom1 != baris2) { cout << "Kesalahan! kolom matriks pertama tidak sama dengan baris matriks kedua." << endl; cout << "Masukkan baris dan kolom matrix pertama: "<<endl; cout << "baris :";cin >> baris1; cout << "kolom :";cin >> kolom1; cout << "Masukkan baris dan kolom matrix kedua: "<<endl; cout << "baris :";cin >> baris2; cout << "kolom :";cin >> kolom2; } masukdata(matrix1, matrix2, baris1, kolom1, baris2, kolom2); gabmatrix(matrix1, matrix2, dua, baris1, kolom1, baris2, kolom2); tampil(dua, baris1, kolom2); return 0; } void masukdata(int matrix1[][10], int matrix2[][10], int baris1, int kolom1, int baris2, int kolom2) { int i, j; cout << endl << "Enter elements of matrix 1:" << endl; for(i = 0; i < baris1; ++i) { for(j = 0; j < kolom1; ++j) { cout << "Enter elements a"<< i + 1 << j + 1 << ": "; cin >> matrix1[i][j]; } } cout << endl << "Enter elements of matrix 2:" << endl; for(i = 0; i < baris2; ++i) { for(j = 0; j < kolom2; ++j) { cout << "Enter elements b" << i + 1 << j + 1 << ": "; cin >> matrix2[i][j]; } } } void gabmatrix(int matrix1[][10], int matrix2[][10], int dua[][10], int baris1, int kolom1, int baris2, int kolom2) { int i, j, k; for(i = 0; i < baris1; ++i) { for(j = 0; j < kolom2; ++j) { dua[i][j] = 0; } } for(i = 0; i < baris1; ++i) { for(j = 0; j < kolom2; ++j) { for(k=0; k<kolom1; ++k) { dua[i][j] += matrix1[i][k] * matrix2[k][j]; } } } } void tampil(int dua[][10], int baris1, int kolom2) { int i, j; cout << "Output Matrix:" << endl; for(i = 0; i < baris1; ++i) { for(j = 0; j < kolom2; ++j) { cout << dua[i][j] << " "; if(j == kolom2 - 1) cout << endl << endl; } } }
Sumber Pustaka : programiz.com
0 komentar: