Buatlah program untuk melakukan operasi matematika pada matriks (perkalian, penjumlahan, transpose) dengan alokasi memory!
#include <iostream>
#include <conio.h>
#include <math.h>
#include <limits>
#include <cstdlib>
#include <cstring>
using namespace std;
int **A,**B,**C;
int ib, ik, row1, row2, col1, col2;
char pil;
int kata;
char val [100];
int validasi()
{
do
{
kata=0;
cin>>val;
for(int i=0; i<strlen(val); i++)
{
if(isdigit(val[i])==0)
kata++;
}
if(kata !=0)
cout<<"Input salah !\n"<<endl;
}
while(kata!=0);
return 0;
}
int perkalian_matriks();
int penjumlahan_matriks();
int pengurangan_matriks();
int transpose_matriks();
int inputan();
int tampilkan_matriks();
int main() {
awal:
cout << "\t\t PROGRAM PENJUMLAHAN PENGURANGAN PERKALIAN " << endl;
cout << "\t DAN TRANSPOSE MATRIKS " << endl;
cout << endl << endl;
cout << "MENU PROGRAM" << endl;
cout << "1. Perkalian Matriks" << endl;
cout << "2. Penjumlahan Matriks" << endl;
cout << "3. Transpose Matriks" << endl;
cout << "4. Keluar" << endl;
cout << "\n\tInput Pilihan Anda : ";
validasi(); pil=atoi(val);
cout <<"==================================================================="<< endl;
switch (pil){
case 1 : perkalian_matriks();
break;
case 2 : penjumlahan_matriks();
break;
case 3 : transpose_matriks();
break;
case 4 :{
cout<<"Bye ";
return 0;
}
break;
default : cout <<"Input tidak ada dalam pilihan !" << endl;
}
getch();
int pilihan;
cout << "\nApakah anda ingin mencoba lagi? jika ya tekan <1>, tidak tekan <2>." << endl;
cout << "Pilihan anda : ";
validasi(); pilihan=atoi(val);
cout << endl;
if(pilihan==1) {
system("CLS");
goto awal;
}
else{
cout << "\nTerima Kasih Telah Menggunakan Program ini." << endl;
}
}
int inputan(){
cout << "\nInput jumlah baris Matriks A : ";
validasi(); row1=atoi(val);
cout << "Input jumlah kolom Matriks A : ";
validasi(); col1=atoi(val);
cout << "\nInput jumlah baris Matriks B : ";
validasi(); row2=atoi(val);
cout << "Input jumlah kolom Matriks B : ";
validasi(); col2=atoi(val);
cout << endl;
}
int tampilkan_matriks(){
cout << "\nMatriks A"<< endl;
A=(int**) malloc(row1*sizeof(int*));
for (ib=0; ib<row1; ib++){
A[ib]=(int*)malloc(col1*sizeof(int));
for (ik=0; ik<col1; ik++){
cout << "Input baris ke "<< (ib+1) <<" kolom ke " << (ik+1) << " : ";
validasi(); A[ib][ik]=atoi(val);
}
}
cout << "\nMatriks B"<< endl;
B=(int**) malloc(row2*sizeof(int*));
for (ib=0; ib<row2; ib++){
B[ib]=(int*)malloc(col2*sizeof(int));
for (ik=0; ik<col2; ik++){
cout << "Input baris ke "<< (ib+1) <<" kolom ke " << (ik+1) << " : ";
validasi(); B[ib][ik]=atoi(val);
}
}
//alokasi memori untuk matriks C
C=(int**) malloc(row1*sizeof(int*));
for(int i=0;i<row1;i++)
C[i]=(int*)malloc(col2*sizeof(int));
cout <<"\nMatriks A" << endl;
for (ib=0; ib<row1; ib++){
for(ik=0; ik<col1; ik++){
cout << A[ib][ik] << " ";
}
cout << endl;
}
cout << "\nMatriks B" << endl;
for (ib=0; ib<row2; ib++){
for(ik=0; ik<col2; ik++){
cout << B[ib][ik] << " ";
}
cout << endl;
}
cout << "==================================================================="<< endl;
}
int perkalian_matriks() {
cout<<"Jumlah Colom Matriks A harus sama dengan Baris Matriks B!"<<endl;
do{
inputan();
}while (col1 != row2);
tampilkan_matriks();
cout << "\nMatriks A * Matriks B ";
for (ib=0; ib<row1; ib++){
for (ik=0; ik<col2; ik++){
C[ib][ik]=0;
for (int j=0; j<col1; j++){
C[ib][ik] = C[ib][ik] + (A[ib][j] * B[j][ik]);
}
}
}
cout << endl;
for (ib=0; ib<row1; ib++){
for(ik=0; ik<col2; ik++){
cout << C[ib][ik] << " ";
}
cout << endl;
}
}
int penjumlahan_matriks(){
cout<<"Jumlah Colom dan Baris Matriks A harus sama dengan Matriks B!"<<endl;
do{
inputan();
}while(col1 != col2 || row1 != row2);
tampilkan_matriks();
cout <<"\nMatriks A + Matriks B";
for (ib=0; ib<row1; ib++){
for (ik=0; ik<col2; ik++){
C[ib][ik] = A[ib][ik] + B[ib][ik];
}
}
cout << endl;
for (ib=0; ib<row1; ib++){
for(ik=0; ik<col1; ik++){
cout << C[ib][ik] << "\t";
}
cout << endl;
}
}
int transpose_matriks(){
cout << "\nInput jumlah baris Matriks : ";
validasi(); row1=atoi(val);
cout << "Input jumlah kolom Matriks : ";
validasi(); col1=atoi(val);
cout << endl;
cout << "\nMatriks A"<< endl;
for (ib=0; ib<row1; ib++){
for (ik=0; ik<col1; ik++){
cout << "Input baris ke "<< (ib+1) <<" kolom ke " << (ik+1) << " : ";
validasi(); A[ib][ik]=atoi(val);
}
}
cout <<"\nMatriks" << endl;
for (ib=0; ib<row1; ib++){
for (ik=0; ik<col1; ik++){
cout << A[ib][ik]<<" ";
}
cout << endl;
}
cout <<"\nTranspose Matriks" << endl;
for (ib=0; ib<col1; ib++){
for (ik=0; ik<row1; ik++){
cout << A[ik][ib]<<" ";
}
cout << endl;
}
for(int i=0;i<row1;i++)
free(A[i]);
for(int i=0;i<row2;i++)
free(B[i]);
for(int i=0;i<row1;i++)
free(C[i]);
free(A);
free(B);
free(C);
getch();
}


This comment has been removed by the author.
ReplyDelete