Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Concevoir un programme qui :
- - Remplit un tableau de 20 entiers tirés aléatoirement dans [0,15]
- - Affiche le contenu du tableau
- - Calcule et affiche la moyenne des éléments
- - Remplit un deuxieme tableau de la façon suivante : Pour chaque élément du premier tableau correspond dans le second le reste de la divisionentière par 4
- - Affiche le contenu du second tableau, et la moyenne de ses éléments
- - Pour chaque entier de [0,3], affiche à quels indices du second tableau ils apparaissent respectivement */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int rand_a_b(int a, int b)
- {
- return rand()%(b-a) +a;
- }
- void remplir_tab (int t[], int n)
- {
- for (n=0; n<20; n++)
- {
- t[n] = rand_a_b(0, 15);
- }
- }
- void aff_tab (int t[], int n)
- {
- for (n=0; n<20; n++)
- {
- printf("%d ", t[n]);
- }
- printf("\n");
- }
- double moyenne (int t[], int n)
- {
- double moyenne=0;
- for (n=0; n<20; n++)
- {
- moyenne=+t[n];
- }
- moyenne/=20;
- return moyenne;
- }
- void remplir_u(int t[], int u[], int n)
- {
- for (n=0; n<20; n++)
- {
- u[n]=t[n]%4;
- }
- }
- void chercher (int x, int t[], int n)
- {
- for (x=0; x<4; x++)
- {
- printf("%d apparait aux indices : ", x);
- for (n=0; n<20; n++)
- {
- if (t[n] == x)
- {
- printf("%d ", n);
- }
- else;
- }
- printf("\n");
- }
- }
- int main (void)
- {
- int t[20], n, u[20], x, a, b;
- double m;
- srand(time(NULL)); // tu mets ça dans ton main (au début par exemple) avant d'utiliser rand(), et comme y'a time() il faut inclure time.h dans ton projet
- // maintenant à chaque appel rand génère un nouveau nombre
- a = rand();
- b = rand();
- printf("Tirage aléatoire des 20 chiffres.\n");
- remplir_tab (t, n);
- printf("Premier tableau : \n");
- aff_tab (t, n);
- m = moyenne (t, n);
- printf("La moyenne est de %lf.\n", m);
- remplir_u (t, u, n);
- printf("Second tableau, associant à chaque élément du premier le reste de sa division entière par 4 :\n");
- aff_tab (u, n);
- m = moyenne (u, n);
- printf("La moyenne est de %lf.\n", m);
- chercher (x, u, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement