Advertisement
dmilicev

task_03.c

Feb 21st, 2022
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. /*
  2.  
  3.     task_03.c
  4.  
  5.     Task from:
  6.     https://www.facebook.com/photo/?fbid=467781461516432&set=gm.1858927787627453
  7.  
  8. The basic problem is to assume you have an opaque bag containing 20 balls,
  9. 12 of which are red and 8 of which are green.
  10.  
  11. You jiggle the bag to mix the balls and then remove balls, one at a time,
  12. without looking in the bag.
  13.  
  14. You will write a program that simulates this activity and reports
  15. the order of the balls as they are withdrawn from the bag.
  16.  
  17. You are to report each ball as an "R" or a "G" indicating if the ball is red or green.
  18.  
  19. You should end up with 20 letters printed to the screen.
  20. 8 G's and 12 R's in some random order.
  21.  
  22. Important: At each step, you should simulate the right probabilities.
  23.  
  24. For example, in the beginning, the probabilities of drawing a red ball is 60%
  25. and a green ball is 40%.
  26.  
  27. You are to count and keep track of the longest streak of green balls
  28. experienced in the random drawing.
  29. You are to report this longest streak as a number, after all 20 balls have been selected.
  30.  
  31. Sample output: RGGGRRRGRGGRGRRRGRRR 3
  32.  
  33.  
  34.  
  35. Necessary code lines to generate random numbers:
  36.  
  37.     #include<stdlib.h>  // RAND_MAX is defined in stdlib.h
  38.     #include<time.h>    // for random number generator
  39.  
  40. in function main():
  41.  
  42.     time_t t;   // for random number generator
  43.  
  44.     // Intializes random number generator, should only be called once.
  45.     srand((unsigned) time(&t));
  46.  
  47.     // function rand() returns an integer value between 0 and RAND_MAX.
  48.     number = rand();
  49.  
  50.     number = rand() % 9 + 1;    // generate random number between 1 and 9
  51.  
  52.  
  53.     You can find all my C programs at Dragan Milicev's pastebin:
  54.  
  55.     https://pastebin.com/u/dmilicev
  56.  
  57. */
  58.  
  59. #include <stdio.h>
  60. #include<stdlib.h>  // RAND_MAX is defined in stdlib.h
  61. #include<time.h>    // for random number generator
  62.  
  63. #define MAX_SIZE 100
  64.  
  65. // Function to print array of characters, n is number of characters in array arr[],
  66. // text describes the shown array arr[]
  67. void printArray( char *text, char arr[], int n ){
  68.     int i;
  69.     printf("%s", text );
  70.     for(i=0; i<n; i++)
  71.         printf("%2c", *(arr+i) );
  72.     printf("\n");
  73. }
  74.  
  75. // This function is not used here.
  76. // generate and return random integer number between lower and upper, including them.
  77. int get_random_integer_from_lower_to_uppper(int lower, int upper)
  78. {
  79.     // generate and return random number between 0 and upper-1
  80.     //return( rand() % upper );
  81.  
  82.     // generate and return random number between 1 and upper
  83.     //return( rand() % upper + 1 );
  84.  
  85.     // generate and return random integer number between lower and upper
  86.     return( (rand() % (upper - lower + 1)) + lower );
  87. }
  88.  
  89.  
  90. int main(void){
  91.     int i, position, num=20, num_red=12, num_green=num-num_red, longest_subarray_of_G=0, temp_subarray_of_G=0;
  92.     time_t t;       // for random number generator
  93.     char arr[MAX_SIZE];
  94.  
  95.     // Intializes random number generator, should only be called once.
  96.     srand((unsigned) time(&t));
  97.  
  98.     // Fill the array with num_red 'R'.
  99.     i=num_red;
  100.     while(i>0){
  101.         position = rand() % num;    // generate random number between 0 and num
  102.         if(arr[position]!='R'){
  103.             arr[position]='R';
  104.             i--;
  105.         }
  106.     }
  107.  
  108.     // Fill the remaining num_green empty spaces in the array with 'G'
  109.     for(i=0;i<num;i++)
  110.         if(arr[i]!='R')
  111.             arr[i]='G';
  112.  
  113.     printArray("\n Array is: \n\n",arr,num);
  114.  
  115.     // Find longest subarray of 'G'
  116.     for(i=0;i<num;i++)
  117.         if(arr[i]=='G'){
  118.             temp_subarray_of_G++;
  119.             if(longest_subarray_of_G < temp_subarray_of_G)
  120.                 longest_subarray_of_G = temp_subarray_of_G;
  121.         }else
  122.             temp_subarray_of_G=0;
  123.  
  124.     printf("\n Longest subarray of 'G' is %d \n\n", longest_subarray_of_G);
  125.  
  126.     return 0;
  127. } // main()
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement