|
1 /** |
|
2 * $Id: gauss.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ |
|
3 * $Source: /development/c/gauss/gauss.c,v $ |
|
4 * |
|
5 */ |
|
6 |
|
7 #define VERSION 0.8 |
|
8 |
|
9 #ifndef VERBOSE |
|
10 #define VERBOSE 1 |
|
11 #endif |
|
12 #ifndef ZEROASSUMPTION |
|
13 #define ZEROASSUMPTION 1E-3 /* 0.001 = 0.0 */ |
|
14 #endif |
|
15 |
|
16 #include <stdio.h> |
|
17 #include <stdlib.h> |
|
18 |
|
19 void ausgabe (float **A, int MAXX, int MAXY) |
|
20 { |
|
21 int n, k; |
|
22 |
|
23 for (k = 0; k < MAXY; k++) { |
|
24 for (n = 0; n < MAXX - 1; n++) { |
|
25 if ((A[k][n] < ZEROASSUMPTION) && (A[k][n] > -ZEROASSUMPTION)) |
|
26 A[k][n] = 0.0; |
|
27 printf ("%8.2f ", A[k][n]); |
|
28 } |
|
29 printf ("=%8.2f\n", A[k][n]); |
|
30 } |
|
31 } |
|
32 |
|
33 void gauss (float **A, int MAXX, int MAXY) |
|
34 { |
|
35 int i, j, k, n; |
|
36 float h; |
|
37 |
|
38 i = 0; |
|
39 for (j = 0; j < MAXY; j++) { |
|
40 for (k = j + 1; k < MAXY; k++) { |
|
41 if (A[k][i] != 0) |
|
42 h = A[j][i] / A[k][i]; |
|
43 else |
|
44 continue; |
|
45 |
|
46 for (n = 0; n < MAXX; n++) { |
|
47 A[k][n] *= -h; |
|
48 A[k][n] += A[j][n]; |
|
49 } |
|
50 } |
|
51 i++; |
|
52 } |
|
53 |
|
54 #ifdef PUPILS |
|
55 printf ("\n"); |
|
56 ausgabe (A, MAXX, MAXY); |
|
57 printf ("\n"); |
|
58 #endif |
|
59 |
|
60 i--; /* i = (MAXX-1)-1; */ |
|
61 |
|
62 for (j = MAXY - 1; j > -1; j--) { |
|
63 for (k = j - 1; k > -1; k--) { |
|
64 h = A[j][i] / A[k][i]; |
|
65 for (n = MAXX - 1; n > k - 1; n--) { |
|
66 A[k][n] *= -h; |
|
67 A[k][n] += (A[j][n]); |
|
68 if (A[j][i] == 0.0) { |
|
69 printf ("Divison by Zero\n"); |
|
70 exit (-1); |
|
71 } |
|
72 A[j][n] /= (A[j][i]); |
|
73 } |
|
74 } |
|
75 i--; |
|
76 } |
|
77 |
|
78 if ((h = A[0][0]) == 0.0) { |
|
79 printf ("Division by Zero\n"); |
|
80 exit (-1); |
|
81 } |
|
82 /*h=A[0][0];*/ |
|
83 A[0][0] /= h; |
|
84 A[0][MAXX - 1] /= h; |
|
85 } |
|
86 |
|
87 int main (int argc, char **argv) |
|
88 { |
|
89 int i, k; |
|
90 int MAXX, MAXY; |
|
91 float value; |
|
92 |
|
93 float **A; |
|
94 |
|
95 if (argc != 2) { |
|
96 printf ("Benutzung: %s UNBEKANNTE\n", argv[0]); |
|
97 printf ("Berechnet ein lineares Gleichungssystem mit n UNBEKANNTEN und n Zeilen ( nxn ).\n"); |
|
98 printf ("Praktische Beispiele stehen in der README Datei.\n"); |
|
99 return EXIT_SUCCESS; |
|
100 } |
|
101 |
|
102 MAXY = atoi (argv[1]); |
|
103 MAXX = MAXY + 1; |
|
104 |
|
105 if ((A = calloc (MAXY, sizeof (float *))) == NULL) { |
|
106 printf ("Nicht genug Speicher verfuegbar\n"); |
|
107 return EXIT_SUCCESS; |
|
108 } |
|
109 |
|
110 for (i = 0; i < MAXY; i++) { |
|
111 if ((A[i] = calloc (MAXX, sizeof (float))) == NULL) { |
|
112 printf ("Nicht genug Speicher verfuegbar\n"); |
|
113 for (k = 0; k < i; k++) |
|
114 free (A[k]); |
|
115 free (A); |
|
116 |
|
117 return EXIT_SUCCESS; |
|
118 } |
|
119 } |
|
120 |
|
121 for (k = 0; k < MAXY; k++) { |
|
122 printf ("Spalte %d: ", k); |
|
123 for (i = 0; i < MAXX; i++) { |
|
124 if (scanf ("%f", &value) < 1) { |
|
125 printf ("READ ERROR\n"); |
|
126 return EXIT_FAILURE; |
|
127 } |
|
128 A[k][i] = value; |
|
129 } |
|
130 } |
|
131 |
|
132 if (VERBOSE) { |
|
133 printf ("\n"); |
|
134 ausgabe (A, MAXX, MAXY); |
|
135 } |
|
136 |
|
137 gauss (A, MAXX, MAXY); |
|
138 |
|
139 printf ("\nLoesung des GL-Systems( %d x %d )\n\n", MAXX - 1, MAXY); |
|
140 for (i = 0; i < MAXY; i++) |
|
141 printf ("a[%2d] = %8.2f\n", i, A[i][MAXX - 1]); |
|
142 |
|
143 for (i = 0; i < MAXY; i++) |
|
144 if (A[i] != NULL) |
|
145 free (A[i]); |
|
146 if (A != NULL) |
|
147 free (A); |
|
148 |
|
149 return EXIT_SUCCESS; |
|
150 } |