Skip to content

Instantly share code, notes, and snippets.

@hasayvaz
Created April 11, 2012 18:08
Show Gist options
  • Save hasayvaz/2361039 to your computer and use it in GitHub Desktop.
Save hasayvaz/2361039 to your computer and use it in GitHub Desktop.
iki matrisin çarpımı
#include <stdio.h>
#include <stdlib.h> // calloc ve free fonksiyonlari icin gerekli kutuphane
#define BOYUT 5 // Carpimi bulunacak kare matrislerin boyutu
int i, j; // Global değiskenler. Hem fonksiyonlarda hem mainde ortak kullanildigindan global yapildi.
int **yap(int **h)
{
for (i = 0;i < BOYUT;i++)
for (j = 0;j < BOYUT;j++) {
printf("\n%d.satir %d.sutun: ", i+1, j+1); scanf("%d", &h[i][j]);
}
return h;
}
// Matrislerin her elemani icin bellek aliyoruz
int **bellek(int **t)
{
for (i = 0;i < BOYUT;i++)
t[i] = calloc(BOYUT, sizeof(int));
return t;
}
int main(void)
{
int k;
int **a, **b, **c;
a = calloc(BOYUT, sizeof(int)); // Birinci matris
b = calloc(BOYUT, sizeof(int)); // Ikinci matris
c = calloc(BOYUT, sizeof(int)); // Sonuc matrisi
a = bellek(a); // her matris icin bellek aliyoruz
b = bellek(b);
c = bellek(c);
// a ile b matrisinin elemanlarini yerlestiriyoruz
printf("Lütfen 1.matrisin elemanlarini giriniz.\n");
a = yap(a);
printf("\nLütfen 2.matrisin elemanlarini giriniz.\n");
b = yap(b);
// Matrislerin carpilmasi ve sonuc matrisine yazilmasi
for (i = 0;i < BOYUT;i++)
for (j = 0;j < BOYUT;j++)
for (k = 0;k < BOYUT;k++)
c[i][j] += a[i][k] * b[k][j];
// Sonuc matrisini yazdiriyoruz
printf("\nsonuc matrisi\n\n");
for (i = 0; i < BOYUT; i++) {
for (j = 0;j < BOYUT;j++)
printf("%d ", c[i][j]);
printf("\n");
}
// Aldigimiz bellek alanini geri iade ediyoruz
free(a);
free(b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment