Skip to content

Instantly share code, notes, and snippets.

@ekickx
Last active March 16, 2020 03:50
Show Gist options
  • Save ekickx/21e6e3abd6fe2f4595ccddee7299bec0 to your computer and use it in GitHub Desktop.
Save ekickx/21e6e3abd6fe2f4595ccddee7299bec0 to your computer and use it in GitHub Desktop.
Operasi matriks dengan C
#include<stdio.h>
#include<string.h>
int determinan(int kolom, int matriks1[][kolom]); // Inisialisasi fungsi
int main(void){
//Matriks
int hasil[10][10],det;
int i , j , k , m , n , p , q , jumlah = 0,ordo;
int CA00,CA01,CA02,CA03;
int z;
char y ,lanjut;
printf("======================================== Matriks ========================================\n");
printf("ukuran baris matriks 1 :");
scanf("%d", &m);
printf("ukuran kolom matriks 1 :");
scanf("%d", &n);
printf("ukuran baris matriks 2 :");
scanf("%d", &p);
printf("ukuran kolom matriks 2 :");
scanf("%d", &q);
ordo =n*m;
int matriks1[m][n];
int matriks2[p][q];
printf("\n");do{
printf("masukan angka matriks 1\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d", &matriks1[i][j]);
}
}
printf("\n");
printf("masukan angka matriks 2\n");
for(i=0;i<p;i++){
for(j=0;j<q;j++){
scanf("%d", &matriks2[i][j]);
}
}
printf("Matriks 1\n");
for (i = 0; i < m; i++){
for (j = 0; j < n; j++){
printf("%d\t", matriks1[i][j]);
if(j == n-1){printf("\n");}}}
printf("Matriks 2\n");
for (i = 0; i < p; i++){
for (j = 0; j < q; j++){
printf("%d\t", matriks2[i][j]);
if(j == q-1){printf("\n");}}}
printf("-------------------------------------------- Pengoperasian --------------------------------------------\n");
//pengoperasian
scanf("%c",&z);
printf("pilih Pengoperasian matrik\n");
printf("1. penjumlahan matriks\n");
printf("2. pengurangan matriks\n");
printf("3. perkalian matriks\n");
printf("4. determinan matriks\n");
scanf("%i",&z);
//penjumlahan matriks
if (z==1){
printf("hasil penjumlahan matriks :\n");
for (i = 0; i < m; i++){
for (j = 0; j < n; j++){
hasil[i][j] = matriks1[i][j] + matriks2[i][j];
printf("%d\t", hasil[i][j]);
if(j == n-1){printf("\n");}
}
}
}
//pengurangan matriks
else if (z==2){
printf("hasil pengurangan matriks :\n");
for (i = 0; i < m; i++){
for (j = 0; j < n; j++){
hasil[i][j] = matriks1[i][j] - matriks2[i][j];
printf("%d\t", hasil[i][j]);
if(j == n-1){printf("\n");}
}
}
}
//perkalian matriks
else if (z==3){
for(i = 0; i < m; i++){
for(j = 0; j < q; j++){
for(k = 0; k < p; k++){
jumlah = jumlah + matriks1[i][k] * matriks2[k][j];
}
hasil[i][j] = jumlah;
jumlah = 0;
}
}
printf("Hasil perkalian matriks: \n");
for(i = 0; i < m; i++){
for(j = 0; j < n; j++){
printf("%d\t", hasil[i][j]);
if(j == n-1){printf("\n");}
}
}
}
//determinan matriks
else if (z==4){
int a=sizeof(matriks1[0]);
int kolom=a/4;
printf("\nDeterminan akan diambil dari matriks1\n");
printf("Determinan = %d", determinan(kolom, matriks1));
}
else if (z>4){
printf("maaf tidak ada dipilihan");
}
printf("\nMau coba lagi(Y/N)? "); scanf("%s", &lanjut);
}while((lanjut=='y')||(lanjut=='Y'));
}
// Fungsi Determinan
int determinan(int kolom, int matriks1[][kolom]){
int jumlah =0;
int a=sizeof(matriks1[0]);
int size=a/4;
int minor[size],l;
if (size==2){
for (int i=0;i<size;i++){
minor[i]=matriks1[1][1-i];
}
}
else if (size>2){
int temp_minor[size-1][size-1];
for (int i=0;i<size;i++){
for (int k=0;k<size-1;k++){
int o = 0;
for (int l=0;l<size-1;l++){
if (l==i){o= o+1;}
temp_minor[k][l]= matriks1[k+1][o];
o=o+1;
}
}
minor[i] = determinan(sizeof(temp_minor[0])/4, temp_minor);
}
}
for (int i=0;i<size;i++){
int cofactor[size];
if (i%2==0){
cofactor[i]=matriks1[0][i]*minor[i]*1;
}
else {
cofactor[i]=matriks1[0][i]*minor[i]*-1;
}
jumlah = jumlah + cofactor[i];
}
return jumlah;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment