Lecture 9

staff.fisika.ui.ac.id
  • No tags were found...

Lecture 9

IntroductionDivide and conquerConstruct a program from smaller pieces or componentsEach piece more manageable than the original program


Program Components in C++Programs written bycombining new functions with “prepackaged” functionsin the C++ standard library.The standard library provides a rich collection offunctions.Functions are invoked by a function callA function call specifies the function name and providesinformation (as arguments) that the called function needsBoss to worker analogy:A boss (the calling function or caller) asks a worker(the called function) to perform a task and return(i.e., report back) the results when the task is done.


Program Components in C++Function definitionsOnly written onceThese statements are hidden from other functions.Boss to worker analogy:The boss does not know how the worker gets the jobdone; he just wants it done


Math Library FunctionsMath library functionsAllow the programmer to perform common mathematicalcalculationsAre used by including the header file Functions called by writingfunctionName (argument)Examplecout


Math Library FunctionsFunction arguments can beConstantssqrt( 4 );Variablessqrt( x );Expressionssqrt( sqrt( x ) ) ;sqrt( 3 - 6x );


FunctionsFunctionsAllow the programmer to modularize a programLocal variablesKnown only in the function in which they are definedAll variables declared in function definitions are localvariablesParametersLocal variables passed when the function is called thatprovide the function with outside information


Fig. 3.3: fig03_03.cpp1. Function prototype2. Loop3. Function definition


Fig. 3.4: fig03_04.cpp1. Function prototype(3 parameters)2. Input values2.1 Call function3. Function definition


Fig. 3.3: fig03_03.cppEnter three integers: 22 85 17Maximum is: 85Enter three integers: 92 35 14Maximum is: 92Enter three integers: 45 19 98Maximum is: 98


Function PrototypesFunction prototypeFunction nameParametersInformation the function takes inReturn typeType of information the function passes back to caller (defaultint)void signifies the function returns nothingOnly needed if function definition comes after the function call inthe programExample:int maximum( int, int, int );Takes in 3 intsReturns an int


Header FilesHeader filesContain function prototypes for library functions , , etc.Load with #include Example:#include Custom header filesDefined by the programmerSave as filename.hLoaded into program using#include "filename.h"


Random Number Generationrand functioni = rand();Load Generates a pseudorandom number between 0 andRAND_MAX (usually 32767)A pseudorandom number is a preset sequence of"random" numbersThe same sequence is generated upon every programexecution


Random Number Generationsrand functionJumps to a seeded location in a "random" sequencesrand( seed );srand( time( 0 ) ); //must includetime( 0 )The time at which the program was compiledChanges the seed every time the program is compiled,thereby allowing rand to generate random numbers


Random Number GenerationScalingReduces random number to a certain rangeModulus ( % ) operatorReduces number between 0 and RAND_MAX to anumber between 0 and the scaling factorExamplei = rand() % 6 + 1;Generates a number between 1 and 6


Fig. 3.7: fig03_07.cpp123456789101112131415161718192021// Fig. 3.7: fig03_07.cpp// Shifted, scaled integers produced by 1 + rand() % 6.#include using std::cout;using std::endl;#include using std::setw;#include // contains function prototype for randint main(){// loop 20 timesfor ( int counter = 1; counter


Fig. 3.7: fig03_07.cpp123456789101112131415161718192021// Fig. 3.7: fig03_07.cpp// Shifted, scaled integers produced by 1 + rand() % 6.#include using std::cout;using std::endl;#include using std::setw;#include // contains function prototype for randint main(){Notice rand() % 6 . This returns anumber between 0 and 5 (scaling).Add 1 to get a number between 1and 6.// loop 20 timesfor ( int counter = 1; counter


Fig. 3.7: fig03_07.cpp5 5 3 5 52 4 2 5 55 3 2 2 15 1 4 6 4


Fig. 3.9: fig03_09.cpp1234567891011121314151617181920212223// Fig. 3.9: fig03_09.cpp// Randomizing die-rolling program.#include using std::cout;using std::cin;using std::endl;#include using std::setw;// contains prototypes for#include int main(){unsigned seed;functions srand and randcout > seed;srand( seed ); // seed random number generator


Fig. 3.9: fig03_09.cpp24252627282930313233343536373839// loop 10 timesfor ( int counter = 1; counter


Fig. 3.9: fig03_09.cppEnter seed: 671 6 5 1 45 6 3 1 2Enter seed: 4324 2 6 4 32 5 1 4 4Enter seed: 671 6 5 1 45 6 3 1 2Notice how the die rolls changewith the seed.

More magazines by this user
Similar magazines