Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 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.  
  15.  
  16. int rand_a_b(int a, int b)
  17. {
  18. return rand()%(b-a) +a;
  19. }
  20.  
  21.  
  22. void remplir_tab (int t[], int n)
  23. {
  24. for (n=0; n<20; n++)
  25. {
  26. t[n] = rand_a_b(0, 15);
  27. }
  28. }
  29.  
  30.  
  31.  
  32. void aff_tab (int t[], int n)
  33. {
  34. for (n=0; n<20; n++)
  35. {
  36. printf("%d ", t[n]);
  37. }
  38. printf("\n");
  39. }
  40.  
  41.  
  42.  
  43. double moyenne (int t[], int n)
  44. {
  45. double moyenne;
  46. for (n=0; n<20; n++)
  47. {
  48. moyenne=+t[n];
  49. }
  50. moyenne/20;
  51. return moyenne;
  52. }
  53.  
  54. void remplir_u(int t[], int u[], int n)
  55. {
  56. for (n=0; n<20; n++)
  57. {
  58. u[n]=t[n]%4;
  59. }
  60. }
  61.  
  62.  
  63.  
  64. void chercher (int x, int t[], int n)
  65. {
  66. for (x=0; x<4; x++)
  67. {
  68.  
  69. printf("%d apparait aux indices : ", x);
  70. for (n=0; n<20; n++)
  71. {
  72. for (n=0; n<20; n++)
  73. {
  74. if (t[n] == x)
  75. {
  76. printf("%d ", n);
  77. }
  78. else;
  79. }
  80. }
  81. printf("\n");
  82. }
  83.  
  84.  
  85.  
  86. }
  87.  
  88.  
  89. int main (void)
  90. {
  91. int t[20], n, u[20], x, a, b;
  92. double m;
  93.  
  94. 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
  95. // maintenant à chaque appel rand génère un nouveau nombre
  96. a = rand();
  97. b = rand();
  98.  
  99.  
  100. printf("Tirage aléatoire des 20 chiffres.\n");
  101. remplir_tab (t, n);
  102. aff_tab (t, n);
  103. m = moyenne (t, n);
  104. printf("La moyenne est de %lf.", m);
  105. printf("\n");
  106. remplir_u (t, u, n);
  107. chercher (x, u, n);
  108.  
  109.  
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement