#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
typedef struct
{
int M[40][40];
int br;
int kl;
}mtrks;
void entri (mtrks *A, mtrks *B);
//void entrinilai(mtrks *A, mtrks *B);
void entrinilai(mtrks *A,mtrks *B);
void tampil(mtrks A, mtrks B);
//void tampil(Matriks M);
void pilih(mtrks *A, mtrks *B, mtrks *C);
int add(mtrks A, mtrks B, mtrks *C);
void transposeSquareMatrix(mtrks *C, int br, int kl);
int main()
{
int br,kl;
mtrks A,B,C;
entri(&A,&B);
entrinilai(&A,&B);
// entrinilai(&B);
tampil(A,B);
getch();
pilih(&A,&B,&C);
getch();
return 0;
}
int pil;
void entri(mtrks *A, mtrks *B)
{
do{
printf("Masukkan jumlah baris matriks A : ");
scanf("%d",&(*A).br);
printf("Masukkan jumlah kolom matriks A :");
scanf("%d",&(*A).kl);
if((((*A).br)*((*A).kl))>41 )
{
printf("Maaf, jumlah maksimal elemen adalah 40\n\n");
}
}while((((*A).br)*((*A).kl))>41);
do{
printf("Masukkan jumlah baris matriks B : ");
scanf("%d",&(*B).br);
printf("Masukkan jumlah kolom matriks B :");
scanf("%d",&(*B).kl);
if((((*B).br)*((*B).kl))>41 )
{
printf("Maaf, jumlah maksimal elemen adalah 40\n\n");
}
}while((((*B).br)*((*B).kl))>41);
}
void entrinilai(mtrks *A, mtrks *B)
{
int br,kl;
printf("\n\n\nMatriks A : \n");
for(br=0;br<(*A).br;br++)
{
for(kl=0;kl<(*A).kl;kl++)
{
printf("Masukkan nilai baris ke %d kolom ke %d : ",br+1,kl+1);
scanf("%d",&(*A).M[br][kl]);
}
}
printf("\n\n");
printf("Matriks B :\n");
for(br=0;br<(*B).br;br++)
{
for(kl=0;kl<(*B).kl;kl++)
{
printf("Masukkan nilai baris ke %d kolom ke %d : ",br+1,kl+1);
scanf("%d",&(*B).M[br][kl]);
}
}
}
void tampil(mtrks A, mtrks B)
{
int br,kl;
printf("\n\nMatriks A = \n");
for(br=0;br<A.br;br++)
{
for(kl=0;kl<A.kl;kl++)
{
printf("%d\t",A.M[br][kl]);
}
printf("\n");
}
printf("\n\n");
printf("Matriks B =\n");
for(br=0;br<B.br;br++)
{
for(kl=0;kl<B.kl;kl++)
{
printf("%d\t",B.M[br][kl]);
}
printf("\n");
}
}
int add(mtrks A, mtrks B, mtrks *C)
{
int br,kl;
if(A.br==B.br&&A.kl==B.kl)
{
for(br=0;br<A.br;br++)
for(kl=0;kl<A.kl;kl++)
{
(*C).M[br][kl]=A.M[br][kl]+B.M[br][kl];
}
}
else
printf("Operasi penjumlahan tidak dapat dilakukan\n");
}
void pilih(mtrks *A, mtrks *B, mtrks *C)
{
int pil;
int br,kl;
do{
system("CLS");
printf("\n************MENU OPERASI MATEMATIKA***********\n");
printf("1. Penjumlahan\n");
printf("2. Pengurangan\n");
printf("3. Perkalian\n");
printf("4. Transpose\n");
printf("5. Lihat tampilan matriks\n");
printf("0. Keluar\n");
printf("Masukkan pilihan Anda :");
scanf("%d",&pil);
switch(pil)
{
case 1 :
{
//printf("Total penjumlahan adalah : %d\n");
if((*A).br==(*B).br&&(*A).kl==(*B).kl)
{
printf("Matriks C=\n");
for(br=0;br<(*A).br;br++)
{
for(kl=0;kl<(*A).kl;kl++)
{
(*C).M[br][kl]=(*A).M[br][kl]+(*B).M[br][kl];
printf("%d\t",(*C).M[br][kl]);
}
printf("\n");
}
}
else
printf("tidak dapat menjalankan operasi penjumlahan ordo tidak sama");
getch();
break;
}
case 2 :
if((*A).br==(*B).br&&(*A).kl==(*B).kl)
{
printf("Matriks C=\n");
for(br=0;br<(*A).br;br++)
{
for(kl=0;kl<(*A).kl;kl++)
{
(*C).M[br][kl]=(*A).M[br][kl]-(*B).M[br][kl];
printf("%d\t",(*C).M[br][kl]);
}
printf("\n");
}
}
else
printf("tidak dapat menjalankan operasi penjumlahan ordo tidak sama");
getch();
break;
case 3 :
{
int br,kl,k;
for(br=0;br<(*B).br;br++)
{
for(kl=0;kl<(*B).kl;kl++)
{
(*C).M[br][kl]=0;
for(k=0;k<(*B).kl;k++)
{
(*C).M[br][kl]=(*C).M[br][kl]+(*A).M[br][k]*(*B).M[k][kl];
}
printf("%d\t",(*C).M[br][kl]);
}
printf("\n");
}
}
getch();
break;
case 4 :
// void transposeSquareMatrix(mtrks *C, int br, int kl)
{
int i,j;
if(br == kl){
for(i=0;i<br;i++)
{
for(j=0;j<kl;j++)
{
int temp=(*C).M[i][j];
(*C).M[i][j]=(*C).M[j][i];
(*C).M[j][i]=temp;
printf("%d\t",(*C).M[br][kl]);
}
}printf("\n");
}
else {
printf("Error transposing matrix, not square matrix");
exit(0);
}
}
getch();
break;
case 5 :
{
int br,kl;
printf("\n\nMatriks A = \n");
for(br=0;br<(*A).br;br++)
{
for(kl=0;kl<(*A).kl;kl++)
{
printf("%d\t",(*A).M[br][kl]);
}
printf("\n");
}
printf("\n\n");
printf("Matriks B =\n");
for(br=0;br<(*B).br;br++)
{
for(kl=0;kl<(*B).kl;kl++)
{
printf("%d\t",(*B).M[br][kl]);
}
printf("\n");
}
getch();break;
}
case 0 :
{
printf(" ");
break;
}
default :
printf("Masukkan angka 0-5 saja.\n");
getch(); break;
}
}while(pil!=0);
}
rachelwiliam 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Tashandra 0 Newbie Poster
jephthah 1,888 Posting Maven
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
tux4life 2,072 Postaholic
Salem commented: Excellence! +36
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.