void triFichier(produit *tabProduit, int sens, int j) { int permut; permut = 1; int i; while (permut ==1) { permut = 0; for(i =0; i<j-1;i++) { if(sens==1) { if(tabProduit[i].num_produit > tabProduit[i+1].num_produit) { echange(tabProduit, i); permut = 1; } } else if (sens == 2) { if(tabProduit[i].num_produit < tabProduit[i+1].num_produit) { echange(tabProduit, i); permut = 1; } } } } }
/* Étape élémentaire du tri rapide : découpe le bloc b en 0, 1 ou 2 blocs * Dans le cas normal, découpe en 2 blocs, les éléments inférieurs au * pivot, et ceux supérieurs au pivot * Si un bloc contient moins de 1 élément, il n’est pas retourné */ int rapide_decoupebloc(bloc_t b, bloc_t bret[2]) { pos_t g, d; base_t pivot, tmp; bloc_t b1, b2; int nb_ret = 0; if(b.debut >= b.fin) { /* Arrive uniquement dans le cas d’un tri d’un tableau de * taille 1 au départ */ assert (b.debut == b.fin); return 0; } /* Définit une petite macro pour échanger deux cases de tableau en * passant par la variable tmp */ #define echange(p1,p2) \ do { \ tmp = tableau[p1]; \ tableau[p1] = tableau[p2]; \ tableau[p2] = tmp; \ } while(0) pivot = tableau[b.debut]; g = b.debut + 1; d = b.fin; while (g < d) { while (g < d && tableau[g] <= pivot) g++; while (d > g && tableau[d] > pivot) d--; if (g < d) echange(g, d); } b1.debut = b.debut; b2.fin = b.fin; if (tableau[g] <= pivot) { echange(g, b.debut); b1.fin = g - 1; b2.debut = min(g + 1, b2.fin); } else if (g > b.debut + 1) { echange(g - 1, b.debut); b1.fin = max(g - 2, b1.debut); b2.debut = g; } else { /* sinon le pivot est le plus petit, donc déjà bien placé */ b1.fin = b.debut; b2.debut = b.debut + 1; } if (b1.debut < b1.fin) bret[nb_ret++] = b1; if (b2.debut < b2.fin) bret[nb_ret++] = b2; return nb_ret; }
int main (void){ std::cout << "\n----------- Echange -----------" <<std::endl; int a=1; int b=7; std::cout << "a="<< a << " b="<< b << std::endl; //on echange 2 int echange(a,b); std::cout << "a="<< a << " b="<< b << std::endl; double c=4.5; double d=6.3; std::cout << "c="<< c << " d="<< d << std::endl; //on echange 2 double echange(c,d); std::cout << "c="<< c << " d="<< d << std::endl; std::string e="lolo"; std::string f="popo"; std::cout << "e="<< e << " f="<< f << std::endl; //on echange 2 String echange(e,f); std::cout << "e="<< e << " f="<< f << std::endl; std::cout << "\n---------- Comparaison ----------" <<std::endl; //on compare 2 int std::cout << "[a,b] min("<< a << ","<< b <<") --> " << min(a,b) << std::endl; //on compare 2 double std::cout << "[c,d] min("<< c <<","<< d <<") --> " << min(c,d) << std::endl; //on compare 2 String std::cout << "[e,f] min("<< e <<","<< f <<") --> " << min(e,f) << std::endl; std::cout << "\n----------- Trie tab -----------" <<std::endl; //declaration de tableau de int int t1[]={1,3,7,8,4,1,2}; //on affiche le tableau non trier t1 <type int , taille 7> afficheTab<int,7>(t1); //on trie le tableau t1 <type int , taille 7> trieTab<int,7>(t1); //on affiche le tableau trier t1 <type int , taille 7> afficheTab<int,7>(t1); //declaration de tableau de double double t2[]={2.2,5.6,6.8,2.1,4.4,1.0,9.1,3.6,7.7}; //on affiche le tableau non trier t1 <type double , taille 7> afficheTab<double,9>(t2); trieTab<double,9>(t2); //on affiche le tableau non trier t2 <type double , taille 9> afficheTab<double,9>(t2); return 0; }
int main(){ // test de : echange int a=0, b = 1; printf("%d & %d \n", a, b); echange(&a, &b); printf("%d & %d \n \n", a, b); // test de : derive polypoint P2, D2, C2; double tab[]={2, 6, 7, 3, 0, 1, 5, 0, 8, 9, 9, 1}; int i; P2 = (poly *) malloc (sizeof(poly)); P2->deg=11; for (i=0;i <= P2->deg;i++){ P2->coef[i] = tab[i]; } D2 = derive(P2); dispoly(P2); dispoly(D2); C2 = derive(D2); while(C2->deg >= 1){ C2 = derive(C2); dispoly(C2); } return 0; }
int main() { float a=3.5, b=4.6; printf("avant : a=%f et b=%f\n",a,b); echange(&a,&b); printf("apres : a=%f et b=%f\n",a,b); return 0; }
int main(void) { int i = 10; int j = 55; printf("Avant : i=%d et j=%d\n", i, j); echange(&i, &j); printf("Après : i=%d et j=%d\n", i, j); return 0; }
int main() { int src[] = {0x00C0FFEE, 0xDECAFF00, 0xDEADBEEF, 0xEFFACEEE, 0xBEBAADAB, 0xEFBEADDE, 0}; printf("Little-endian : %s.\n", est_little_endian() ? "oui" : "non"); printf("Big-endian : %s.\n", est_big_endian() ? "oui" : "non"); affiche(src); echange(src); affiche(src); return 0; }
void triBulle(Liste l) { Liste tmp = l; while(!testtri(l)) { while(tmp != NULL) { if(tmp->val > tmp->nxt->val) { echange(tmp, tmp->nxt); } tmp = tmp->nxt; } } }
void tri(sotl_atom_set_t * set){ int paire = (set->natoms%2==0)?1:0; int impaire = (set->natoms%2==0)?0:1; for(unsigned i=0; i<set->natoms; i++){ if(i%2==0){ #pragma omp parallel num_threads(NB_THREAD ) #pragma omp for schedule(static) for(unsigned j =0; j<set->natoms-impaire;j+=2){ if(set->pos.z[j]<set->pos.z[j+1]){ echange(set,j,j+1); } } }else{ #pragma omp parallel num_threads(NB_THREAD ) #pragma omp for schedule(static) for(unsigned j=1;j<(set->natoms-paire);j+=2){ if(set->pos.z[j]<set->pos.z[j+1]){ echange(set,j,j+1); } } } } }
/* void echange(void) { int i,j; for(i=0; i<N; i++) for(j=0;j<N;j++) mCal[i][j] = mAff[i][j]; } */ void initJeu(void) { int i,j; srandom(time(NULL)); for (i=1;i<N-1;i++) for (j=1;j<N-1;j++) { if (random()%2) mUni[i][j] = 0; else mUni[i][j] = -1; } tor(); echange(); }
void boucle(void) { int i,j,nbv; int changement = 0; while(changement >= 0) { changement = 0; for(i=1; i<N-1; i++) { for(j=1;j<N-1;j++) { nbv = nbVoisins(i,j); if (nbv == 3 && !mCal[i][j]) mAff[i][j] = true; else if ((nbv<=1 || nbv>=4) && mCal[i][j]) mAff[i][j] = false; else { mAff[i][j] = mCal[i][j]; changement++; } } } tor(); //Echange des matrices echange(); sleep(1); //affJeu(); affJeuSDL(); //Condition d'arrêt if(changement == (N-2)*(N-2)) changement = -1; } /*for(i=1;i<N-1;i++) for(j=1;j<N-1;j++) SDL_FreeSurface(cellule[((i-1)*N)+j]); // Libération de la surface SDL_Quit(); */ }
void initJeu(void) { int i,j; srandom(time(NULL)); for (i=1;i<N-1;i++) for (j=1;j<N-1;j++) { //~ if (random()%2) //~ mAff[i][j] = true; //~ else mAff[i][j] = false; } mAff[1][1]=true; mAff[1][2]=true; mAff[1][3]=true; mAff[3][1]=true; mAff[4][1]=true; mAff[4][4]=true; mAff[1][4]=true; tor(); echange(); }
void * thread(void *a) { int i,j,nbv; int numThread; numThread = -1; bool fin = true; /* On incremente le nombre de threads lances * et on assigne un numero au thread courant */ pthread_mutex_lock(&m1); numThread = nb_th_lance ; nb_th_lance++; //printf("MUTEX1\n numThread = %d\n nb_th_lance = %d\n",numThread,nb_th_lance); pthread_mutex_unlock(&m1); while(fin) { //printf("WHILE\n TID : %d - Num : %d\n",(unsigned int)pthread_self(),numThread); for(i=(numThread*NB_LIGNES_PAR_THREADS)+1; i<(numThread*NB_LIGNES_PAR_THREADS)+NB_LIGNES_PAR_THREADS+1; i++) { for(j=1;j<N-1;j++) { nbv = nbVoisins(i,j); if (nbv == 3 && !mCal[i][j]) mAff[i][j] = true; else if ((nbv<=1 || nbv>=4) && mCal[i][j]) mAff[i][j] = false; else { mAff[i][j] = mCal[i][j]; } } } pthread_mutex_lock(&m2); //printf("MUTEX\n TID : %d - Num : %d\n",(unsigned int)pthread_self(),numThread); nb_th_finis++; if (nb_th_finis == NB_THREADS) { tor(); echange(); affJeuSDL(); nb_th_finis = 0; pthread_cond_broadcast(&cond); } else{ pthread_cond_wait(&cond,&m2); } while(SDL_PollEvent(&event) ){ if( event.type == SDL_QUIT ) fin = false;//On quitte le programme } pthread_mutex_unlock(&m2); //sleep(1); } SDL_FreeSurface(cellule); SDL_Quit(); pthread_exit(NULL); return NULL; }
int main() { unsigned long int a; char tab[10]; // Question a printf("Question A: \n \n"); a = sizeof(char); printf("sizeof(char) = %lu \n",a); // Le plus petit type et pred donc 1 octet a = sizeof(int); printf("sizeof(int) = %lu \n",a); // 4 octets ou 32 bits a = sizeof(double); printf("sizeof(double) = %lu \n",a); // Un double est plus precis qu'un float donc prend plus de mémoire qu'un float. a = sizeof(float); printf("sizeof(float) = %lu \n",a); // Tout les void*, char*,int*,.... sont de taille 4 car se sont des adresses. // Ici c'est de 4 octets car je marche sur un systeme 32 bits. a = sizeof(char*); printf("sizeof(char*) = %lu \n",a); a = sizeof(void*); printf("sizeof(void*) = %lu \n", a); a = sizeof(int*); printf("sizeof(int*) = %lu \n",a); a = sizeof(double*); printf("sizeof(double*) = %lu \n",a); a = sizeof(int**); printf("sizeof(int**) = %lu \n",a); a = sizeof(int[10]); printf("sizeof(int[10]) = %lu \n",a); a = sizeof(char[7][3]); printf("sizeof(char[7][3]) = %lu \n \n",a); /* a = sizeof(int[]); printf("sizeof(int[]) = %lu \n",a); // Erreur: Ne ne fonctionne pas quand on ne définie pas la taille du tableau */ a = sizeof(tab); printf("sizeof(tab) = %lu \n",a); // Tableau de 10 caractère a = sizeof(tab[0]); printf("sizeof(tab[0]) = %lu \n",a); // 1 seul caractère donc taille 1 a = sizeof(&tab[0]); printf("sizeof(&tab[0]) = %lu \n",a); // car c'est l'adresse du tableau. a = sizeof(*&tab); printf("sizeof(*&tab) = %lu \n",a); // Pointe la valeur de l'adresse du tableau : Donc 10 caracère de taille 1 a = sizeof(*&tab[0]); printf("sizeof(*&tab[0]) = %lu \n \n",a); // Pointe la valeur de l'adresse du tableau : Donc 1 car c'est 1 seul caractère. char (*p)[10] = &tab; // C'est un pointeur sur un tableau de caractère ( l'adresse d'un tableaud e 10 caractères) a = sizeof(p); printf("sizeof(p) = %lu \n",a); a = sizeof(*p); printf("sizeof(*p) = %lu \n",a); // revoie ce qu'il y a à l'adresse de p a = sizeof((*p)[2]); printf("sizeof((*p)[2]) = %lu \n",a); a = sizeof(&(*p)[2]); printf("sizeof(&(*p)[2]) = %lu \n \n",a); // Question b) printf("Question B: \n \n"); int c; int b; c = 2; b = 3; printf(" c = %d et b = %d \n", c, b); echange( &c, &b); printf(" c = %d et b = %d \n \n", c, b); // Question c) printf("Question C: \n \n"); int d = 1; int* q = &d; printf(" q = %p \n ", q); reinitPointeur(&q); printf(" q = %p \n", q); return 0; }
void inverse(int a[], int taille){ int i=0; for(;i<(taille/2);i++){ echange(a+i,a+(taille-1-i)); } }