Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- task_03.c
- Task from:
- https://www.facebook.com/photo/?fbid=467781461516432&set=gm.1858927787627453
- The basic problem is to assume you have an opaque bag containing 20 balls,
- 12 of which are red and 8 of which are green.
- You jiggle the bag to mix the balls and then remove balls, one at a time,
- without looking in the bag.
- You will write a program that simulates this activity and reports
- the order of the balls as they are withdrawn from the bag.
- You are to report each ball as an "R" or a "G" indicating if the ball is red or green.
- You should end up with 20 letters printed to the screen.
- 8 G's and 12 R's in some random order.
- Important: At each step, you should simulate the right probabilities.
- For example, in the beginning, the probabilities of drawing a red ball is 60%
- and a green ball is 40%.
- You are to count and keep track of the longest streak of green balls
- experienced in the random drawing.
- You are to report this longest streak as a number, after all 20 balls have been selected.
- Sample output: RGGGRRRGRGGRGRRRGRRR 3
- Necessary code lines to generate random numbers:
- #include<stdlib.h> // RAND_MAX is defined in stdlib.h
- #include<time.h> // for random number generator
- in function main():
- time_t t; // for random number generator
- // Intializes random number generator, should only be called once.
- srand((unsigned) time(&t));
- // function rand() returns an integer value between 0 and RAND_MAX.
- number = rand();
- number = rand() % 9 + 1; // generate random number between 1 and 9
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include<stdlib.h> // RAND_MAX is defined in stdlib.h
- #include<time.h> // for random number generator
- #define MAX_SIZE 100
- // Function to print array of characters, n is number of characters in array arr[],
- // text describes the shown array arr[]
- void printArray( char *text, char arr[], int n ){
- int i;
- printf("%s", text );
- for(i=0; i<n; i++)
- printf("%2c", *(arr+i) );
- printf("\n");
- }
- // This function is not used here.
- // generate and return random integer number between lower and upper, including them.
- int get_random_integer_from_lower_to_uppper(int lower, int upper)
- {
- // generate and return random number between 0 and upper-1
- //return( rand() % upper );
- // generate and return random number between 1 and upper
- //return( rand() % upper + 1 );
- // generate and return random integer number between lower and upper
- return( (rand() % (upper - lower + 1)) + lower );
- }
- int main(void){
- int i, position, num=20, num_red=12, num_green=num-num_red, longest_subarray_of_G=0, temp_subarray_of_G=0;
- time_t t; // for random number generator
- char arr[MAX_SIZE];
- // Intializes random number generator, should only be called once.
- srand((unsigned) time(&t));
- // Fill the array with num_red 'R'.
- i=num_red;
- while(i>0){
- position = rand() % num; // generate random number between 0 and num
- if(arr[position]!='R'){
- arr[position]='R';
- i--;
- }
- }
- // Fill the remaining num_green empty spaces in the array with 'G'
- for(i=0;i<num;i++)
- if(arr[i]!='R')
- arr[i]='G';
- printArray("\n Array is: \n\n",arr,num);
- // Find longest subarray of 'G'
- for(i=0;i<num;i++)
- if(arr[i]=='G'){
- temp_subarray_of_G++;
- if(longest_subarray_of_G < temp_subarray_of_G)
- longest_subarray_of_G = temp_subarray_of_G;
- }else
- temp_subarray_of_G=0;
- printf("\n Longest subarray of 'G' is %d \n\n", longest_subarray_of_G);
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement