axyd

poncareRT-1DArray

Dec 7th, 2021 (edited)
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. #include <stdio.h>          //printf
  2. #include <stdlib.h>         //random
  3. #include <time.h>               //clock, time
  4. #include <unistd.h>         //fork
  5. #include <sys/types.h>  //clock_t type
  6.  
  7. int drawTicket(const int, const int);
  8. void showUrns(int*, const int, const unsigned long long, const int);
  9.  
  10. int main(){
  11.     srand(time(NULL));  //seed randomizer
  12.  
  13.     int finish= 0, balls= 10;
  14.     int urns[balls];
  15.  
  16.     //initialize urn
  17.     for (int i= 0; i < balls; ++i){ urns[i]= 0; }
  18.  
  19.     int ktr1= balls, ticket= 0;
  20.     unsigned long long nrDraws= 0;
  21.  
  22.     do{
  23.         ticket= (drawTicket(1,balls)) -1;   //draw a random ticket     
  24.         showUrns(urns,balls,nrDraws,0);
  25.        
  26.         //move respective ball to other urn
  27.         if(urns[ticket] == 0){
  28.             urns[ticket]= 1;
  29.             --ktr1;        
  30.         }   else{
  31.             urns[ticket]= 0;
  32.             ++ktr1;
  33.         }
  34.        
  35.         ++nrDraws;  //track number of draws    
  36.         showUrns(urns,balls,nrDraws,1); //display ball positions
  37.        
  38.         //Finish when balls are all in the first jar
  39.         if(ktr1 == balls) finish= 1;               
  40.     } while(!finish);
  41.  
  42.     printf("\n\tFINISHED, iterations: %llu\n\n", nrDraws);
  43.  
  44.     _exit(0);
  45. }
  46.  
  47.  
  48. /* Purpose: generates random number from range
  49.  *   In : low, upper  bound
  50.  *   Out: random number         */
  51. int drawTicket(const int low, const int high){
  52.     return (low + (rand() % high) ) ;
  53. }
  54.  
  55.  
  56. /* Purpose: displays in which urn a ball is in
  57.  *   In : urn array, nr of balls, nr of draws, show pre(0) or post(1) draw
  58.  *   Out: none                          */
  59. void showUrns(int *arrUrns, const int balls, const unsigned long long draws, const int post){
  60.     int urn, b= balls;
  61.    
  62.     if (!post){
  63.         printf("\n  ## Balls location, draws:(%llu) ##", draws+1);
  64.         printf("\n------------------------------------\n");
  65.         printf(" pre-draw  : |");  
  66.            
  67.         for(int i= 0 ; i<balls; ++i){          
  68.             printf("%d|", (arrUrns[i] == 0)? 1: 2);
  69.         }  
  70.    
  71.     }else if (post){       
  72.         printf("\n post-draw : |");
  73.        
  74.         for(int i=0 ; i<balls; ++i){
  75.         printf("%d|", (arrUrns[i] == 0)? 1: 2);
  76.         }
  77.        
  78.         printf("\n------------------------------------\n");
  79.                
  80.     }else{
  81.         printf("\n\t/!\\ ERROR: no pre/post-draw selected /!\\");
  82.     }
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
Add Comment
Please, Sign In to add comment