Lec03FunctionsMemory_6per.pdf

Lec03FunctionsMemory_6per.pdf - EECS 402 Pre-Existing...

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

View Full Document Right Arrow Icon
1 The University Of Michigan Andrew M. Morgan Andrew M Morgan 1 EECS402 Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records, etc. EECS EECS 402 EECS 402 Andrew M Morgan 2 Review: Pass-By-Value A value parameter in a function becomes a copy of the argument that is passed in Changing the value of a value parameter: Does changes the memory associated with the parameter – Does not change the memory associated with the argument passed in void valFunc(float val) { val = 50; } int main() { int mainVal = 9; valFunc(mainVal); cout << "mainVal: " << mainVal << endl; return (0); } mainVal: 9 This assignment changes the memory associated with this variable (only)! (Main's mainVal is unaffected) EECS EECS EECS EECS 402 EECS 402 Andrew M Morgan 3 Review: Pass-By-Reference A reference parameter in a function "references" the same physical memory location as the argument passed in Changing the value of a reference parameter: – Does not change the memory associated with the parameter Does change the memory associated with the argument passed in Therefore – argument's memory must not be constant or literal void refFunc(float &val) { val = 50; } int main() { int mainVal = 9; refFunc(mainVal); cout << "mainVal: " << mainVal << endl; return (0); } mainVal: 50 This assignment changes the memory associated with this variable! EECS 402 402 EECS 402 Andrew M Morgan 4 Pre-Existing Functions C++ libraries contain many functions that you usually do not have to write algorithms for Many math related functions in <cmath> include file (sqrt,pow,etc) Example: Pseudo-random number generation Pseudo-random numbers are always generated in a sequence The same sequence always results from the same starting value Modifying the starting value changes the sequence – called a "seed" Must #include <cstdlib> to access these functions Set seed with function "srand" void srand(unsigned int seedValue); Example: srand(100); //sets the seed to begin pseudo-random #s to be 100 Generate a pseudo-random number with "rand" int rand(); Returns an integer between 0 and constant "RAND_MAX" (usually 32,767) EECS 402 EECS 402 Andrew M Morgan 5 Pseudo-Random Numbers, Example Program #include <cstdlib> //req'd for srand and rand #include <iostream> //req'd for cout and cin using namespace std; int main() { double avg = 0.0; int i, minX = 30, maxX = 50; int randVal, seed; cout << "Enter seed: "; cin >> seed; srand(seed); for (i = 0; i < 10; i++) { randVal = rand() % (maxX - minX + 1) + minX; cout << randVal << endl; } for (i = 0; i < 10000; i++) { avg += rand() % (maxX - minX + 1) + minX; } avg /= 10000; cout << "Avg of 10000: " << avg << endl; return (0); } srand() is usually called only one time to start a sequence rand() is called each time a pseudo-random number is needed EECS EECS 402 EECS 402 Andrew M Morgan 6 Pseudo-Random Numbers, Example Output [ 34 ] temp -: ./a.out Enter seed: 12 42 46 40 41 40 43 32 38 31 42 Avg of 10000: 39.9971 [ 35 ] temp -: ./a.out Enter seed: 1652 38 32 43 43 36 48 34 48 31 49 Avg of 10000: 40.0484 [ 36 ] temp -: ./a.out Enter seed: 12 42 46 40 41 40 43 32 38 31 42 Avg of 10000: 39.9971 Note: Same seed = same sequence = same results
Image of page 1

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

View Full Document Right Arrow Icon
2 EECS EECS 402 402 EECS 402 Andrew M Morgan 7 Default Parameters To Functions
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern