Tableau a deux dimensions.
2 participants
Page 1 sur 1
Tableau a deux dimensions.
Bonjour,
Ce code est-il correct ?
main.c
matrice.c
matrice.h
Merci !
Ce code est-il correct ?
main.c
- Spoiler:
- Code:
#include <stdio.h>
#include <stdlib.h>
#include "matrice.h"
int main()
{
Vie* *matrice;
createM(&matrice);
printf("Creation de la matrice\n");
initM(&matrice);
printf("Matrice innitialisée\n");
afficheM(&matrice);
printf("Matrice afficher\n");
freeM(&matrice);
printf("Matrice libre");
return 0;
}
matrice.c
- Spoiler:
- Code:
#include <stdio.h>
#include <stdlib.h>
#include "matrice.h"
void createM(Vie*** matrice)
{
int i;
*matrice = malloc(LIGNE * sizeof(Vie*));
if ( *matrice == NULL )
{
fprintf(stderr,"Allocation impossible");
exit(EXIT_FAILURE);
}
for (i = 0;i < LIGNE; i++)
{
(*matrice)[i] = malloc (COLONNE * sizeof(Vie));
if ( (*matrice)[i] == NULL )
{
fprintf(stderr,"Allocation impossible");
exit(EXIT_FAILURE);
}
}
}
void initM(Vie*** mat)
{
int i = 0, j = 0;
for (i = 0; i < LIGNE; i++)
{
for (j = 0; j < COLONNE; j ++){
(*mat)[i][j].animal = 1;
(*mat)[i][j].age = 1;
(*mat)[i][j].repas = 1;
}
}
}
void afficheM(Vie*** mat){
int i = 0, j = 0;
for (i = 0; i < LIGNE; i++)
{
for (j = 0; j < COLONNE; j ++)
{
printf("%d ", (*mat)[i][j].animal);
printf("%d ", (*mat)[i][j].age);
printf("%d // ", (*mat)[i][j].repas);
}
}
}
void freeM(Vie*** matrice){
int i = 0;
for(i = 0; i < LIGNE; i++)
free((*matrice)[i]);
free(*matrice);
}
matrice.h
- Spoiler:
- Code:
#ifndef DEF_MATRICE
#define DEF_MATRICE
#define LIGNE 5
#define COLONNE 10
typedef struct Vie Vie;
struct Vie{
int animal, age, repas;
};
void createM(Vie*** matrice);
void initM(Vie*** mat);
void freeM(Vie*** matrice);
void afficheM(Vie*** mat);
#endif
Merci !
Dernière édition par frager50 le Lun 10 Mai 2010 - 22:02, édité 7 fois
frager50- Bavard
- Messages : 17
Date d'inscription : 25/03/2009
Re: Tableau a deux dimensions.
Non.
http://www.bien-programmer.fr/notes.php#tabdyn_2d
- Code:
-------------- Build: Debug in hello ---------------
Compiling: main.c
Linking console executable: bin\Debug\hello.exe
C:\dev\hello\main.c:9: warning: function declaration isn't a prototype
C:\dev\hello\main.c: In function `main':
C:\dev\hello\main.c:10: error: syntax error before ';' token
Process terminated with status 1 (0 minutes, 1 seconds)
1 errors, 1 warnings
http://www.bien-programmer.fr/notes.php#tabdyn_2d
Re: Tableau a deux dimensions.
En fait ce code est extrait d'un plus gros projet et je ne voulais pas mettre les 36000 fichiers. C'est mieux comme sa ?
frager50- Bavard
- Messages : 17
Date d'inscription : 25/03/2009
Re: Tableau a deux dimensions.
Non, ça n'a aucune chance de fonctionner.
As-tu lu l'article dont j'ai donné la référence ? Les réponses à tes interrogations s'y trouvent ...
- Code:
-------------- Build: Debug in hello ---------------
Compiling: main.c
Linking console executable: bin\Debug\hello.exe
C:\dev\hello\main.c:9: warning: function declaration isn't a prototype
C:\dev\hello\main.c: In function `main':
C:\dev\hello\main.c:10: warning: 'matrice' might be used uninitialized in this function
obj\Debug\main.o: In function `main':
C:/dev/hello/main.c:11: undefined reference to `_createM'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 4 seconds)
1 errors, 2 warnings
As-tu lu l'article dont j'ai donné la référence ? Les réponses à tes interrogations s'y trouvent ...
Re: Tableau a deux dimensions.
Voila j'ai envoyé le projet. Oui j'ai essayer de m'appuyer sur l'article pour faire ce programme.
frager50- Bavard
- Messages : 17
Date d'inscription : 25/03/2009
Re: Tableau a deux dimensions.
Je n'ai pas le temps de tout regarder maintenant, mais déjà, il ne devrait pas y avoir de 'main.h", car main.c, par définition, n'exporte rien. La seule fonction publique est main() et elle n'a pas besoin de prototype.
En principe, un seul fichier suffit pour exposer le problème (voire 3 si on fait de la programmation modulaire).
Je regarde ça demain matin...
En principe, un seul fichier suffit pour exposer le problème (voire 3 si on fait de la programmation modulaire).
Je regarde ça demain matin...
Dernière édition par -ed- le Lun 10 Mai 2010 - 19:18, édité 1 fois
Re: Tableau a deux dimensions.
Merci beaucoup de prendre un peu de temps pour m'aider !
Pour le main.h ne je savais pas ! En plus dans mon fichier il n'y a qu'un include... je corrige sa !
Pour le main.h ne je savais pas ! En plus dans mon fichier il n'y a qu'un include... je corrige sa !
frager50- Bavard
- Messages : 17
Date d'inscription : 25/03/2009
Re: Tableau a deux dimensions.
Pour le moment, ça donne
c'est nettement plus clair et plus simple à coder ... Les fonctions createM() et freeM() pourraient utiliser ce principe. Les autres fonctions n'auraient plus alors besoin de 3 *, mais simplement de 2 ...
Autre chose qui ne va pas. Les constantes qui définissent la taille ne doivent pas se trouver dans le header (sinon, autant faire des tableaux de taille fixe), mais passées en paramètres à la fonction de création. En fait, l'idéal est de placer leurs valeurs dans une structure qui contient ce qu'il faut pour définir la matrice, à savoir :
- adresse de base
- dimensions.
ensuite, on ne fait que manipuler l'adresse de la structure.
Je te laisse revoir le code en conséquences.
On peut déjà rendre la taille autonome comme ceci :
- Code:
-------------- Build: Debug in hello ---------------
Compiling: matrice.c
Compiling: main.c
Linking console executable: bin\Debug\hello.exe
C:\dev\hello\main.c:7: warning: function declaration isn't a prototype
Output size is 21.37 KB
Process terminated with status 0 (0 minutes, 4 seconds)
0 errors, 1 warnings
- Code:
x := f()
c'est nettement plus clair et plus simple à coder ... Les fonctions createM() et freeM() pourraient utiliser ce principe. Les autres fonctions n'auraient plus alors besoin de 3 *, mais simplement de 2 ...
Autre chose qui ne va pas. Les constantes qui définissent la taille ne doivent pas se trouver dans le header (sinon, autant faire des tableaux de taille fixe), mais passées en paramètres à la fonction de création. En fait, l'idéal est de placer leurs valeurs dans une structure qui contient ce qu'il faut pour définir la matrice, à savoir :
- adresse de base
- dimensions.
ensuite, on ne fait que manipuler l'adresse de la structure.
Je te laisse revoir le code en conséquences.
On peut déjà rendre la taille autonome comme ceci :
- Spoiler:
- Code:
#include <stdio.h>
#include "matrice.h"
#define LIGNE 5
#define COLONNE 8
int main (void)
{
Vie **matrice;
createM (&matrice, LIGNE, COLONNE);
printf ("Creation de la matrice\n");
initM (&matrice, LIGNE, COLONNE);
printf ("Matrice initialisee\n");
afficheM (&matrice, LIGNE, COLONNE);
printf ("Matrice afficher\n");
freeM (&matrice, LIGNE);
printf ("Matrice libre");
return 0;
}
/* matrice.h */
#ifndef DEF_MATRICE
#define DEF_MATRICE
typedef struct
{
int animal, age, repas;
}
Vie;
void createM (Vie *** matrice, int ligne, int colonne);
void initM (Vie *** mat, int ligne, int colonne);
void freeM (Vie *** matrice, int ligne);
void afficheM (Vie *** mat, int ligne, int colonne);
#endif
/* matrice.c */
#include "matrice.h"
#include <stdlib.h>
#include <stdio.h>
void createM (Vie *** matrice, int ligne, int colonne)
{
int i;
*matrice = malloc (ligne * sizeof (Vie *));
if (*matrice == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
for (i = 0; i < ligne; i++)
{
(*matrice)[i] = malloc (colonne * sizeof (Vie));
if ((*matrice)[i] == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
}
}
void initM (Vie *** mat, int ligne, int colonne)
{
int i = 0, j = 0;
for (i = 0; i < ligne; i++)
{
for (j = 0; j < colonne; j++)
{
(*mat)[i][j].animal = 1;
(*mat)[i][j].age = 1;
(*mat)[i][j].repas = 1;
}
}
}
void afficheM (Vie *** mat, int ligne, int colonne)
{
int i = 0, j = 0;
for (i = 0; i < ligne; i++)
{
for (j = 0; j < colonne; j++)
{
printf ("%2d", (*mat)[i][j].animal);
printf ("%2d", (*mat)[i][j].age);
printf ("%2d|", (*mat)[i][j].repas);
}
printf ("\n");
}
printf ("\n");
}
void freeM (Vie *** matrice, int ligne)
{
int i = 0;
for (i = 0; i < ligne; i++)
free ((*matrice)[i]);
free (*matrice);
}
- Code:
Creation de la matrice
Matrice initialisee
1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1|
1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1|
1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1|
1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1|
1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1| 1 1 1|
Matrice afficher
Matrice libre
Process returned 0 (0x0) execution time : 0.043 s
Press any key to continue.
- Spoiler:
- Code:
/* main.c */
#include <stdio.h>
#include "matrice.h"
#define LIGNE 5
#define COLONNE 8
int main (void)
{
Vie **matrice = createM (LIGNE, COLONNE);
printf ("Creation de la matrice\n");
initM (matrice, LIGNE, COLONNE);
printf ("Matrice initialisee\n");
afficheM (matrice, LIGNE, COLONNE);
printf ("Matrice afficher\n");
matrice = freeM (matrice, LIGNE);
printf ("Matrice libre");
return 0;
}
/* matrice.h */
#ifndef DEF_MATRICE
#define DEF_MATRICE
typedef struct
{
int animal, age, repas;
}
Vie;
Vie **createM (int ligne, int colonne);
Vie ** freeM(Vie ** mat, int ligne);
void afficheM (Vie ** mat, int ligne, int colonne);
void initM (Vie ** mat, int ligne, int colonne);
#endif
/* matrice.c */
#include "matrice.h"
#include <stdlib.h>
#include <stdio.h>
Vie **createM (int ligne, int colonne)
{
int i;
Vie **matrice = malloc (ligne * sizeof (Vie *));
if (matrice == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
for (i = 0; i < ligne; i++)
{
matrice[i] = malloc (colonne * sizeof (Vie));
if (matrice[i] == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
}
return matrice;
}
void initM (Vie ** mat, int ligne, int colonne)
{
int i = 0, j = 0;
for (i = 0; i < ligne; i++)
{
for (j = 0; j < colonne; j++)
{
mat[i][j].animal = 1;
mat[i][j].age = 1;
mat[i][j].repas = 1;
}
}
}
void afficheM (Vie ** mat, int ligne, int colonne)
{
int i;
for (i = 0; i < ligne; i++)
{
int j;
for (j = 0; j < colonne; j++)
{
printf ("%2d", mat[i][j].animal);
printf ("%2d", mat[i][j].age);
printf ("%2d|", mat[i][j].repas);
}
printf ("\n");
}
printf ("\n");
}
Vie **freeM (Vie ** matrice, int ligne)
{
int i;
for (i = 0; i < ligne; i++)
free (matrice[i]);
free (matrice);
return NULL;
}
- Spoiler:
- Code:
/* main.c */
#include <stdio.h>
#include "matrice.h"
#define LIGNE 5
#define COLONNE 8
int main (void)
{
mat_s *matrice = createM (LIGNE, COLONNE);
printf ("Creation de la matrice\n");
initM (matrice);
printf ("Matrice initialisee\n");
afficheM (matrice);
printf ("Matrice afficher\n");
matrice = freeM (matrice);
printf ("Matrice libre");
return 0;
}
/* matrice.h */
#ifndef DEF_MATRICE
#define DEF_MATRICE
typedef struct
{
int animal, age, repas;
}
vie_s;
typedef struct
{
int ligne;
int colonne;
vie_s **matrice;
}
mat_s;
mat_s *createM (int ligne, int colonne);
mat_s *freeM (mat_s * mat);
void afficheM (mat_s const *mat);
void initM (mat_s * mat);
#endif
/* matrice.c */
#include "matrice.h"
#include <stdlib.h>
#include <stdio.h>
mat_s *createM (int ligne, int colonne)
{
int i;
mat_s *mat = malloc (sizeof *mat);
if (mat != NULL)
{
mat->matrice = malloc (ligne * sizeof *mat->matrice);
if (mat->matrice == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
for (i = 0; i < ligne; i++)
{
mat->matrice[i] = malloc (colonne * sizeof *mat->matrice[i]);
if (mat->matrice[i] == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
}
/* ne pas oublier de sauvegarder les caracteristiques de la matrice ... */
mat->ligne = ligne;
mat->colonne = colonne;
}
else
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
return mat;
}
void initM (mat_s * mat)
{
if (mat != NULL)
{
if (mat->matrice != NULL)
{
int i;
for (i = 0; i < mat->ligne; i++)
{
int j;
for (j = 0; j < mat->colonne; j++)
{
mat->matrice[i][j].animal = 1;
mat->matrice[i][j].age = 1;
mat->matrice[i][j].repas = 1;
}
}
}
}
}
void afficheM (mat_s const *mat)
{
if (mat != NULL)
{
if (mat->matrice != NULL)
{
int i;
for (i = 0; i < mat->ligne; i++)
{
int j;
for (j = 0; j < mat->colonne; j++)
{
printf ("%2d", mat->matrice[i][j].animal);
printf ("%2d", mat->matrice[i][j].age);
printf ("%2d|", mat->matrice[i][j].repas);
}
printf ("\n");
}
printf ("\n");
}
}
}
mat_s *freeM (mat_s * mat)
{
if (mat != NULL)
{
if (mat->matrice != NULL)
{
int i;
for (i = 0; i < mat->ligne; i++)
free (mat->matrice[i]);
free (mat->matrice);
}
free (mat);
}
return NULL;
}
Dernière édition par -ed- le Mar 11 Mai 2010 - 20:32, édité 3 fois
Re: Tableau a deux dimensions.
Merci d'avoir pris un peu de temps pour me répondre ! C'est vrai qu'il y a beaucoup d'étoiles mais en fait c'est un petit projet pour m'entrainer a manipuler les pointeurs.
C'est vrai que pour les lignes et les colonnes le but était que l'utilisateur choisisse la taille du tableau donc c'est mieux ainsi !
Merci beaucoup !
C'est vrai que pour les lignes et les colonnes le but était que l'utilisateur choisisse la taille du tableau donc c'est mieux ainsi !
Merci beaucoup !
frager50- Bavard
- Messages : 17
Date d'inscription : 25/03/2009
Re: Tableau a deux dimensions.
J'ai complété mes réponses j'espère que tu as compris le cheminement vers le code final. C'est essentiel, car ce principe est très utilisé...
On peut même aller plus loin en rendant la structure 'opaque' :
On peut même aller plus loin en rendant la structure 'opaque' :
- Spoiler:
- Code:
/* matrice.h */
#ifndef DEF_MATRICE
#define DEF_MATRICE
/* structure opaque. L'interieur est invisibe d'ici */
typedef struct mat mat_s;
mat_s *createM (int ligne, int colonne);
mat_s *freeM (mat_s * mat);
void afficheM (mat_s const *mat);
void initM (mat_s * mat);
#endif
/* matrice.c */
#include "matrice.h"
#include <stdlib.h>
#include <stdio.h>
/* structures opaques. Le contenu est invisible de l'exterieur ... */
struct vie
{
int animal, age, repas;
};
struct mat
{
int ligne;
int colonne;
struct vie **matrice;
};
mat_s *createM (int ligne, int colonne)
{
int i;
mat_s *mat = malloc (sizeof *mat);
if (mat != NULL)
{
mat->matrice = malloc (ligne * sizeof *mat->matrice);
if (mat->matrice == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
for (i = 0; i < ligne; i++)
{
mat->matrice[i] = malloc (colonne * sizeof *mat->matrice[i]);
if (mat->matrice[i] == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
}
/* ne pas oublier de sauvegarder les caracteristiques de la matrice ... */
mat->ligne = ligne;
mat->colonne = colonne;
}
else
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
return mat;
}
void initM (mat_s * mat)
{
if (mat != NULL)
{
if (mat->matrice != NULL)
{
int i;
for (i = 0; i < mat->ligne; i++)
{
int j;
for (j = 0; j < mat->colonne; j++)
{
mat->matrice[i][j].animal = 1;
mat->matrice[i][j].age = 1;
mat->matrice[i][j].repas = 1;
}
}
}
}
}
void afficheM (mat_s const *mat)
{
if (mat != NULL)
{
if (mat->matrice != NULL)
{
int i;
for (i = 0; i < mat->ligne; i++)
{
int j;
for (j = 0; j < mat->colonne; j++)
{
printf ("%2d", mat->matrice[i][j].animal);
printf ("%2d", mat->matrice[i][j].age);
printf ("%2d|", mat->matrice[i][j].repas);
}
printf ("\n");
}
printf ("\n");
}
}
}
mat_s *freeM (mat_s * mat)
{
if (mat != NULL)
{
if (mat->matrice != NULL)
{
int i;
for (i = 0; i < mat->ligne; i++)
free (mat->matrice[i]);
free (mat->matrice);
}
free (mat);
}
return NULL;
}
Re: Tableau a deux dimensions.
C'est un peu comme la programmation orientée objet , non ?
frager50- Bavard
- Messages : 17
Date d'inscription : 25/03/2009
Re: Tableau a deux dimensions.
Presque. C'est le principe de base de la POO (l'encapsulation et la protection des données)
Re: Tableau a deux dimensions.
J'ai un nouveau soucis : quand je demande des "grandes" matrice la mémoire ne se libère pas correctement......
Le code est un peu modifier et j'ai pris les valeurs de ligne et colonnes arbitrairement pour l'exemple :
Le programme plante dans la fonction freeM.
Le code est un peu modifier et j'ai pris les valeurs de ligne et colonnes arbitrairement pour l'exemple :
- Spoiler:
main.c :- Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "matrice.h"
#include "lecture.c"
int main()
{
Vie* *matrice;
int ligne, colonne, poissons, requins;
srand(time(NULL));
init(&ligne, &colonne, &poissons, &requins);/*On demande les paramètres*/
ligne = 20, colonne = 20;
createM(&matrice, ligne, colonne);/*On créer la matrice*/
printf("Creation de la matrice\n");
initM(&matrice, ligne, colonne);/*Innitialisation de la matrice */
printf("Matrice innitialisee\n");
Crequins(matrice, ligne, colonne, requins);
Cpoissons(matrice, ligne, colonne, poissons);
afficheM(matrice, ligne, colonne);
printf("Matrice afficher\n");
freeM(&matrice, ligne, colonne);
printf("Matrice libre\n");
getchar();
return 0;
}
matrice.c- Code:
#include <stdio.h>
#include <stdlib.h>
#include "matrice.h"
#include "lecture.h"
void createM(Vie*** matrice, int ligne, int colonne){
int i;
*matrice = malloc(ligne * sizeof(Vie*));
if ( *matrice == NULL )
{
fprintf(stderr,"Allocation impossible");
exit(EXIT_FAILURE);
}
for (i = 0;i < ligne; i++)
{
(*matrice)[i] = malloc (COLONNE * sizeof(Vie));
if ( (*matrice)[i] == NULL )
{
fprintf(stderr,"Allocation impossible");
exit(EXIT_FAILURE);
}
}
}
void initM(Vie*** mat, int ligne, int colonne){
int j, i;
for (i = 0; i < ligne; i++)
{
for (j = 0; j < colonne; j ++){
(*mat)[i][j].animal = 0;
(*mat)[i][j].age = 0;
(*mat)[i][j].repas = 0;
}
}
}
void afficheM(Vie** mat, int ligne, int colonne){
int i = 0, j = 0;
for (i = 0; i < ligne; i++)
{
for (j = 0; j < colonne; j ++)
{
switch (mat[i][j].animal){
case 1:
printf(".");
break;
case 2:
printf("0");
break;
default :
printf(" ");
break;
}
}
printf("\n");
}
}
void freeM(Vie*** matrice, int ligne, int colonne){
int i = 0;
printf("\nLiberation en cours\n");
for(i = 0; i < ligne; i++)
free((*matrice)[i]);
free(*matrice);
}
void init(int* ligne, int* colonne, int* poissons, int* requins){
/*printf("Nombres de ligne : ");
*ligne = lireLong();
printf("\nNombres de colonne : ");
*colonne = lireLong();
*/
printf("\nNombres de poissons : ");
*poissons = lireLong();
printf("\nNombres de requins : ");
*requins = lireLong();
}
void Crequins(Vie** mat, int ligne, int colonne, int requins){
int l, c;
while(requins){
l = hasard(0, (ligne-1));
c = hasard(0, (colonne-1));
if(mat[l][c].animal == 0){
mat[l][c].animal = 2;
requins--;
}
}
}
void Cpoissons(Vie** mat, int ligne, int colonne, int poissons){
int l, c;
while(poissons){
l = hasard(0, (ligne-1));
c = hasard(0, (colonne-1));
if(mat[l][c].animal == 0){
mat[l][c].animal = 1;
poissons--;
}
}
}
matrice.h :- Code:
#ifndef DEF_MATRICE
#define DEF_MATRICE
#define LIGNE 5
#define COLONNE 10
#define hasard(MIN, MAX) (rand() % (MAX - MIN + 1)) + MIN;
typedef struct Vie Vie;
struct Vie{
int animal, age, repas;
};
void init(int* ligne, int* colonne, int* poissons, int* requins);
void createM(Vie*** matrice, int ligne, int colonne);
void initM(Vie*** mat, int ligne, int colonne);
void freeM(Vie*** matrice, int ligne, int colonne);
void afficheM(Vie** mat, int ligne, int colonne);
void Crequins(Vie** mat, int ligne, int colonne, int requins);
void Cpoissons(Vie** mat, int ligne, int colonne, int poissons);
#endif
Le programme plante dans la fonction freeM.
frager50- Bavard
- Messages : 17
Date d'inscription : 25/03/2009
Re: Tableau a deux dimensions.
Qu'est-ce que "lecture.h" et surtout, qu'est-ce que "lecture.c" et pourquoi est-il inclus ? Même si c'est techniquement possible, on inclue pas un .c
OK; j'ai utilisé mes propres fonctions :
http://www.bien-programmer.fr/clib/ed/inc/io.h
http://www.bien-programmer.fr/clib/ed/inc/io.c
Ceci fonctionne (testé avec 200 x 200, 100 poissons et 20 requins):
OK; j'ai utilisé mes propres fonctions :
http://www.bien-programmer.fr/clib/ed/inc/io.h
http://www.bien-programmer.fr/clib/ed/inc/io.c
Ceci fonctionne (testé avec 200 x 200, 100 poissons et 20 requins):
- Spoiler:
- Code:
/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "matrice.h"
#include "lecture.h" /* -ed- etait "*.c" ! */
int main (void)
{
Vie **matrice;
int ligne, colonne, poissons, requins;
srand (time (NULL));
init (&poissons, &requins); /*On demande les paramètres */
ligne = 200, colonne = 200;
createM (&matrice, ligne, colonne); /*On créer la matrice */
printf ("Creation de la matrice\n");
initM (&matrice, ligne, colonne); /*Initialisation de la matrice */
printf ("Matrice initialisee\n");
Crequins (matrice, ligne, colonne, requins);
Cpoissons (matrice, ligne, colonne, poissons);
afficheM (matrice, ligne, colonne);
printf ("Matrice afficher\n");
freeM (&matrice, ligne);
printf ("Matrice libre\n");
return 0;
}
- Code:
/* matrice.c */
#include <stdio.h>
#include <stdlib.h>
#include "matrice.h"
#include "lecture.h"
void createM (Vie *** matrice, int ligne, int colonne)
{
int i;
*matrice = malloc (ligne * sizeof (Vie *));
if (*matrice == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
for (i = 0; i < ligne; i++)
{
/* -ed- attention, il y avait 'COLONNE' que j'avais supprime ... */
(*matrice)[i] = malloc (colonne * sizeof (Vie));
if ((*matrice)[i] == NULL)
{
fprintf (stderr, "Allocation impossible");
exit (EXIT_FAILURE);
}
}
}
void initM (Vie *** mat, int ligne, int colonne)
{
int j, i;
for (i = 0; i < ligne; i++)
{
for (j = 0; j < colonne; j++)
{
(*mat)[i][j].animal = 0;
(*mat)[i][j].age = 0;
(*mat)[i][j].repas = 0;
}
}
}
void afficheM (Vie ** mat, int ligne, int colonne)
{
int i = 0, j = 0;
for (i = 0; i < ligne; i++)
{
for (j = 0; j < colonne; j++)
{
switch (mat[i][j].animal)
{
case 1:
printf (".");
break;
case 2:
printf ("0");
break;
default:
printf (" ");
break;
}
}
printf ("\n");
}
}
void freeM (Vie *** matrice, int ligne)
{
int i = 0;
printf ("\nLiberation en cours\n");
for (i = 0; i < ligne; i++)
free ((*matrice)[i]);
free (*matrice);
}
void init (int *poissons, int *requins)
{
printf ("\nNombres de poissons : ");
*poissons = lireLong ();
printf ("\nNombres de requins : ");
*requins = lireLong ();
}
void Crequins (Vie ** mat, int ligne, int colonne, int requins)
{
int l, c;
while (requins)
{
l = hasard (0, (ligne - 1));
c = hasard (0, (colonne - 1));
if (mat[l][c].animal == 0)
{
mat[l][c].animal = 2;
requins--;
}
}
}
void Cpoissons (Vie ** mat, int ligne, int colonne, int poissons)
{
int l, c;
while (poissons)
{
l = hasard (0, (ligne - 1));
c = hasard (0, (colonne - 1));
if (mat[l][c].animal == 0)
{
mat[l][c].animal = 1;
poissons--;
}
}
}
- Code:
/* matrice.h */
#ifndef DEF_MATRICE
#define DEF_MATRICE
/* -ed- deplacement de LIGNE et COLONNE qui n'ont rien a faire ici. Ils
appartiennent a main.c et sont passees aux fonctions. */
/* -ed- suppression du ';'. Bien que ce soit techniquement possible, on ne met
pas de ';' dans une macro. On reserve ca au code source en C. */
#define hasard(MIN, MAX) (rand() % (MAX - MIN + 1)) + MIN
typedef struct Vie Vie;
struct Vie
{
int animal, age, repas;
};
void init (int *poissons, int *requins);
void createM (Vie *** matrice, int ligne, int colonne);
void initM (Vie *** mat, int ligne, int colonne);
/* -ed- supression de 'colonne'. Pourquoi l'avoir rajouté ? */
void freeM (Vie *** matrice, int ligne);
void afficheM (Vie ** mat, int ligne, int colonne);
void Crequins (Vie ** mat, int ligne, int colonne, int requins);
void Cpoissons (Vie ** mat, int ligne, int colonne, int poissons);
#endif
- Code:
#ifndef H_ED_LECTURE_20100516144542
#define H_ED_LECTURE_20100516144542
#ifdef __cplusplus
extern "C"
{
#endif
/* lecture.h */
long lireLong (void);
#ifdef __cplusplus
}
#endif
#endif /* guard */
/* Guards added by GUARD (c) ED 2000-2005 Jan 17 2005 Ver. 1.7 */
- Code:
/* lecture.c */
#include "lecture.h"
#include "ed/inc/io.h"
long lireLong (void)
{
return get_l (NULL);
}
Re: Tableau a deux dimensions.
Euh oui c'était juste un test. Au final je m'en suis pas servis...
Le code marche....mais je ne comprends pas pourquoi cela plantais cette après-midi......
Le code marche....mais je ne comprends pas pourquoi cela plantais cette après-midi......
frager50- Bavard
- Messages : 17
Date d'inscription : 25/03/2009
Re: Tableau a deux dimensions.
C'est parce que tu avais laissé un 'COLONNE' en 'dur' qui n'avait pas la bonne valeur, d'où un débordement ...
Re: Tableau a deux dimensions.
Ok !!! J'ai passé du temps a chercher l'erreur pourtant.....
Merci pour ton aide précieuse !
Merci pour ton aide précieuse !
frager50- Bavard
- Messages : 17
Date d'inscription : 25/03/2009
Re: Tableau a deux dimensions.
... et j'avais fourni du code qui évitait ce genre de problème "par construction". Ce qu'on appelle la "programmation défensive". On peut s'en passer mais preuve est faite que c'est à ses risques et périls ...
Sujets similaires
» structure et tableau
» Deux exos
» lecture tableau texte 2D erreur de segmentation
» comparaison de deux chaines de caracteres
» Deux exos
» lecture tableau texte 2D erreur de segmentation
» comparaison de deux chaines de caracteres
Page 1 sur 1
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum