Thread: problem_with_pointers_and_tables

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    5

    Angry problem_with_pointers_and_tables

    the program's objectif is to fill up a first table randemly
    search for a number (in 2 methodes) sort the numbers (2 methodes)
    fill a seconde tables, sort it
    then merge the 2, sorted
    here is my program(i have a problem with the first methode of sortin "bulle", but the sconde one works, also i have a problem with the merging because the compiler shuts down after it), plz help

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define bs 100
    
    void remplissage_al(int* t, int n)
    {
        int i;
        for(i=0;i<n;i++)
        *(t++)=rand()%bs;
    }
    void remplissage_man(int* t, int n)
    {
        int i;
        for(i=0;i<n;i++)
        {printf("donnez un element : ");
         scanf("%d",t++);}
    }
    void affichage(int* t,int n)
    {
        int i;
        printf("\n");
        for(i=0;i<n;i++)
        printf("%d\t",*t++);
        printf("\n");
    }
    int recherchesq(int *t,int n,int x)
    {
        int i;
        for(i=0;i<n;i++)
        if(*(t++)==x) return 1;
        return 0;
    }
    void bulle(int* t,int n)
    {
        int i,j,a;
        for(j=0;j<=n-1;j++)
        for(i=n-1;i>j;i--)
        if(t[i]<t[i-1])
        {
            a=t[i-1];
            t[i-1]=t[i];
            t[i]=a;
        }
    }
    void insert(int* t,int i,int g)
    {
        int j,aux;
        j=i-1;
        aux=t[i];
        while((j>=i+1)&&(t[j]>aux))
        {
            t[j+1]=t[j];
            j--;
        }
        if(t[g]>aux){i=g-1; t[g+1]=t[g];}
        t[j+1]=aux;
    }
    void tri_insert(int* t,int n)
    {
        int i,g=0;
        for(i=g+1;i<=n;i++)
        insert(t,i,g);
    }
    int recherchedc(int* t,int b,int x)
    {
        int m,a=0;
        while(a!=b)
        { m=(a+b)/2;
          if(x>t[m]) a=m+1;
          else b=m;
        }
        if(t[a]==x) return 1;
        else return 0;}
    void fusion(int *t1,int n1,int *t2,int n2)
    {
        int *t3,n3,i,j,k;
        i=j=k=0;
        n3=n1+n2;
        t3=(int*)malloc(n3*sizeof(int));
        while((i<n1)&&(j<n2))
        {
            if(t1[i]<t2[j]) {t3[k++]=t1[i++];}
            else {t3[k++]=t2[j++];}
        }
        while(i<n1) {t3[k++]=t1[i++];}
        while(j<n1) {t3[k++]=t2[j++];}
        affichage(t3,n3);
    }
    int main()
    {
        int *t1,*t2,n1,n2,x,choix,b;
        do{
        b=0;
        //******************************************************   aleatoire
        printf("entrez la dimension du tableau aleatoire : ");
        scanf("%d",&n1);
        t1=(int*)malloc(n1*sizeof(int));
        remplissage_al(t1,n1);
        affichage(t1,n1);
            //******************************************************* recher sq
        printf("\nentrez l'element a rechercher (recherche sequentielle) : ");
        scanf("%d",&x);
        if(recherchesq(t1,n1,x)==1) printf("%d existe\n",x);
        else printf("%d n'existe pas\n",x);
        //******************************************************  tri
        printf("\nchoisissez la methode pour tier la table : \n1 pour methode buule\n2 pour methode insetion : ");
        do{
        scanf("%d",&choix);
        switch(choix)
        {
            case 1:tri_insert(t1,n1);affichage(t1,n1);break;
            case 2:bulle(t1,n1);affichage(t1,n1);break;
            default : printf("choix invalide ");break;
        }}while((choix!=1)&&(choix!=2));
        //****************************************************** recher dicho
        printf("entrez l'element a rechercher (recherche dichotomique): ");
        scanf("%d",&x);
        if(recherchedc(t1,n1,x)==1) printf("%d existe",x);
        else printf("%d n'existe pas",x);
        //*******************************************************  manu
        printf("\n");
        printf("\nentrez la dimension du tableau a remplir : ");
        scanf("%d",&n2);
        t2=(int*)malloc(n2*sizeof(int));
        remplissage_man(t2,n2);
        affichage(t2,n2);
        //******************************************************** trier 2eme table
        printf("\nchoisissez la methode pour tier la 2eme table : \n1 pour methode buule\n2 pour methode insetion : ");
        do{
        scanf("%d",&choix);
        switch(choix)
        {
            case 1:tri_insert(t2,n2);affichage(t2,n2);break;
            case 2:bulle(t2,n2);affichage(t2,n2);break;
            default : printf("choix invalide ");break;
        }}while((choix!=1)&&(choix!=2));
        //******************************************************* fusion
        printf("si vous voulez fusioner les 2 tables tapez 1\nsi non tapez 2   :   ");scanf("%d",&choix);
        if(choix==1)
        fusion(t1,n1,t2,n2);
        else return 0;
        printf("\ntappez \n1 : pour une autre tentation\n2 : pour quittez");
        }while(b==1);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your menu selections are backwards so when you think you're selecting bubble sort you're actually selecting insertion sort. It's actually your insertion sort, tri_insert, that is buggy. Note for instance that g will always be 0.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    5
    thank you a lot you've helped me tremendously, this mistake was misleading me. i'll try to fix it and move on to the other error.

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    5
    I have fixed the problem with the sorting thanks to "algorism"
    now all that is left is to figure out why the .exe crashes
    see it gives the the wanted result but then crashes, plz if anyone knows why tell me.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define bs 100
    
    void remplissage_al(int* t, int n)
    {
        int i;
        for(i=0;i<n;i++)
        *(t++)=rand()%bs;
    }
    void remplissage_man(int* t, int n)
    {
        int i;
        for(i=0;i<n;i++)
        {printf("donnez un element : ");
         scanf("%d",t++);}
    }
    void affichage(int* t,int n)
    {
        int i;
        printf("\n");
        for(i=0;i<n;i++)
        printf("%d\t",*t++);
        printf("\n");
    }
    int recherchesq(int *t,int n,int x)
    {
        int i;
        for(i=0;i<n;i++)
        if(*(t++)==x) return 1;
        return 0;
    }
    void permuter(int* a,int* b)
    {
        int c;
        c=*a;
        *a=*b;
        *b=c;
    }
    void bulle(int* t,int n)
    {
        int i,j;
        for(j=0;j<=n-1;j++)
        for(i=n-1;i>j;i--)
        if(t[i]<t[i-1]) permuter(&t[i],&t[i-1]);
    }
    void insert(int* T,int D,int G)
    {
        int i,aux;
        i=D-1;
        aux=T[D];
        while((i>=G+1)&&(T[i]>aux)) // probleme ici
        {T[i+1]=T[i];
        i--;}
        if(T[G]>aux)
        {i=G-1;
        T[G+1]=T[G];
        }
    T[i+1]=aux;// probleme ici
    }
    void tri_insert(int* t,int g,int n)
    {
        int i;
        for(i=g+1;i<=n;i++)
        insert(t,i,g);
    }
    int recherchedc(int* t,int b,int x)
    {
        int m,a=0;
        while(a!=b)
        { m=(a+b)/2;
          if(x>t[m]) a=m+1;
          else b=m;
        }
        if(t[a]==x) return 1;
        else return 0;}
    void fusion(int *t1,int n1,int *t2,int n2)
    {
        int *t3,n3,i,j,k;
        i=j=k=0;
        n3=n1+n2;
        t3=(int*)malloc(n3*sizeof(int));
        while((i<n1)&&(j<n2))
        {
            if(t1[i]<t2[j]) {t3[k]=t1[i];k++;i++;}
            else {t3[k]=t2[j];k++;j++;}
        }
        while(i<n1) {t3[k]=t1[i];k++;i++;}
        while(j<n1) {t3[k]=t2[j];k++;j++;}
        affichage(t3,n3);
    }
    int main()
    {
        int *t1,*t2,n1,n2,x,choix,b;
        do{
        b=0;
        //******************************************************   aleatoire
        printf("entrez la dimension du tableau aleatoire : ");
        scanf("%d",&n1);
        t1=(int*)malloc(n1*sizeof(int));
        remplissage_al(t1,n1);
        affichage(t1,n1);
            //******************************************************* recher sq
        printf("\nentrez l'element a rechercher (recherche sequentielle) : ");
        scanf("%d",&x);
        if(recherchesq(t1,n1,x)==1) printf("%d existe\n",x);
        else printf("%d n'existe pas\n",x);
        //******************************************************  tri
        printf("\nchoisissez la methode pour tier la table : \n1 pour methode bulle\n2 pour methode insetion : ");
        do{
        scanf("%d",&choix);
        switch(choix)
        {
            case 1:bulle(t1,n1);affichage(t1,n1);break;
            case 2:tri_insert(t1,0,n1-1);affichage(t1,n1);break;
            default : printf("choix invalide ");break;
        }}while((choix!=1)&&(choix!=2));
        //****************************************************** recher dicho
        printf("entrez l'element a rechercher (recherche dichotomique): ");
        scanf("%d",&x);
        if(recherchedc(t1,n1,x)==1) printf("%d existe",x);
        else printf("%d n'existe pas",x);
        //*******************************************************  manu
        printf("\n");
        printf("\nentrez la dimension du tableau a remplir : ");
        scanf("%d",&n2);
        t2=(int*)malloc(n2*sizeof(int));
        remplissage_man(t2,n2);
        affichage(t2,n2);
        //******************************************************** trier 2eme table
        printf("\nchoisissez la methode pour tier la 2eme table : \n1 pour methode bulle\n2 pour methode insetion : ");
        do{
        scanf("%d",&choix);
        switch(choix)
        {
            case 1:bulle(t2,n2);affichage(t2,n2);break;
            case 2:tri_insert(t2,0,n2-1);affichage(t2,n2);break;
            default : printf("choix invalide ");break;
        }}while((choix!=1)&&(choix!=2));
        //******************************************************* fusion
        printf("si vous voulez fusioner les 2 tables tapez 1\nsi non tapez 2   :   ");scanf("%d",&choix);
        if(choix==1) {fusion(t1,n1,t2,n2);}
        else return 0;
        printf("\ntappez \n1 : pour une autre tentation\n2 : pour quittez");
        }while(b==1);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your insertion sort is till wrong. It has multiple errors.
    g makes no sense at all. Get rid of it.
    And there's no need to split it into two functions.
    You're making it seem more complicated than bubble sort but it's at least as simple.
    I suggest you delete tri_insert and insert and start fresh.

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    5
    tri_insert it's a methode I learnt at school (I'm still a student), and i have to use it (it worked).
    I'll try to simplify it more.
    if you'd be so kind to tell me why does the .exe crashes

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I doubt they taught insertion sort like that at your school!

    In order to call your insertion sort you need to pass a meaningless 0 and one-less-than the size of the array. That's a crazy interface. The call should be a simple tri_insert(t1, n1).

    And look at g. It's initialized to 0 when tri_insert is called. It's never changed in tri_insert and is always passed as 0 to insert. insert also never changes g. g is essentially a constant 0. If the if(T[G]>aux) branch is ever taken (perhaps it never is) then you access T[-1].

    Anyway, your crash is happening in fusion because you have (j<n1) where it should be (j<n2).
    Strange merge routine that doesn't pass the merged data back to the caller!

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    5
    thanks, it solved fusion
    to explain why g is needed: you need to know that the intial purpose of tri_isert is to sort a portion of the table not all of it,
    you should actually give the left (start) (g stands for left in french gauche) and right (second to last)(d stands for right in french droit) coordonations of the table that you want sorted.
    in my case wich is a particularity (i need all of sorted). g=0 and d=n-1;
    i think it's a classic way; https://openclassrooms.com/courses/le-tri-par-insertion that's a link for it but it's done diffrently.
    and for fusion it's purpose is to give the merged tables sorted and with little time possible.
    anyway thanks algorism for your time, i'm not used to long codes, and i got so confused.
    Last edited by fawaz_bouliste; 03-06-2016 at 01:06 PM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread