Bonjour,
Je suis encore un débutant en c. (j'ai un claver qwerty, dsl pour les accents)
Je chercher un moyen pour retourner un tableau 2D dans mon main grace a la fonction Matrice. mais je ne sais pas comment faire ?
Puis j'aimerais que ma deuxieme fonction Multimatrice puisse prendre en parametre un tableau 2D et un tableau 1D pour retourner un tableau 1D.
La fonction Matrice retourne une matrice inverse forme grace aux coefficients donne en parametre.
La fonction Multimatrice multiplie deux matrices. Une matrice carre et une matrice colonne et retourne matrice colonne.
Le code de la fonction Matrice :Code:
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 #include <stdio.h> #include <stdlib.h> #include <string.h> #include "Inverseur.h" #include "Multimatrice.h" #include <math.h> struct Parametre { double Longueur; int Nbrepoints; int tend; double Re; }; int main(void) { struct Parametre p = {2.,81,2,100.}; double dx= (double) p.Longueur/( (double) p.Nbrepoints-1); double dt=dx/2.; double a = -dt/(p.Re*pow(dx,2)); double b = 1+2*dt/(p.Re*pow(dx,2)); double c = -dt/(p.Re*pow(dx,2)); double*x=NULL,*B=NULL, *u=NULL; /** dynamic allocation */ x= calloc(p.Nbrepoints,sizeof(double)); u= calloc(p.Nbrepoints,sizeof(double)); B= calloc(p.Nbrepoints, sizeof(double)); /** dynamic allocation 2D table */ double **Ainv = (double **)malloc(sizeof(double*)*p.Nbrepoints); double *tableau2 = (double *)malloc(sizeof(double)*p.Nbrepoints*p.Nbrepoints); for(int i = 0 ; i < p.Nbrepoints ; i++) Ainv[i] = &tableau2[i*p.Nbrepoints]; /** initial elevation: a "bump", position */ double t=0.; for(int i=0; i<p.Nbrepoints;i++) { x[i+1] = x[i] + dx; u[i] = 1.; } for(int i =10; i<20;i++) u[i]=2.; /** Boundaries Conditions */ u[0]=1.; u[p.Nbrepoints]=1.; /** Matrice B */ for (int i=1;i<p.Nbrepoints-1;i++){ B[i] = (dt/dx)*(u[i-1]-u[i])+u[i]; } B[0] = u[0]; B[p.Nbrepoints] = u[p.Nbrepoints]; /** Matrice inverse */ Ainv = Matrice(&p.Nbrepoints, &a, &b, &c); /** begin the time loop */ FILE *fic = fopen("data.txt", "w"); fprintf(fic, "%s %s %s\n", "#x", "#u", "#t"); while(t<=p.tend){ for(int i=0;i<p.Nbrepoints;i++) fprintf(fic, "%g %g %g\n", x[i], u[i], t); fflush(fic); u=Multimatrice(Ainv,B,&p.Nbrepoints); memcpy(u,B,sizeof(double)*p.Nbrepoints); fprintf(fic,"\n\n"); t+=dt; } free(x); free(u); free(B); fclose(fic); free(tableau2); free(Ainv); return 0; }
Cette fonction prend en parametre un entier qui est la taille du tableau carre puis deux doubles qui sont des coefficients. Puis retourne mon tableau 2D.
Le code de la fonction Multimatrice : cette fonction prend en parametre mon tableau carre 2D, un tableau 1D et la taille du tableau.Code:
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 #include <stdio.h> #include "Inverseur.h" double *Matrice(int *taille, double *un, double *deux, double *trois){ /* Declaration variable qui contient la valeur de la variable pointee **/ int n = *taille; int a = *un; int b = *deux; int c = *trois; /* Initialisation tableau 2d + determinant + **/ double mat[n][n]; double determinant = 0.; static double **Inverse; /* Init **/ for(int i=0;i<n;i++) for(int j=0;j<n;j++) mat[i][j]=0.; /* Boundaries Conditions **/ mat[0][0]=1.; mat[n][n]=1.; /* Matrice coefficients **/ for (int i=1; i<n-1; i++){ mat[i][i]=b; mat[i-1][i]=a; mat[i][i+1]=c; } #if 0 printf("Enter elements of matrix row wise:\n"); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%le", &mat[i][j]); printf("\nGiven matrix is:"); for(int i = 0; i < n; i++){ printf("\n"); for(int j = 0; j < n; j++) printf("%g\t", mat[i][j]); } #endif //finding determinant for(int i = 0; i < n; i++) determinant = determinant + (mat[0][i] * (mat[1][(i+1)%n] * mat[2][(i+2)%n] - mat[1][(i+2)%n] * mat[2][(i+1)%n])); if(determinant == 0) printf("%s","Attention determinant egale a 0"); //printf("\n\ndeterminant: %f\n", determinant); //printf("\nInverse of matrix is: \n"); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++) Inverse[i][j]=((mat[(j+1)%n][(i+1)%n] * mat[(j+2)%n][(i+2)%n]) - (mat[(j+1)%n][(i+2)%n] * mat[(j+2)%n][(i+1)%n]))/ determinant; } #if 0 for(int i = 0; i < n; i++){ printf("\n"); for(int j = 0; j < n; j++) printf("%g\t", Inverse[i][j]); } printf("\n"); #endif return Inverse; }
Je sais que je fais des grosses erreurs sur la gestions des pointeurs et de la memoire mais je me perds un peu.Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <stdlib.h> #include "Multimatrice.h" double **Multimatrice(double **mat1, double *mat2, int *taille){ int n = *taille; double *mul=NULL; mul = calloc(n, sizeof(double)); //static double mul[taille]; for(int i = 0; i < n; i++) { for(int k = 0; k < n; k++) { mul[i] += mat1[i][k] * mat2[k]; } } return mul; }
Puis dans mes fonctions je pense que je dois mieux initialiser mes tableau avec des allocation dynamqiques mais j'aimerais avoir vos conseills.
Je vous en remercie !
Voici le resultats de ma compilation :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 burger1D.c: In function main: burger1D.c:70:8: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] Ainv = Matrice(&p.Nbrepoints, &a, &b, &c); ^ burger1D.c:82:6: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] u=Multimatrice(Ainv,B,&p.Nbrepoints); ^ Inverseur.c: In function Matrice: Inverseur.c:71:11: warning: return from incompatible pointer type [-Wincompatible-pointer-types] return Inverse; ^~~~~~~ Multimatrice.c: In function Multimatrice: Multimatrice.c:16:10: warning: return from incompatible pointer type [-Wincompatible-pointer-types] return mul; ^~~ /tmp/cc3nEme4.o*: Dans la fonction «*main*»*: burger1D.c:(.text+0xc3)*: référence indéfinie vers «*pow*» burger1D.c:(.text+0x122)*: référence indéfinie vers «*pow*» burger1D.c:(.text+0x195)*: référence indéfinie vers «*pow*» collect2: error: ld returned 1 exit status