Professional Documents
Culture Documents
#include <math.h>
/*Fonction intgrer */
double f(double x)
{
return 1/x;
}
/* Calcul de l'intgrale de f de a b */
double I(double a, double b, int n)
{
double s;
int i ;
s = (f(a) + f(b))/2;
for(i = 1; i < n; i = i + 1)
s = s + f(a + (b-a)*i/n);
return s*(b-a)/n;
}
/* Calcul de I pour un n particulier*/
void approcheLn2(int n)
{
double x, y, erreur;
x = I(1, 2, n); /* La valeur approche pour n */
y = log(2); /* La valeur relle*/
erreur = (x-y) / y * 100;
erreur = (erreur >= 0) ? erreur : - erreur;
printf (" Pour N = %d : I = %g L'erreur avec ln2 est de %g%%nn", n, x, erreur);
}
/* Fonction principale */
void main(void)
{
approcheLn2(5);
approcheLn2(10);
approcheLn2(20);
approcheLn2(50);
approcheLn2(100);
}
Exercice 3 (Tri par slction)
Tous les types simples comme les entiers ou les caractres sont munis d'un ordre total
permettant de comparer deux lments. Trier un tableau d'lments d'un ensemble totalement
ordonne consiste ranger les lments du tableau de manire croissante par rapport cet
ordre. Il y a plusieurs mthodes de tri, parmi lesquels les tris par slection et les tris par
insertion. Dans ces algorithmes de tri, il est exig de ne pas utiliser de tableau auxiliaire. Le
tableau comportera des entiers positifs avec une valeur ngative marquant la fin du tableau.
La mthode de tri par slection "ordinaire" consiste slectionner la position du plus petit
lment du tableau, placer cet lment la premire place par change puis recommencer
sur le reste du tableau. Exemple :
Tableau: | 12 60 10 25 5
Selection: | 12 60 10 25 <5>
Placement: 5| 60 10 25 12
Slection : 5| 60 <10> 25 12
Placement : 5 10| 60 25 12
{
int i ,p;
int aux;
for ( i=0; i< taille ; i=i+1)
{
p=posMin(tab,i, taille );
aux=tab[i];
tab[ i]=tab[p];
tab[p]=aux;
}
}
Exercice 4:
1. crire un programme qui demande un entier n, alloue un tableau de n entiers, remplit
le tableau avec des nombres alatoires et contient les fonctions suivantes :
o affichage du tableau ;
o valeur et indice de l'lment minimum ;
o valeur et indice de l'lment maximum ;
o inversion de tous les lments du tableau ;
o calcul de la somme des lments ;
o calcul de la moyenne des lments ;
Correction
#include <stdio.h>
#include <stdlib.h>
Void init(int *pt, int
{
int *t = NULL;
int n =
0;
int i =
0;
*pn)
printf("n = ");
scanf("%d", &n);
if (n < 1)
erreur("valeur de n invalide");
t = calloc(n, sizeof(int));
for (i = 0; i < n; i++)
{
t[i] = rand();
}
*pt = t;
*pn = n;
}
Void affichage(int *t, int
{
int i ;
n)
int min(int
{
int i
int i_min
int v_min
*t, int
= 0;
= 0;
= 0;
if (!n)
return 0;
i_min = 0;
v_min = t[i_min];
for (i = 1; i < n; i++)
{
if (t[i] < v_min)
{
i_min = i;
v_min = t[i_min];
}
}
*pi = i_min;
*pv = v_min;
return 1;
}
Int max(int
{
int i
int i_max
int v_max
*t, int
= 0;
= 0;
= 0;
if (!n)
return 0;
i_max = 0;
v_max = t[i_max];
for (i = 1; i < n; i++)
{
if (t[i] < v_max)
{
i_max = i;
v_max = t[i_max];
}
}
*pi = i_max;
*pv = v_max;
return 1;
}
Void inversion(int *t, int
{
int *t1 = t;
n)
int *t2 = t + n - 1;
int c ;
while (t1 < t2)
{
c= 0;
c
= *t1;
*t1 = *t2;
*t2 = c;
t1++;
t2--;
}
}
Double somme(int *t, int
{
double s = 0;
n)
while (n--)
{
s += t[n];
}
return s;
}
Double moyenne(int *t, int
{
double m = 0;
n)
if (n)
{
m = somme(t, n)/n;
}
return m;
}
main()
{
int *t = NULL;
int n =
0;
init(&t, &n);
printf("Tableau de depart\n");
affichage(t, n);
printf("Inversion\n");
inversion(t, n);
affichage(t, n);
printf("Somme et moyenne\n");
{
double s = 0;
s = somme(t, n);
printf("somme des lments: %.0f\n", s);
}
{
double m = 0;
m = moyenne(t, n);
printf("moyenne des lments: %f\n", m);
}
}