Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.

Similar presentations


Presentation on theme: "Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the."— Presentation transcript:

1 Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the coin 100 times, and count the number of times each side of the coin appears. Print the results. The program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads. (Use srand() and rand() function.) #include /* RAND.C: This program seeds the random-number generator * with the time, then displays 10 random integers. */ #include void main( void ) { int i; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ srand( (unsigned)time( NULL ) ); /* Display 10 numbers. */ for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() ); }

2 2. Write a program that simulates the rolling of two dice. The program should use rand() to roll the first die, and should use rand() again to roll the second die. The sum of the two values should then be calculated. [Note: Since each die can show an integer value from 1 to 6, then the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 the least frequent sums.] Your program should roll the two dice 36,000 times and print the number of cases among 36,000 and probability for each sum value. The result of your program should look similar to the following output example (exact number may be different). Output Example 2 : 1026 (0.028500) 3 : 2023 (0.056194) 4 : 2988 (0.083000) 5 : 4086 (0.113500) 6 : 5018 (0.139389) 7 : 5978 (0.166056) 8 : 4928 (0.136889) 9 : 3992 (0.110889) 10 : 3096 (0.086000) 11 : 1907 (0.052972) 12 : 958 (0.026611) #include int main(void) { int i; int sum; int sum_count[13] = {0}; // all initialized to 0 // put your code here return 0; }

3 3. What does following program compute and print? Understand why such output results are obtained. What happens if the parameter b of mystery is negative integer or zero? #include int count=0; int mystery(int a, int b); int main(void) { int x, y; printf("Enter two integers: "); scanf("%d %d", &x, &y); printf("The result is %d\n",mystery(x,y)); return 0; } int mystery(int a, int b) { int c; printf("[BEFORE] count=%d, a=%d, b=%d\n",++count,a,b); if (b == 1) c=a; else c=a+mystery(a, b-1); printf("[AFTER] count=%d, a=%d, b=%d\n",--count,a,b); return c; }

4 4. Write a program that has the input and output as follows : input : N (N<1000), N integer numbers output : print the N numbers in reverse order Input Example : 4 1 5 9 3 Output Example : 3 9 5 1 #include #define SIZE 10 int main() { int num[1000]; int N, i; // put your code here return 0; }

5 5. Write a program that takes an integer decimal value from a keyboard (standard input) and print a binary number of the value (as standard output). Your output should have16 binary digits. (You may assume that input decimal value can be represented with 16 bit integer number (less than 2^16.) For example, 21  keyboard input should be writen as 0000 0000 0001 0101  output #include void print_bin(int n) { // put your code here } int main() { int decimal_val; scanf(“%d”,&decimal_val); print_bin(decimal_val); return 0; }


Download ppt "Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the."

Similar presentations


Ads by Google