Concetti Chiave
- The program simulates a coin flip 100 times, displaying "Heads" or "Tails" for each flip.
- A function named 'flip' returns 0 for Tails and 1 for Heads, simulating the randomness of a coin.
- It counts and displays the number of times each side of the coin appears.
- For a realistic simulation, both Heads and Tails should appear approximately 50 times each.
- The program utilizes the rand() function for generating random outcomes, seeded by the current time.
/* * Scrivete un programma che simuli il lancio di una monetina. * Per ogni lancio della monetina il programma dovrà visualizzare * Heads o Tails. Lasciate che il programma lanci la monetina per * 100 volte e contate il numero di occorrenze per la comparsa di * ogni faccia della monetina. Visualizzate i risultati. * Il programma dovrà richiamare una funzione flip, che non riceverà * argomenti e che restituirà 0 per croce e 1 per testa. * Nota: qualora il programma simuli realisticamente il lancio di * una monetina, allora ogni faccia della stessa dovrà apparire * approssimativamente la metà delle volte, per un totale approssimativo * di 50 teste e 50 croci. * */ #include#include #include #define TESTA 1 #define CROCE 0 #define LANCI 1000 unsigned int flip(void); int main(void) { int totTesta, totCroce; int i, result; totTesta = totCroce = 0; srand(time(NULL)); for (i = 0; i "); totCroce++; } else { printf("Heads
"); totTesta++; } } printf("Totale croce: %d, Totale testa: %d
", totCroce, totTesta); return 0; } unsigned int flip(void) { return (rand() % 2); }