Program_Example - A Programming Example Flips Write a...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
A Programming Example
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Flips Write a program that simulates n flips of a coin. The program is to keep track of the number of head and tails outcomes. It is to ouput the number of flips, the number of heads and tails, and the percentage of flips that results in a heads outcome.
Background image of page 2
Things to think about What programming structures are required? How do you simulate a flip? How will you keep track of each outcome?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The rand Function rand – produces a single pseudo-random number between 1 and 0. Each use of rand produces a new value in a series of random numbers. The first call to rand after starting MATLAB will always produce the same first number and thus the same series of numbers. The random number generator can be made to start with a different first number by first executing the command: rand('state',sum(100*clock)) How can this be used to produce a head or tail?
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
n=input('How many flips? '); heads = 0 ; tails = 0 ; for i = 1:n flip = round(rand); if (flip == 0) heads = heads + 1; elseif (flip == 1)
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/28/2011 for the course CHE 210 taught by Professor Swinnea during the Fall '08 term at University of Texas.

Page1 / 11

Program_Example - A Programming Example Flips Write a...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online