-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGaussEliminasyon.c
114 lines (109 loc) · 3 KB
/
GaussEliminasyon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
void getInputMatrix(float **matrix, int n, int m) {
int i,j;
printf("Matrisin elemanlarýný giriniz:\n");
for ( i = 0; i < n; i++) {
for ( j = 0; j < m; j++) {
printf("[%d][%d]elemaný giriniz: ", i+1, j+1);
scanf("%f", &matrix[i][j]);
}
}
}
void birimMatris(float **matrix, int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j){
*(*(matrix+i)+j)=1;
}else *(*(matrix+i)+j)=0;
}
}
}
void printMatrix(float **matrix, int n, int m){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
printf("%.2f ", matrix[i][j]);
}printf("\n");
}
}
void swapRows(float **matrix, int row1, int row2, int n) {
int i;
for ( i = 0; i < n; i++) {
float temp = matrix[row1][i];
matrix[row1][i] = matrix[row2][i];
matrix[row2][i] = temp;
}
}
void multiplyRow(float **matrix, int row, float scalar, int n) {
int i;
for ( i = 0; i < n; i++) {
matrix[row][i] *= scalar;
}
}
void addMultipleOfRow(float **matrix, int sourceRow, int destinationRow, float multiple, int n) {
int i;
for ( i = 0; i < n; i++) {
matrix[destinationRow][i] += multiple * matrix[sourceRow][i];
}
}
float** yerAc(int n, int m){
int i;
float **matrix = (float **)calloc(n ,sizeof(float *));
if (matrix == NULL) {
printf("Bellek tahsis edilemedi.\n");
exit(1);
}
for ( i = 0; i < n; i++) {
matrix[i] = (float *)calloc(m ,sizeof(float));
if (matrix[i] == NULL) {
printf("Bellek tahsis edilemedi.\n");
exit(1);
}
}
return matrix;
}
void sonucYazdir(float* cevap, int n){
int i;
for(i=0;i<n;i++){
printf("x%d: %f\n", i+1, cevap[i]);
}
}
void GaussEliminasyon(float **matrix, int n, float*cevap) {
// Girilen matrisi birim matrise cevirmek icin elementer satir islemleri kullanma
int i = 0,j;
while (i < n) {
// kosegendeki elemaný 1 yapalým
float scalar = 1.0f / matrix[i][i];
multiplyRow(matrix, i, scalar, n+1);
// i=0 j=1
// Eliminate non-zero elements below the diagonal
for (j = i+1; j < n; j++) {
if (matrix[j][i]!=0) {
float multiple = (-1)*matrix[j][i];
addMultipleOfRow(matrix, i, j, multiple, n+1);
}
}
i++;
}
printMatrix(matrix, n, n+1);
for (i = n - 1; i >= 0; i--) {
cevap[i] = matrix[i][n];
for (j = i + 1; j < n; j++) {
cevap[i] -= matrix[i][j] * cevap[j];
}
}
sonucYazdir(cevap, n);
}
int main(){
int n;
printf("Kaç tane deklem gireceðinizi yazýnýz.\n");
scanf("%d",&n);
float **GEmatrix= yerAc(n,n+1);
float* sonuclar= calloc(n, sizeof(float));
getInputMatrix(GEmatrix, n,n+1);
GaussEliminasyon(GEmatrix, n, sonuclar);
sonucYazdir(sonuclar, n);
return 0;
}