Advertisement
xGhod

Lab 04 Es 2 verifica

Oct 29th, 2020
2,286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 300
  4.  
  5. //prototipo della funzione
  6.  
  7. int main() {
  8.     int n = 300, random, k; //k è il maggiorante
  9.     int v[N]; //vettore con maggiorante casuale
  10.     int d[N]; //vettore che indica le posizioni del maggiorante, 0 se non è contenuto in d[i] e 1 se è contenuto
  11.  
  12.     for(int i = 0; i < 100000; i++) //genera 100000 vettori
  13.     {
  14.         for(int t = 0; t < n; t++) //azzera il vettore delle posizioni
  15.             d[t] = 0;
  16.  
  17.         k = rand() % N; //genero casualmente un nuovo maggiorante
  18.  
  19.         if(i % 2 == 0) //una volta su due
  20.         {
  21.             for(int j = 0; j < n / 2 + 1; j++) //creami un vettore con un maggiorante
  22.             {
  23.                 do {
  24.                     random = rand() % n;
  25.                 } while(d[random] != 0);
  26.                 d[random] = 1;
  27.                 v[random] = k;
  28.             }
  29.             for(int j = 0; j < n; j++)
  30.                 if(d[j] == 0)
  31.                     v[j] = rand() % n;
  32.         }
  33.         else //altrimenti creami un vettore probabilmente senza maggiorante
  34.         {
  35.             for(int j = 0; j < n; j++)
  36.                 v[j] = rand() % n;
  37.             k = -1;
  38.         }
  39.  
  40.         if(k != majority(v, n))
  41.             printf("%d\n", i); //se il maggiorante trovato non è quello che è stato inserito nel vettore, stampa a video
  42.  
  43.         //for(int j = 0; j < n; j++)
  44.         //    printf("%d ", v[j]);
  45.         //printf("\n");
  46.     }
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement