Bonjour, je poste ce sujet car dans le cadre d'un stage je dois réaliser un projet qui demande un algorithme capable de résoudre un système d'équations linéaires (n équations à n inconnues).
En cherchant sur internet, j'en ai trouvé un en open source (posté par cs_khayyam sur codes_sources.com) :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <math.h>
#include <conio.h>
#include <conio.c>
#define beep printf("%c",0x07);
#define precision 0.0001
 
void clreos(void)
{
    int i,x,y;
    clreol();
    x=wherex();
    y=wherey();
    for (i=wherey()+1; i<=25; i++)
    {
        gotoxy(1,i);
        clreol();
    }
    gotoxy(x,y)  ;
    return;
}
///////////////////////////////////////////////////////////////////////////////////////////
 
void afficher(int n,int p, float A[n][p],float B[n], int imposs)
{
    int ref,i,j,i2;
 
    if (n==0) printf("syst%cme vide",138);
    else if ((imposs==1) || (n>p))
    {
        textcolor(12);
        printf("Syst%cme impossible",138);
        textcolor(7);
    }
    else if (n==1)
    {
        i=0;
        while ((i<p) && (A[0][i]!=1))
            i++;  //i est donc la position d'un "1", il y en a forcément au moins un
        printf("X%d = ",i+1);
        ref=0;
        for (j=0; j<p; j++)
        {
            if (j==i) continue;
            if (fabs(A[0][j])>precision)
            {
                if ((A[0][j]<0) && (ref==1)) printf("+");
                if (A[0][j]==1)
                {
                    printf("-X%d",j+1);
                    ref=1;
                }
                else if (A[0][j]==-1)
                {
                    printf("X%d",j+1);
                    ref=1;
                }
                else
                {
                    printf("%8.3fX%d",-A[0][j],j+1);
                    ref=1;
                }
            }
 
        }
        if (fabs(B[0])>precision)
        {
            if ((ref==1) && (B[0]>0)) printf("+");
            printf("%8.3f",B[0]);
        }
    }
 
    else
        for (i=0; i<n; i++)
        {
            for (j=0; j<p; j++)
                if (A[i][j]==1)
                {
                    i2=0;
                    while (i2<n)
                    {
                        if (i2==i)
                        {
                            i2++;
                            continue;
                        }
                        if (fabs(A[i2][j])<precision)i2++;
                    }
                    if (i2==n)
                    {
                        printf("X%d",j+1);     //juste pour ne pas perdre l'indice de la valeur déjà écrite
                        i2=j;
                    }
                }
            printf(" = ");
 
            ref=0;   //drapeau
            for (j=0; j<p; j++)
            {
                if (j==i2) continue;  //tous les autres différents de 0 seront écrits
 
                if (fabs(A[i][j])>precision)
                {
                    if ((ref==1) && (A[i][j]<0)) printf("+");
 
                    if (A[i][j]==1)
                    {
                        printf("-X%d",j+1);
                        ref=1;
                    }
                    else if (A[i][j]==-1)
                    {
                        printf("X%d",j+1);
                        ref=1;
                    }
                    else
                    {
                        printf("%8.3fX%d",-A[i][j],j+1);
                        ref=1;
                    }
                }
 
            }
            if (fabs(B[i])>precision)
            {
                if ((ref==1) && (B[i]>0)) printf("+");
                printf("%8.3f",B[i]);
            }
            printf("\n");
 
        }//fin du for i (pour chaque ligne) et donc fin du else
    return;
}//fin de la fonction afficher
////////////////////////////////////////////////////////////////////////////////////////
 
float saisie (int i, int j)
{
    float coef;
    int compteur;
    char * coefstring;
    coefstring=(char*)malloc(sizeof(char));
 
    gotoxy(1,8);
    clreos();
    printf("entrez le nombre [%d][%d] : ",i+1,j+1);
    gets(coefstring);
    compteur=sscanf(coefstring,"%f",&coef);
 
    while (compteur!=1)
    {
        beep
        gotoxy(1,8);
        clreos();
        printf("entrez le nombre [%d][%d] : ",i+1,j+1);
        gets(coefstring);
        //scanf("%s",coefstring);
        compteur=sscanf(coefstring,"%f",&coef);
    }
 
    return coef;
}
/////////////////////////////////////////////////////////////////////////////////////////
 
float saisie2(int i)
{
    float coef;
    int compteur;
    char * coefstring;
    coefstring=(char*)malloc(sizeof(char));
 
    gotoxy(1,8);
    clreos();
    printf("entrez la constante %d : ",i+1);
    gets(coefstring);
    compteur=sscanf(coefstring,"%f",&coef);
 
    while (compteur!=1)
    {
        beep
        gotoxy(1,8);
        clreos();
        printf("entrez la constante %d : ",i+1);
        gets(coefstring);
        //scanf("%s",coefstring);
        compteur=sscanf(coefstring,"%f",&coef);
    }
 
    return coef;
}
/////////////////////////////////////////////////////////////////////////////////////////
 
void resoudre(int n,int p)
{
    float A[n][p];
    float B[n];
 
    float max,constante;
    int i,i2,j,rang;
    char refaire,impossible;
 
 
 
    for (i=0; i<n; i++)
        for (j=0; j<p; j++)
            A[i][j]=saisie(i,j);
 
    for (i=0; i<n; i++)
        B[i]=saisie2(i);
 
 
 
    gotoxy(1,wherey()-1);
    clreos();
    printf("Le syst%cme suivant \n",138);;
    for (i=0; i<n; i++)
    {
        for (j=0; j<p; j++)
        {
            if ((j>0) && (A[i][j]>=0)) printf("+");
            printf("%8.3f X%d ",A[i][j],j+1);
        }
        printf("  =  %8.3f\n",B[i]);
    }
 
    printf("\nest %cquivalent %c\n",130,133);
 
    impossible=0;
    for(j=0; (j<n) && (impossible==0); j++)
    {
        refaire=1;
        while (refaire==1)
        {
            /*recherche du max de la jieme ligne*/
            max=A[j][0];
            rang=0;
            for(i=1; i<p; i++)
                if (fabs(A[j][i])>fabs(max))
                {
                    max=A[j][i];
                    rang=i;
                }
 
            /*verifier que le max n'est pas nul*/
            if (fabs(max)<precision)       //car la gestion de nombres à virgules n'est pas efficace au delà d'un certain rang - equivalent à max==0
                if (fabs(B[j])>precision)
                {
                    impossible=1;    //equivalent à if B[j]!=0
                    refaire=0;
                }
                else if (j<(n-1))         //s'il y a encore une équation après
                {
                    for(i=0; i<p; i++)          //une equation est combinaison lineaire des autres, elle est donc inutile
                        A[j][i]=A[n-1][i];       //remonter la n-1 ieme équation en jieme position
                    B[j]=B[n-1];
                    n--;
                    refaire=1;
                }
                else
                {
                    refaire=0;
                    max=1; //max a une valeur quelconque autre que 0 (pour la division)
                    n--;
                }
 
            else refaire=0;
 
        }
 
        /*printf("max = %7.20f\n",max);*/
 
        if (impossible==0)
        {
            /*division de la jieme equation par max*/
            for (i=0; i<p; i++)
                A[j][i]/=max;
            B[j]/=max;
 
            /*soustractions aux autres equations*/
            for (i=j+1; i<n; i++)  /*pour chaque equation d'après*/
            {
                constante=A[i][rang];
                B[i]-=constante*B[j];
                for (i2=0; i2<p; i2++)          /*pour chaque coefficient*/
                    A[i][i2]-=constante*A[j][i2];
            }
 
        }/*fin du if*/
 
    }/*fin de la boucle j*/
 
    if ((impossible==0) && (n!=0))
    {
        /*remonter le systeme*/
        for(j=n-1; j>=0; j--)
        {
            max=A[j][0];
            rang=0;
            for(i=0; i<p; i++)
                if (fabs(A[j][i])>max)
                {
                    max=A[j][i];
                    rang=i;
                }
 
            for(i=0; i<p; i++)
                A[j][i]/=max;
 
            for(i=j-1; i>=0; i--)
            {
                constante=A[i][rang];
                B[i]-=constante*B[j];
                for(i2=0; i2<p; i2++)
                    A[i][i2]-=constante*A[j][i2];
            }
        }
 
    }
 
    afficher(n,p,A,B,impossible);
 
}//fin de la fonction resoudre
/////////////////////////////////////////////////////////////////////////////////////
 
int main()
{
    int n,p,i;
    char * temp;
    temp=(char*)malloc(sizeof(char));
    printf("%c",201);
    for (i=1; i<=78; i++) printf("%c",205);
    printf("%c",187);
    printf("%c",186);
    printf("                      M%cthode du pivot maximum                                ",130);
    printf("%c",186);
    printf("%c",200);
    for (i=1; i<=78; i++) printf("%c",205);
    printf("%c\n\n\n",188);
 
 
    gotoxy(1,7);
    clreos();
    printf("Combien d'%cquations : ",130);
    gets(temp);
    i=sscanf(temp,"%d",&n);
    if (n<=0) i=0;
 
    while (i!=1)
    {
        beep
        gotoxy(1,7);
        clreos();
        printf("Combien d'%cquations : ",130);
        gets(temp);
        //scanf("%d",&n);
        i=sscanf(temp,"%d",&n);
        if (n<=0) i=0;
    }
 
 
    gotoxy(1,8);
    clreos();
    printf("Combien d'inconnues : ");
    gets(temp);
    //scanf("%d",&p);
    i=sscanf(temp,"%d",&p);
    if (p<=0) i=0;
 
    while  (i!=1)
    {
        beep
        gotoxy(1,8);
        clreos();
        printf("Combien d'inconnues : ");
        gets(temp);
        //scanf("%d",&p);
        i=sscanf(temp,"%d",&p);
        if (p<=0) i=0;
    }
 
 
    gotoxy(1,7);
    clreos();
    printf("R%csolution d'un syst%cme  %d x %d\n\n",130,138,n,p);
 
    resoudre(n,p);
 
    printf("\nfini");
    getchar();
    return 0;
}
J'ai donc installé la bibliothèque conio sur codeblocks mais après lancement du run il s'avère que codeblocks m'informe que les variables p,n,A,B ne sont pas définies.
Peut-etre que j'ai mal installé la bibliothèque, en ce cas je continue à chercher à moins que arriviez à m'expliquer ou placer les fichier conio.c et .h, mais sinon je ne comprends pas comment l'utiliser
En espérant que vous puissiez m'aider !