informaticage

Numbers distribution C - Correction

Jun 23rd, 2021
800
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define LEN 4
  6.  
  7. int main(void) {
  8.   printf("Inserisci quante volte vuoi randomizzare: ");
  9.   int n;
  10.   scanf("%d", &n);
  11.  
  12.   float contan[LEN];
  13.  
  14.   srand(time(NULL));
  15.   for (int i = 0; i < n; i++) {
  16.     contan[rand() % LEN]++;
  17.   }
  18.  
  19.   float s = 0.0f;
  20.   for (int i = 0; i < LEN; i++) {
  21.     s += contan[i];
  22.   }
  23.   s = s / 100.0f;
  24.  
  25.   float p[LEN];
  26.   for (int i = 0; i < LEN; i++) {
  27.     p[i] = contan[i] / s;
  28.   }
  29.  
  30.   printf("Percentuali:\n\t");
  31.   for (int i = 0; i < LEN; i++) {
  32.     printf("Percentuale di [%d]: %.2f\n\t", i, p[i]);
  33.   }
  34.  
  35.   return 0;
  36. }
RAW Paste Data