Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 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;
  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. for (n=0; n<20; n++)
  74. {
  75. if (t[n] == x)
  76. {
  77. printf("%d ", n);
  78. }
  79. else;
  80. }
  81. }
  82. printf("\n");
  83. }
  84.  
  85.  
  86.  
  87. }
  88.  
  89.  
  90. int main (void)
  91. {
  92. int t[20], n, u[20], x, a, b;
  93. double m;
  94.  
  95. 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
  96. // maintenant à chaque appel rand génère un nouveau nombre
  97. a = rand();
  98. b = rand();
  99.  
  100.  
  101. printf("Tirage aléatoire des 20 chiffres.\n");
  102. remplir_tab (t, n);
  103. printf("Premier tableau : \n");
  104. aff_tab (t, n);
  105. m = moyenne (t, n);
  106. printf("La moyenne est de %lf.\n", m);
  107. remplir_u (t, u, n);
  108. printf("Second tableau, associant à chaque élément du premier le reste de sa division entière par 4 :\n");
  109. aff_tab (u, n);
  110. m = moyenne (u, n);
  111. printf("La moyenne est de %lf.\n", m);
  112. chercher (x, u, n);
  113.  
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement