Created
December 20, 2013 12:58
-
-
Save mertyildiran/8054471 to your computer and use it in GitHub Desktop.
nxn'lik bir matrisin determinantını hesaplayan program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// nxn'lik bir matrisin determinantını hesaplayan program | |
// Mehmet Mert Yıldıran 12060367 | |
// Ubuntu terminal altında çalıştırma: | |
// gcc determinant.c -o determinant -lm | |
// ./determinant | |
#include<stdio.h> | |
#include<math.h> | |
#define MAX 10 | |
int determ(int a[MAX][MAX],int n); | |
int main() | |
{ | |
int i,j,r,c,a[MAX][MAX],b[MAX][MAX],det=0; | |
printf("\nMatrisin satır sayısını giriniz: "); | |
scanf("%d",&r); | |
printf("\nMatrisin sütun sayısını giriniz: "); | |
scanf("%d",&c); | |
if(r==c) | |
{ | |
printf("\nMatrisin elemanlarını sırasıyla Enter'a basarak giriniz:\n"); | |
for(i=0;i<r;i++) | |
{ | |
for(j=0;j<c;j++) | |
{ | |
scanf("%d",&a[i][j]); | |
} | |
} | |
det=determ(a,r); | |
printf("\nVerilen matris:\n"); | |
for(i=0;i<r;i++) | |
{ | |
for(j=0;j<c;j++) | |
{ | |
printf("%d\t",a[i][j]); | |
} | |
printf("\n"); | |
} | |
printf("\nMatrisin determinantı: %d\n",det); | |
} | |
else | |
{ | |
printf("\nDeterminant hesaplanamadı! Matrisin satır ve sütun sayıları eşit olmalı.\n"); | |
} | |
return 0; | |
} | |
int determ(int a[MAX][MAX],int n) | |
{ | |
int det=0,p,h,k,i,j,temp[MAX][MAX]; | |
if(n==1) | |
{ | |
return a[0][0]; | |
} | |
else if(n==2) | |
{ | |
det=(a[0][0]*a[1][1]-a[0][1]*a[1][0]); | |
return det; | |
} | |
else | |
{ | |
for(p=0;p<n;p++) | |
{ | |
h = 0; | |
k = 0; | |
for(i=1;i<n;i++) | |
{ | |
for( j=0;j<n;j++) | |
{ | |
if(j==p) | |
{ | |
continue; | |
} | |
temp[h][k] = a[i][j]; | |
k++; | |
if(k==n-1) | |
{ | |
h++; | |
k = 0; | |
} | |
} | |
} | |
det=det+a[0][p]*pow(-1,p)*determ(temp,n-1); | |
} | |
return det; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment