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;
}