IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

méthodes directes avec pointeurs


Sujet :

C

  1. #1
    Membre régulier
    Femme Profil pro
    Etudiante
    Inscrit en
    Avril 2012
    Messages
    203
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Etudiante

    Informations forums :
    Inscription : Avril 2012
    Messages : 203
    Points : 85
    Points
    85
    Par défaut méthodes directes avec pointeurs
    Bonjour,

    j'essaye de programmer deux méthodes directes (gauss et décomposition LU) en un seul programme C

    le programme s’exécute mais après que j'entre la première valeur de la matrice A le fenêtre d’exécution est fermé (quelque soit la méthode choisi)

    je pense que j'ai des probléme au niveau des pointeurs et les 'free()'

    Svp , j'ai besoin de votre aide

    Merci d'avance

  2. #2
    Membre actif
    Avatar de fmdao
    Profil pro
    Formateur en informatique
    Inscrit en
    Novembre 2010
    Messages
    90
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Formateur en informatique

    Informations forums :
    Inscription : Novembre 2010
    Messages : 90
    Points : 210
    Points
    210
    Par défaut
    des détails, un bout de code ?

  3. #3
    Membre régulier
    Femme Profil pro
    Etudiante
    Inscrit en
    Avril 2012
    Messages
    203
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Etudiante

    Informations forums :
    Inscription : Avril 2012
    Messages : 203
    Points : 85
    Points
    85
    Par défaut
    Citation Envoyé par fmdao Voir le message
    des détails, un bout de code ?
    ooops le voilà
    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
    // LES MÉTHODES DIRECTES : GAUSS & DECOMPOSITION LU
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    #define N 30
     
    // * Prototype * //
     
    void remplissage(float **mat,float *b,int n);
    void aff_syst(float **mat,float *b,int n);
    void aff_mat(float **mat,int n);
    void zero(float **mat,float *b,int n);
    void gauss(float **mat,float *b,int n);
    void LU(float **mat,float *b,int n);
     
     
    // * Définition des fonctions *//
     
    // fonction de remplissage
    void remplissage(float **mat,float *b,int n)
    {
    	int i,j;
    	printf("\n  La matrice A:\n\n");
    	for (i=0;i<n;i++)
    	{
    		for (j=0;j<n;j++)
    		{
    			printf("A(%d,%d) : ",i+1,j+1);
    			scanf("%f",mat[i][j]);
    		}
    		printf("\n");
    	}
    	printf("\n  Le vecteur b:\n\n");
    	for(i=0;i<n;i++)
    	{
    		printf("b(%d) : ",i+1);
    		scanf("%f",b[i]);
    	}
    }
     
    // fonction d'affichage système
     
    void aff_syst(float **mat,float *b,int n)
    {
    	int i,j;
    	printf("\n\n");
    	for (i=0;i<n;i++)
    	{
    		printf(" (");
    		for (j=0;j<n;j++)
    		{
    			printf(" %.5f ",mat[i][j]);
    		}
    		printf(") ( %.5f )",b[i]);
    		printf("\n");
    	}
    }
     
    // fonction d'affichage de la matrice
     
    void aff_mat(float **mat,int n)
    {
    	int i,j;
    	printf("\n\n");
    	for (i=0;i<n;i++)
    	{
    		printf(" (");
    		for (j=0;j<n;j++)
    		{
    			printf(" %.5f ",mat[i][j]);
    		}
    		printf(")\n");
    	}
    }
     
    // Mettre à Zero les elements qui doivent etre des zéro
    void zero(float **mat,float *b,int n)
    {
    	int i,j;
    	float erreur=0;
    	for(i=0;i<n;i++)
    	{
    		for (j=0;j<n;j++)
    			if (fabs(mat[i][j])<erreur)
    				mat[i][j]=0;
    			if (fabs(b[i])<erreur)
    				 b[i]=0;
    	}
    }
     
    // -------------------------------------------------------------
    // Résolution Par Gauss
    // -------------------------------------------------------------
     
    void gauss(float **mat,float *b,int n)
    {
    	float *x, p, s;
    	int i,j,k,l;
     
    	// Allocation mémoire
        x = (float*)malloc(n*sizeof(float));
     
     
    	for(l=0;l<n-1;l++)
    	{
    		if (mat[l][l]==0)
    		{
    				printf("\n\n  Un pivot nul n : %d ! => methode de Gauss non applicable\n\n",l);
    		}
    		else
    		{
    			for(k=0;k<n-1;k++)
    			{
    				//Triangulaire supérieure
    				for(i=k+1;i<n;i++)
    				{
    					p=mat[i][k]/mat[k][k];
    					for (j=k;j<n;j++)
    						mat[i][j]-=p*mat[k][j];
    					b[i]-=p*b[k];
    				}
    			}
    			//Résolution
    			for(i=n-1;i>=0;i--)
    			{
    				s=0;
    				for(j=i+1;j<n;j++)
    					s+=mat[i][j]*x[j];
    				x[i]=(b[i]-s)/mat[i][i];
    			}
    			zero(mat,b,n);
    			printf("\n-------------- Gauss -------------\n");
    			printf("\n  La matrice reduite :");
    			aff_syst(mat,b,n);
    			printf("\n  La resolution donne :\n\n");
    			for (i=0;i<n;i++)
    				printf(" X(%d) = %f ;\n",i+1,x[i]);
    		}
    	}
     
    	free(x);
    }
    // -------------------------------------------------------------
    // Résolution Par décomposition LU
    // -------------------------------------------------------------
     
    void LU(float **mat,float *b,int n)
     
    {
    	float **L, **U, *x, *y,s;
    	int i,j,k,m;
     
    	// Allocation mémoire
        x = (float*)malloc(n*sizeof(float));
        y = (float*)malloc(n*sizeof(float));
     
        L = (float**)malloc(n*sizeof(float*));
        for(i=0 ; i<n ;i++)
        {
            L[i] = (float*)malloc(n*sizeof(float));
        }
     
        U = (float**)malloc(n*sizeof(float*));
        for(i=0 ; i<n ;i++)
        {
            L[i] = (float*)malloc(n*sizeof(float));
        }
     
    	for (i=0;i<n;i++){
    		for (j=0;j<n;j++)
    		{
    			if(i==j)
    				U[i][j]=1;
    			else
    				U[i][j]=0;
    			    L[i][j]=0;
    		}
    	}
     
    	for (m=0;m<n;m++)
    	{
    		for (i=m;i<n;i++)
    		{
    			s=0;
    			for (k=0;k<m;k++)
    				s+=L[i][k]*U[k][m];
    			L[i][m]=mat[i][m]-s;
    		}
    		for (j=m+1;j<n;j++)
    		{
    			s=0;
    			for (k=0;k<m;k++)
    				s=s+L[m][k]*U[k][j];
    			U[m][j]=(mat[m][j]-s)/L[m][m];
    		}
    	}
     
    	// resolution
    	for(i=0;i<n;i++)
    	{
    		s=0;
    		for(j=0;j<i;j++)
    			s=s+L[i][j]*y[j];
    		y[i]=(b[i]-s)/L[i][i];
    	}
     
    	for(i=n-1;i>=0;i--)
    	{
    		s=0;
    		for(j=i+1;j<n;j++)
    			s=s+U[i][j]*x[j];
    		x[i]=(y[i]-s)/U[i][i];
    	}
     
    	printf("\n-------------- LU -------------\n");
    	printf("\n  A = L  U \n");
    	printf("\n  La matrice L :");
    	aff_mat(L,n);
    	printf("\n  La matrice U :");
    	aff_mat(U,n);
    	printf("\n  La resolution donne :\n\n");
    	for (i=0;i<n;i++)
    		printf(" X(%d) = %f ;\n",i+1,x[i]);
     
        free(L);
        free(U);
        free(y);
        free(x);
    }
     
    // La fonction principale
     
    int main()
    {
     
    	float **mat,*b;
    	int n,i;
        char valider;
     
        b = (float*)malloc(n*sizeof(float));
     
        mat = (float**)malloc(n*sizeof(float*));
        for(i=0 ; i<n ;i++)
        {
            mat[i] = (float*)malloc(n*sizeof(float));
        }
     
     
    	// choix de la méthode
    	printf("\n \n");
    	printf("  \n");
    	printf("  Resolution de A x = B \n");
    	printf("  \n");
    	printf(" \n");
    	printf("  \n");
    	printf("  Choix de la methode : \n");
    	printf("  \n");
     
    	printf("  A => Gauss \n");
    	printf("  B => Decomposition LU \n");
     
    	printf("  \n");
    	printf(" \n");
    	printf("  \n");
    	printf("  X => E X I T \n"); // Pour quitter le programme
    	printf("  \n");
    	printf(" \n\n");
     
    	essayer:
     
    	printf("------ => donner votre choix ------ : ");
    	scanf("%s",&valider);
    	switch(valider)
    	{
    		case 'A' :
    		{
    			printf("\n\n  Nombre d'equation-inconues strictement inferieur a %d : \n\n  N = ",N);
    			scanf("%d",n);
     
    			    remplissage(mat,b,n);
    				printf("\n\n  Le systeme est : ");
    				aff_syst(mat,b,n);
    				gauss(mat,b,n);
    				break;
            }
     
    		case 'B' :
    		{
     
    			printf("\n\n  Nombre d'equation-inconues strictement inferieur a %d : \n\n  N = ",N);
    			scanf("%d",n);
     
    				remplissage(mat,b,n);
    				printf("\n\n  Le systeme est : ");
    				aff_syst(mat,b,n);
    				LU(mat,b,n);
    				break;
    		}
    		case 'X' :
    		{
    			printf("\nProgramme interompu par l'utilisateur...\n\n");
    			break;
    		}
     
    		default : goto essayer;
    	}
    	printf("\n");
     
    	return 0;
    	free(mat);
    	free(b);
     
    }

  4. #4
    Membre actif
    Avatar de fmdao
    Profil pro
    Formateur en informatique
    Inscrit en
    Novembre 2010
    Messages
    90
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Formateur en informatique

    Informations forums :
    Inscription : Novembre 2010
    Messages : 90
    Points : 210
    Points
    210
    Par défaut
    Regarder dans la section C de ce site, vous y trouverez certainement un tutoriel qui vous aidera.

    "scanf( %s, ...)" : entrée d'une chaîne de caractère et pas d'un caractère.
    essayer" scanf( %c," ? ou "getchar();" http://www.cplusplus.com/reference/cstdio/getchar/


    éviter le "goto", utiliser un "while"...

  5. #5
    Membre expérimenté Avatar de Ngork
    Homme Profil pro
    Barbare IT
    Inscrit en
    Avril 2009
    Messages
    160
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Barbare IT
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2009
    Messages : 160
    Points : 1 372
    Points
    1 372
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
      float **mat,*b;
      int n,i;
      char valider;
     
      b = (float*)malloc(n*sizeof(float));
     
      mat = (float**)malloc(n*sizeof(float*));
      for(i=0 ; i<n ; i++)
        {
          mat[i] = (float*)malloc(n*sizeof(float));
        }
    Tu utilises la valeur de n, non définie, pour réserver de la mémoire dès le début de ta fonction main() !

    Si n a une valeur aléatoire délirante ou vaut simplement 0, tu t'exposes à un plantage dès que ton programme voudra utiliser la mémoire ainsi réservée (ou pas !), d'autant plus que, dans la même veine, tu ne vérifies pas les valeurs de retour de tes malloc() !

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    329
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Octobre 2004
    Messages : 329
    Points : 608
    Points
    608
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    scanf("%f",mat[i][j]);
    scanf prend un pointeur vers la donnée, donc


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    scanf("%f",&mat[i][j]);
    Sinon si les lignes de la matrice ont toutes la même taille, il n'y a aucune raison de faire un float ** et une boucle de malloc, un seul malloc suffit.

    L'utilisation du C99 aide aussi :

    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
     
    void my_function(int n, int m, float matrice[n][m]) {
      for(int i=0; i<m;i++) {
        for(int j=0; j<m;j++) {
          // operate on matrice[i][j]
          ... ;
        }
      }
    }
     
    int main() {
     int n = ...;
     int m = ...;
     
    #ifdef MALLOC
      float *matrice=(float *) malloc(n*m*sizeof(float));
    #else
      float matrice[n][m]; // stack allocated, no free required
    #
     
      my_function(n,m,matrice);
     
    #ifdef MALLOC
      free(matrice);
    #else
     
    }

Discussions similaires

  1. Méthode Download avec le composant TNMFTP
    Par mattdef dans le forum C++Builder
    Réponses: 3
    Dernier message: 20/10/2005, 06h48
  2. Empêcher l'accès direct avec squid
    Par Amélie Ladoque dans le forum Réseau
    Réponses: 6
    Dernier message: 11/04/2005, 14h49
  3. Ecrire directement avec une autre langue???
    Par touhami dans le forum API, COM et SDKs
    Réponses: 2
    Dernier message: 21/02/2005, 21h57
  4. [LG]Listes chainées avec pointeur
    Par PaowZ dans le forum Langage
    Réponses: 2
    Dernier message: 17/02/2004, 19h49
  5. probleme avec pointeurs de structures
    Par remi77 dans le forum C
    Réponses: 2
    Dernier message: 20/10/2003, 13h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo