Problème dans 2 exo

Résolu/Fermé
man r. Messages postés 11 Date d'inscription samedi 30 mars 2013 Statut Membre Dernière intervention 1 juin 2013 - 25 mai 2013 à 13:41
man r. Messages postés 11 Date d'inscription samedi 30 mars 2013 Statut Membre Dernière intervention 1 juin 2013 - 28 mai 2013 à 00:50
comme ceci est indiqué dans le titre, j'ai des problèmes dans deux programmes, ils se compilent mais ne marchent pas :/ ! ceci dit, voilà les 2 programmes :
triangle de pascale
void allouer(int **t,int dim);
void remplir(int **t,int dim);
void afficher(int **t,int dim);

int main(){
    int **t,i,degre,j;
    printf("la dimension du triangle\n");
    scanf("%d",&degre);
    allouer(t,degre);
    remplir(t,degre);
    afficher(t,degre);
    system("pause");
   return 0;
}
    void allouer(int **t,int dim){
         int i;
    t=(int**)malloc(dim*sizeof(int*));
    for(i=0;i<dim;i++) t[i]=(int*)malloc((i+1)*sizeof(int));
    }
    void remplir (int **t,int dim){
         int i,j;
    for(i=0;i<dim;i++){
         for(j=0;j<dim;j++){
             if (i==j) t[i][j]=1;
             else if (j==0) t[i][j]=1;
             else if (i>j) t[i][j]=t[i-1][j-1]+t[i-1][j];
             }}}
    void afficher(int **t,int dim){
         int i,j;
    for(i=0;i<dim;i++){
         for(j=0;j<=i;j++){
              printf("%d",t[i][j]);
         }
         printf("\n");
   }}

vecteur
void initialiser(int *t, int nbr, int x);
void afficher(int *t,int nbr);
void inserer(int *t,int nbr,int a, int pos);
void supprimer(int *t,int nbr,int a, int pos);
void chercherprmierepos(int *t, int nbr, int c);
void chercherdrnierepos(int *t, int nbr, int c);
void cherchernpos (int *t, int nbr, int c, int n);
void triercroissant (int *t, int nbr);
void trierdecroissant (int *t, int nbr);

int main(){
    int *t,nbr,x,n,d,a,pos,i;
    printf("la dimension du tableau\n");
    scanf("%d",&nbr);
    t=(int*)malloc(nbr*sizeof(int));
    printf("x = \n");
    scanf("%d",&x);
    initialiser(t,nbr,x);
    afficher(t,nbr);
    printf("\n");
    printf("les elements du vecteur\n");
    for(i=0;i<nbr;i++) scanf("%d",t+i);
    afficher(t,nbr);
    printf("le nombre a inserer\n");
    scanf("%d",&a);
    printf("la position ou l'inserer\n");
    scanf("%d",&pos);
    inserer(t,nbr,a,pos);
    for(i=0;i<=nbr;i++) printf("%d ",t[i]);
    printf("\n");
    printf("suppression de %d\n",a);
    supprimer(t,nbr,a,pos);
    for(i=0;i<nbr;i++) printf("%d ",t[i]);
    printf("\n");
    printf("d=\n");
    scanf("%d",&d);
    printf("la premiere position de %d\n",d);
    chercherprmierepos(t, nbr, d);
    printf("\n");
    printf("d=\n");
    scanf("%d",&d);
    printf("la derniere position de %d\n",d);
    chercherdrnierepos(t, nbr, d);
    printf("\n");
    printf("d=\n");
    scanf("%d",&d);
    printf("la position\n");
    scanf("%d",&n);
    cherchernpos (t,nbr,d,n);
    printf("\n");
    printf("trier le vecteur en ordre croissant\n");
    triercroissant (t, nbr);
    afficher(t,nbr);
    printf("trier le vecteur en ordre decroissant\n");
    trierdecroissant (t, nbr);
    afficher(t,nbr);


    system("pause");
    return 0;
}

void initialiser(int *t, int nbr, int x){
     int i;
    for(i=0;i<nbr;i++) t[i]=x;
}
void afficher(int *t,int nbr){
     int i;
    for(i=0;i<nbr;i++) printf("%d ",t[i]);
}
void inserer(int *t,int nbr,int a, int pos){
    t[nbr]=t[pos];
    t[pos]=a;
}
void supprimer(int *t,int nbr,int a, int pos){
    t[pos]=t[nbr];
}
void chercherprmierepos(int *t, int nbr, int c){
     int i;
     for(i=0;i<nbr;i++){
          if(c==t[i]) {printf("%d",i); break;}
     }
}
void chercherdrnierepos(int *t, int nbr, int c){
     int i;
     for(i=nbr-1;i>=0;i++){
          if(c==t[i]) {printf("%d",i); break;}
     }
}
void cherchernpos (int *t, int nbr, int c, int n){
     int i,k=0;
     for(i=0;(i<nbr)&&(k<n);i++){
          if (c==t[i]) k++;
     }
     if (k>=n) printf("%d",i);
     else printf("cet element n'existe pas\n");
}
void triercroissant (int *t, int nbr){
     int aide,i,j;
     if (t[i]<t[j]){
          for(i=0;i<nbr;i++){
               for(j=i;j<nbr;j++){
                   aide=t[i];
                   t[i]=t[j];
                   t[j]=aide;
               }
          }
     }
}
void trierdecroissant (int *t, int nbr){
     int aide,i,j;
     if (t[i]>t[j]){
          for(i=0;i<nbr;i++){
               for(j=i;j<nbr;j++){
                   aide=t[i];
                   t[i]=t[j];
                   t[j]=aide;
               }
          }
     }
}


Merci d'avance pour votre aide :-) !
A voir également:

4 réponses

man r. Messages postés 11 Date d'inscription samedi 30 mars 2013 Statut Membre Dernière intervention 1 juin 2013
25 mai 2013 à 20:50
???
0
fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 1 835
25 mai 2013 à 22:16
Je n'ai regardé que le premier exo.
Il faut passer l'adresse de la variable pour pouvoir modifier sa valeur.

Dans main() -> allouer(&t,degre);
Dans allouer() :
void allouer(int ***t,int dim){
int i;
*t=(int**)malloc(dim*sizeof(int*));
for(i=0;i<dim;i++) (*t)[i]=(int*)malloc((i+1)*sizeof(int));
}

N'oublie pas de modifier le prototype.

Cdlt,
0
man r. Messages postés 11 Date d'inscription samedi 30 mars 2013 Statut Membre Dernière intervention 1 juin 2013
26 mai 2013 à 01:51
merci infiniment, y a pas moyen que tu me dises ce qui va pas dans le second exo (a) ?
0
fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 1 835
26 mai 2013 à 11:39
Dans tes deux codes, tu n'as pas mis #include <stdio.h> et #include <stdlib.h>.

void supprimer(int *t,int nbr,int a, int pos){
t[pos]=t[nbr];
}

A quoi sert la variable a si tu t'en sers pas ?

void triercroissant (int *t, int nbr){
int aide,i,j;
if (t[i]<t[j]){

Attention i et j sont non initialisés à ce moment-là.

void trierdecroissant (int *t, int nbr){
int aide,i,j;
if (t[i]>t[j]){

Idem.

void inserer(int *t,int nbr,int a, int pos){
t[nbr]=t[pos];
t[pos]=a;
}

L'indice nbr n'appartient pas au tableau. L'indice maximum (tel que défini avec ton malloc) est nbr-1. Donc, ça risque de planter. Il faut que tu agrandisses ton tableau avec realloc().
De plus, tu n'insères pas l'élément ici. Tu mets l'élément t[pos] en fin de tableau et l'élément nouveau en indice pos. Insérer consiste à tout décaler vers la droite (boucle for).
Sinon, ton main n'est pas propre. Tu utilises des fonctions pour faire des traitements (très bien) et parfois tu mets les traitements dans le main. Utilise plutôt des fonctions ;-)
0
man r. Messages postés 11 Date d'inscription samedi 30 mars 2013 Statut Membre Dernière intervention 1 juin 2013
28 mai 2013 à 00:50
d'accord, je vous remercie une seconde fois :) !
0