ee150_broke_c - EE 150 Lab 5 Broke Again? 1 Introduction In...

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

View Full Document Right Arrow Icon
Last Revised: 10/20/2008 1 EE 150 Lab 5 Broke Again? 1 Introduction In this exercise you will take the simulation you wrote in Matlab for the coin flipping game and implement the same program in C/C++. Recall the scenario of three bored students with some number of quarters who agree to play a game where all three flip one of their coins examining the resulting combination of heads and tails. If all three coins match (heads or tails), each player takes their coin back. Otherwise, whoever threw the one coin that does NOT match the two others gets all three coins, and the game repeats. Assuming, all players start with n coins, what is the average number of flips before one player goes broke? 1 2 What you will learn This lab exercise will familiarize you with programming in C/C++ and give you experience with the data types and common control structures (loops and conditionals) used in computational procedures. In addition, you will become proficient performing I/O in C/C++ using the C methods (printf, scanf) and C++ stream operators (cout/cin). 3 Background Information and Notes Performing I/O in C Before C++, the primary function used to output text to the screen was the printf function. The primary function to input text from the user (keyboard) was scanf . These functions are prototyped in the C library header <stdio.h>. int printf(char *format, [argument],…); int scanf(char *format, [argument],…); Printf : Printf takes a variable number of arguments. The first one is a character array (text string) that contains the text to be printed out and place holders for any variable values that you may want to display within the text. The following arguments are the actual variables for the placeholders you entered (in the order that you entered them). Their values will be substituted for the place holders. An easy example is simply: printf(“Hello world. \ n”); This example prints the text string “Hello world.” followed by a newline ( \n) character (which is what you get when you hit ‘return’ or ‘enter’ on the keyboard). 1 Taken from Nahin, Digital Dice , Princeton University Press, 2008 and originally published by G.W. Petrie in American Mathematical Monthly , August-September 1941 issue.
Background image of page 1

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

View Full DocumentRight Arrow Icon
EE 150 Lab 5 - Broke Again? 2 Last Revised: 10/20/2008 There are a few of these special characters that printf handles. They are known as escape characters and all start with the backward slash ‘ \ ’. Some of them are: \0 = null character, \t = tab, \\ = actual ‘ \ ’ character, \r = carriage return (moves the cursor to the start of the same line). Very often we want to use printf to output variable values. The following placeholders (or format tags) must be used depending on the type of variable you want to display: %c Outputs a single text (ASCII) character (used for ‘char’ variables) %d Outputs decimal value of the variable (used for ‘int’ variables) %f Outputs a single (‘float’) FP number %lf Outputs a ‘double’ FP number
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/12/2009 for the course EE 150 taught by Professor Dr.burke during the Fall '08 term at USC.

Page1 / 7

ee150_broke_c - EE 150 Lab 5 Broke Again? 1 Introduction In...

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

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