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.

Slides:



Advertisements
Similar presentations
The Random Class.
Advertisements

Building Java Programs
Probability Predictions Ch. 1, Act. 5. Probability The study of random events. Random events are things that happen without predictability – e.g. the.
Mathematics in Today's World
Describing Probability
Programming Functions: Passing Parameters by Reference.
Probability theory and average-case complexity. Review of probability theory.
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp.
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 Copyright M.R.K. Krishna Rao 2003 Chapter 5. Discrete Probability Everything you have learned about counting constitutes the basis for computing the.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
Section 5.1 What is Probability? 5.1 / 1. Probability Probability is a numerical measurement of likelihood of an event. The probability of any event is.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
Counting and Probability. Counting Elements of Sets Theorem. The Inclusion/Exclusion Rule for Two or Three Sets If A, B, and C are finite sets, then N(A.
How to start Visual Studio 2008 or 2010 (command-line program)
CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.
Lecture-5 Miscellaneous. Random Numbers Can use ‘rand()’ function declared in the stdlib.h header file The seed for random number generation can be set.
CMSC 1041 Functions II Functions that return a value.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
CALCULATE THE PROBABILITY OF AN EVENT. 1.ANSWER THIS QUESTION: IS THE EVENT POSSIBLE? STOP: DON’T CONTINUE. THE PROBABILITY OF THE EVENT IS O GO TO NUMBER.
C++ Programming Lecture 10 Functions – Part II
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
How to design and code functions Chapter 4 (ctd).
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
UNIT 11 Random Numbers.
Computer Programming for Engineers
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
Section The Idea of Probability AP Statistics
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
AP STATISTICS LESSON AP STATISTICS LESSON PROBABILITY MODELS.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Adding Probabilities 12-5
Homework 3 (due:May 27th) Deadline : May 27th 11:59pm
TMF1414 Introduction to Programming
Box models Coin toss = Head = Tail 1 1
© 2016 Pearson Education, Ltd. All rights reserved.
Functions, Part 2 of 2 Topics Functions That Return a Value
Number guessing game Pick a random number between 1 and 10
Homework 3 (due:June 5th)
Relative Frequency.
CS1010 Programming Methodology
The Random Class and its Methods
Looping.
Assignment Operators Topics Increment and Decrement Operators
Dry run Fix Random Numbers
Program Control Topics While loop For loop Switch statement
Binomial Distribution Prof. Welz, Gary OER –
Assignment Operators Topics Increment and Decrement Operators
Homework 2 (due:May 5th) Deadline : May 5th 11:59pm
Computer Science I: Get out your notes.
Assignment Operators Topics Increment and Decrement Operators
CSCE 206 Lab Structured Programming in C
Chapter 3: Independent Events
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 (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)

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 1 5 9 3 Output Example : 3 9 5 1

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 <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;