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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
| #include <stdio.h>
#include <process.h>
#include <math.h>
float *a, **x, *y;
int N;
char filename[256];
FILE* InFile=NULL;
void count_num_lines(){
//count number of lines in input file - number of equations
int nelf=0; //non empty line flag
do{
nelf = 0;
while(fgetc(InFile)!='\n' && !feof(InFile)) nelf=1;
if(nelf) N++;
}while(!feof(InFile));
}
void freematrix(){
//free memory for matrixes
int i;
for(i=0; i<N; i++){
delete [] x[i];
}
delete [] a;
delete [] x;
delete [] y;
}
void allocmatrix(){
//allocate memory for matrixes
int i,j;
a = new float[N];
y = new float[N];
x = new float*[N];
if(x==NULL || y==NULL || a==NULL){
printf("\nNot enough memory to allocate for %d equations.\n", N);
exit(-1);
}
for(i=0; i<N; i++){
x[i] = new float[N];
if(x[i]==NULL){
printf("\nNot enough memory to allocate for %d equations.\n", N);
}
}
for(i=0; i<N; i++){
for(j=0; j<N; j++){
x[i][j]=0;
}
y[i]=0;
a[i]=0;
}
}
void readmatrix(){
int i=0,j=0;
//read matrixes from input file
for(i=0; i<N; i++){
fscanf(InFile, "%f", &x[i][1]);
fscanf(InFile, "%f", &y[i]);
}
//init square x matrix
for(i=0; i<N; i++){
for(j=0; j<N; j++){
x[i][j] = pow(x[i][1], j);
}
}
}
void printmatrix(){
//print system of equations
int i=0,j=0;
printf("\n");
for(i=0; i<N; i++){
for(j=0; j<N; j++){
printf(" %+f*A%d", x[i][j], j);
}
printf(" =%f\n", y[i]);
}
}
void testsolve(){
//test that ax=y
int i=0, j=0;
printf("\n");
for(i=0; i<N; i++){
float s = 0;
for(j=0; j<N; j++){
s += x[i][j]*a[j];
}
printf("%f\t%f\n", s, y[i]);
}
}
void printresult(){
int i=0;
printf("\n");
printf("Result\n");
for(i=0; i<N; i++){
printf("A%d = %f\n", i, a[i]);
}
}
void diagonal(){
int i, j, k;
float temp=0;
for(i=0; i<N; i++){
if(x[i][i]==0){
for(j=0; j<N; j++){
if(j==i) continue;
if(x[j][i] !=0 && x[i][j]!=0){
for(k=0; k<N; k++){
temp = x[j][k];
x[j][k] = x[i][k];
x[i][k] = temp;
}
temp = y[j];
y[j] = y[i];
y[i] = temp;
break;
}
}
}
}
}
void cls(){
for(int i=0; i<25; i++) printf("\n");
}
void main(){
int i=0,j=0, k=0;
cls();
do{
printf("\nInput filename: ");
scanf("%s", filename);
InFile = fopen(filename, "rt");
}while(InFile==NULL);
count_num_lines();
allocmatrix();
rewind(InFile);
//read data from file
readmatrix();
//check if there are 0 on main diagonal and exchange rows in that case
diagonal();
fclose(InFile);
printmatrix();
//process rows
for(k=0; k<N; k++){
for(i=k+1; i<N; i++){
if(x[k][k]==0){
printf("\nSolution is not exist.\n");
return;
}
float M = x[i][k] / x[k][k];
for(j=k; j<N; j++){
x[i][j] -= M * x[k][j];
}
y[i] -= M*y[k];
}
}
printmatrix();
for(i=N-1; i>=0; i--){
float s = 0;
for(j = i; j<N; j++){
s = s+x[i][j]*a[j];
}
a[i] = (y[i] - s) / x[i][i];
}
InFile = fopen(filename, "rt");
readmatrix();
fclose(InFile);
printmatrix();
testsolve();
printresult();
freematrix();
} |