Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp.

Similar presentations


Presentation on theme: "The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp."— Presentation transcript:

1 The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp

2 a few things will help us design our own Princeton Egg a coin flipper

3 "for" loop boundaries // int x is the for loop variable counting // coin flips: int x = 0; for (x=1; x<=100; x=x+1) { cout << "X in for loop: " << x << endl; } cout << "X after for loop: " << x << endl;

4 Domain of a variable for (int x=1; x<=100; x=x+1) { cout << "X in for loop: " << x << endl; } cout << "X after for loop: " << x << endl; // error, x not useable outside for loop

5 #include rand( ); - "pseudo" random number randomNumber = rand( )%10; // generate random number 0 thru 9 srand( int seed ); - seed the random number

6 remember... calling a library function int ranNum; ranNum = rand( ) %2; // random one or zero The result of the library function is placed into ranNum. The rand()%2 function returns a random 0 or 1 as a “service”.

7 two problems with rand( ) 1. random numbers are the same every time you run it (the algorithm starts at a consistent place) 2. rand( )%2 gives equally spaced 0s and 1s (not random for these low numbers) on old computers (32 bit)

8 getting a coin flip int seed = 0; int randomNumber = 0; cout << "type a number to seed generator"; cin >> seed; srand( seed ); randomNumber = rand( )%2; // 0 or 1

9 #include time( 0 ); - returns the number of seconds since Jan 1 1970. I have no idea why. But, not an integer, some mutant "time" string… so: #include srand ( (int) time(0) ); // seed random number generator with time

10 actually, time(0) is a "structure" a structure, or "struct" in C++, is a meta-variable, that contains many variables… one main, and a number of auxiliary values the "time" struct contains the number of seconds (its main value), but also month, day, date and year which we can access when we get good at this

11 #include using namespace std; int main( ) {

12 Our Princeton Egg Elementary Statistics

13 Design 1. Flip some coins: a lot of them, in a for loop - use rand to generate numbers 0-9. - 0-4 are heads, 5-9 are tails 2. Count the number of heads and the number of tails

14 Design 3. abs(heads - tails) / 2 is the number of errant coin flips e.g. For 10 flips, if you get 6 heads and 4 tails, 6 - 4 / 2 = 1 coin flip was errant NOTE THAT THIS INVOLVES DIVIDING INTEGERS

15 we will use Casting ints and doubles ARE interchangeable but we have to cast int x; double y; x = 10; y = (double)x;

16 casting int numberOfFlips=0; int numberOfHeads=0; int numberOfTails=0; int diff = 0; // mistake double percentage = 0.0; diff = abs( numberOfHeads - numberOfTails )/2; // no division should EVER be done with integers

17 best: all divisions done with doubles double diff = 0.0; diff = (double) (abs( numberOfHeads - numberOfTails )) /2.0; 4. Calculate total % of errant flips percentage = (diff/(double)(numberOfFlips)) * 100.0;

18 Design 5. Finally, is percentage greater than 10%? - stay home

19

20

21


Download ppt "The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp."

Similar presentations


Ads by Google