1 / 14

a few things

a few things. Casting. ints and doubles ARE interchangeable int x; double y; x = 10; y = (double)x;. #include <cstdlib>. rand( ); - "pseudo" random number srand( int seed ); - seed the random number randomNumber = rand( )%10; // generate random number 0 thru 9. getting a coin flip.

rasha
Download Presentation

a few things

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. a few things

  2. Casting ints and doubles ARE interchangeable int x; double y; x = 10; y = (double)x;

  3. #include <cstdlib> • rand( ); - "pseudo" random number • srand( int seed ); - seed the random number randomNumber = rand( )%10; // generate random number 0 thru 9

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

  5. 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”.

  6. #include <ctime> • 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 <ctime> #include <cstdlib> srand ( (int) time(0) ); // seed random number generator with time

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

  8. C++ has two ways to combine variables • structs - variables are of different types (difficult) • arrays - variables are of the same type (easy, just a list)

  9. #include <string> • allows us to manipulate strings of characters string variableName = "Hello";

  10. manipulate? • define • convert upper to lower, or lower to upper • find substrings • compares • determine what was typed, and act on it especially • see if user typed a number or a character

  11. #include <iostream> #include <string > #include <string > #include <cstdlib> #include <ctime> using namespace std; void main( ) {

  12. string testPhrase = "Test some libraries"; cout << testPhrase << endl; cout << time( 0 ) << endl; cout << rand( ) << endl; srand( (int)time(0) ); cout << rand( ) << endl; } // end main

  13. the Princeton Egg

More Related