Skip to content
Snippets Groups Projects
Commit 8278228d authored by Pierre Kunzli's avatar Pierre Kunzli
Browse files

ajout corrige serie 8

parent 05f26c44
Branches
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int* a, int* b){
int tmp = *a;
*a = *b;
*b = tmp;
}
void fill(int* tab, int size){
for(int i=0; i<size; i++){
tab[i] = i;
}
}
void permut_random(int* tab, int size){
for(int i=0; i<size; i++){
swap(tab+i, tab+(rand()%size));
}
}
void permut_cyclic(int* tab, int size, int n){
int* new = malloc(size*sizeof(int));
for(int i=0; i<size; i++){
new[(i+n)%size] = tab[i];
}
for(int i=0; i<size; i++){
tab[i] = new[i];
}
free(new);
}
void place_smaller_at_end(int* tab, int size){
int ind_min = 0;
for(int i=1; i<size; i++){
if(tab[i]<tab[ind_min]) ind_min = i;
}
swap(tab+ind_min, tab+size-1);
}
void print(int* tab, int size){
for(int i=0; i<size; i++){
printf("%d, ", tab[i]);
}
}
void sum(int* tab, int* tab_two, int* tab_three, int size){
for(int i=0; i<size; i++){
tab_three[i] = tab[i] + tab_two[i];
}
}
void main(){
int size;
srand(time(NULL));
printf("Entrez la taille du tableau : ");
scanf("%d", &size);
int* tab = malloc(size*sizeof(int));
fill(tab, size);
printf("fill : ");
print(tab, size);
printf("\n");
printf("permut random : ");
permut_random(tab, size);
print(tab, size);
printf("\n");
printf("permut cyclic 3 : ");
permut_cyclic(tab, size, 3);
print(tab, size);
printf("\n");
printf("place smaller at the end: ");
place_smaller_at_end(tab, size);
print(tab, size);
printf("\n");
int* tab_two = malloc(size*sizeof(int));
fill(tab_two, size);
permut_random(tab_two, size);
permut_cyclic(tab_two, size, 3);
place_smaller_at_end(tab_two, size);
printf("tab_two : ");
print(tab_two, size);
printf("\n");
int* tab_three = malloc(size*sizeof(int));
sum(tab, tab_two, tab_three, size);
printf("sum : ");
print(tab_three, size);
printf("\n");
free(tab);
free(tab_two);
free(tab_three);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment