Penjumlahan dan pengurangan matriks memiliki syarat ordo kedua matriks harus sama dan untuk perkalian matriks kolom matriks 1 harus sama dengan baris matriks 2
Source Code:
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <dos.h>
using namespace std;
int b,k,b1,k1;
int a[10][10];
int c[10][10];
int d[10][10];
int e[10][10];
int temp;
void setcolor(unsigned short color)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,color);
}
#define delay Sleep
void gotoxy(unsigned int kolom, unsigned int baris)
{
COORD posisi = {kolom,baris};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), posisi);
}
int masukan()
{
cout << "Masukan jumlah baris matriks 1 = ";
cin>>b;
cout<< "Masukan jumlah kolom matriks 1 = ";
cin>>k;
system ("cls");
for (int i=0;i<b;i++)
{
for (int j=0;j<k;j++)
{
cout<<"Masukan baris "<<i+1<<" kolom "<<j+1<<" = ";
cin>>a[i][j];
}
}
system ("cls");
cout << "Masukan jumlah baris matriks 2 = ";
cin>>b1;
cout<< "Masukan jumlah kolom matriks 2 = ";
cin>>k1;
system ("cls");
}
int input()
{
for (int i=0;i<b1;i++)
{
for (int j=0;j<k1;j++)
{
cout<<"Masukan baris "<<i+1<<" kolom "<<j+1<<" = ";
cin>>c[i][j];
}
}
system ("cls");
cout<<"Matriks 1"<<endl;
for (int i=0;i<b;i++)
{
for (int j=0;j<k;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
cout<<endl;
cout<<"Matriks 2"<<endl;
for (int i=0;i<b1;i++)
{
for (int j=0;j<k1;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
}
tambah()
{
for (int i=0;i<b;i++)
{
for (int j=0;j<k;j++)
{
d[i][j]=a[i][j]+c[i][j];
}
}
cout<<endl;
cout<<"Matriks 1 + Matriks 2 "<<endl;
for (int i=0;i<b;i++)
{
for (int j=0;j<k;j++)
{
cout<<d[i][j]<<"\t";
}
cout<<endl;
}
}
kurang()
{
for (int i=0;i<b;i++)
{
for (int j=0;j<k;j++)
{
d[i][j]=a[i][j]-c[i][j];
}
}
cout<<"Matriks 1 - Matriks 2 "<<endl;
for (int i=0;i<b;i++)
{
for (int j=0;j<k;j++)
{
cout<<d[i][j]<<"\t";
}
cout<<endl;
}
}
kali()
{
for(int i=0;i<b;i++)
{
for(int j=0;j<k1;j++)
{
e[i][j]=0;
for(int k=0;k<b1;k++)
{
e[i][j]+=a[i][k]*c[k][j];
}
}
}
cout<<"\nMatriks 1 x Matriks 2 "<<endl;
for (int i=0;i<b;i++)
{
for (int j=0;j<k1;j++)
{
cout<<e[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
int pilih;
char balik;
for(int i=0;i<20; i++)
{ setcolor(2);
gotoxy(i,11);
cout << "PERHITUNGAN MATRIKS";
delay(100);
gotoxy(i,10);
cout << "";
gotoxy(i,11);
cout << " "<<endl;
if (i==69)
{
i=0;
}
}
setcolor(7);
cout<<"\t\t\tMENU"<<endl;
cout<<"\t\t1. PENAMBAHAN"<<endl;
cout<<"\t\t2. PENGURANGAN"<<endl;
cout<<"\t\t3. PERKALIAN"<<endl;
cout<<"\t\t4. EXIT"<<endl;
cout<<"\t\tMasukan pilihan = ";
cin>>pilih;
system ("cls");
switch(pilih){
case 1:
masukan();
if (b==b1 && k==k1)
{
input();
tambah();
}
else
{
cout<<"MATRIKS TIDAK BISA DITAMBAHKAN KARENA ORDO MATRIKS 1 DAN 2 TIDAK SAMA !!"<<endl;
}
getch();
system ("cls");
cout<<"Kembali kemenu awal ? (y/n) = ";
cin>>balik;
if(balik=='y'||balik=='Y')
{ system ("cls");
main();
}
else{
break;
}
case 2:
{
masukan();
if (b==b1 && k==k1)
{
input();
kurang();
}
else
{
cout<<"MATRIKS TIDAK BISA DIKURANGKAN KARENA ORDO MATRIKS 1 DAN 2 TIDAK SAMA !!"<<endl;
}
getch();
system ("cls");
cout<<"Kembali kemenu awal ? (y/n) = ";
cin>>balik;
if(balik=='y'||balik=='Y')
{ system ("cls");
main();
}
else{
break;
}
}
case 3:
{
masukan();
if (k==b1)
{
input();
kali();
}
else
{
cout<<"MATRIKS TIDAK BISA DIKALIKAN KARENA JUMLAH KOLOM MATRIKS 1 TIDAK SAMA DENGAN JUMLAH BARIS MATRIKS 2 !!"<<endl;
}
getch();
system ("cls");
cout<<"Kembali kemenu awal ? (y/n) = ";
cin>>balik;
if(balik=='y'||balik=='Y')
{ system ("cls");
main();
}
else{
break;
}
}
return 0;
}
}
Output:
0 comments:
Posting Komentar