Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1.  
  2. /* Concevoir un programme qui :
  3. - Remplit un tableau de 20 entiers tirés aléatoirement dans [0,15]
  4. - Affiche le contenu du tableau
  5. - Calcule et affiche la moyenne des éléments
  6. - 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
  7. - Affiche le contenu du second tableau, et la moyenne de ses éléments
  8. - Pour chaque entier de [0,3], affiche à quels indices du second tableau ils apparaissent respectivement */
  9.  
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15.  
  16.  
  17. int rand_a_b(int a, int b)
  18. {
  19. return rand()%(b-a) +a;
  20. }
  21.  
  22.  
  23. void remplir_tab (int t[], int n)
  24. {
  25. for (n=0; n<20; n++)
  26. {
  27. t[n] = rand_a_b(0, 15);
  28. }
  29. }
  30.  
  31.  
  32.  
  33. void aff_tab (int t[], int n)
  34. {
  35. for (n=0; n<20; n++)
  36. {
  37. printf("%d ", t[n]);
  38. }
  39. printf("\n");
  40. }
  41.  
  42.  
  43.  
  44. double moyenne (int t[], int n)
  45. {
  46. double moyenne=0;
  47. for (n=0; n<20; n++)
  48. {
  49. moyenne=+t[n];
  50. }
  51. moyenne/=20;
  52. return moyenne;
  53. }
  54.  
  55. void remplir_u(int t[], int u[], int n)
  56. {
  57. for (n=0; n<20; n++)
  58. {
  59. u[n]=t[n]%4;
  60. }
  61. }
  62.  
  63.  
  64.  
  65. void chercher (int x, int t[], int n)
  66. {
  67. for (x=0; x<4; x++)
  68. {
  69.  
  70. printf("%d apparait aux indices : ", x);
  71. for (n=0; n<20; n++)
  72. {
  73. if (t[n] == x)
  74. {
  75. printf("%d ", n);
  76. }
  77. else;
  78. }
  79. printf("\n");
  80. }
  81.  
  82.  
  83.  
  84. }
  85.  
  86.  
  87. int main (void)
  88. {
  89. int t[20], n, u[20], x, a, b;
  90. double m;
  91.  
  92. 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
  93. // maintenant à chaque appel rand génère un nouveau nombre
  94. a = rand();
  95. b = rand();
  96.  
  97.  
  98. printf("Tirage aléatoire des 20 chiffres.\n");
  99. remplir_tab (t, n);
  100. printf("Premier tableau : \n");
  101. aff_tab (t, n);
  102. m = moyenne (t, n);
  103. printf("La moyenne est de %lf.\n", m);
  104. remplir_u (t, u, n);
  105. printf("Second tableau, associant à chaque élément du premier le reste de sa division entière par 4 :\n");
  106. aff_tab (u, n);
  107. m = moyenne (u, n);
  108. printf("La moyenne est de %lf.\n", m);
  109. chercher (x, u, n);
  110.  
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement