Skip to content
Snippets Groups Projects
Commit be115e70 authored by pierre.kunzli's avatar pierre.kunzli
Browse files

Upload New File

parent f484c884
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define SIZE 10
// initialise aléatoirement le tableau
// avec des valeurs entre 0 et 1
void init(double tab[]){
for (int i = 0; i < SIZE; ++i) {
tab[i] = rand() / (double)RAND_MAX;
}
}
// retourne l'indice de l'élément minimum à
// partir de l'indice lbound
int indmin(double tab[], int lbound){
double min = tab[lbound];
int ind_min = lbound;
for (int j = lbound + 1; j < SIZE; ++j) {
if (min > tab[j]) {
ind_min = j;
min = tab[j];
}
}
return ind_min;
}
// trie le tableau par ordre croissant
// avec le tri par selection
void sort(double tab[]){
for (int i = 0; i < SIZE - 1; ++i) {
int ind_min = indmin(tab, i);
// placer le minimum
double tmp = tab[i];
tab[i] = tab[ind_min];
tab[ind_min] = tmp;
}
}
// affiche le tableau
void print(double tab[]){
for (int i = 0; i < SIZE; ++i) {
printf("%f ", tab[i]);
}
printf("\n");
}
// retourne vrai si le tableau est trié
// faux sinon
bool issorted(double tab[]){
for (int i = 0; i < SIZE - 1; ++i) {
if (tab[i] > tab[i + 1]) {
return false;
}
}
return true;
}
int main() {
srand(time(NULL));
double tab[SIZE];
init(tab);
sort(tab);
print(tab);
if(!issorted(tab)) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment