# 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 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:

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.) <example code that generates 10 random integers > #include <stdio.h> #include <stdlib.h> /* RAND.C: This program seeds the random-number generator * with the time, then displays 10 random integers. */ #include <time.h> 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. Write a program that simulates the rolling of two dice
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). #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int i; int sum; int sum_count[13] = {0}; // all initialized to 0 // put your code here return 0; } Output Example 2 : 1026 ( ) 3 : 2023 ( ) 4 : 2988 ( ) 5 : 4086 ( ) 6 : 5018 ( ) 7 : 5978 ( ) 8 : 4928 ( ) 9 : 3992 ( ) 10 : 3096 ( ) 11 : 1907 ( ) 12 : 958 ( )

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 <stdio.h> 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. 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 #include <stdio.h> #define SIZE 10 int main() { int num[1000]; int N, i; // put your code here return 0; } Input Example : 4 Output Example :

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  output #include <stdio.h> 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