Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static variables.

Similar presentations


Presentation on theme: "Static variables."— Presentation transcript:

1 Static variables

2 Outline In this lesson, we will:
Review the behavior of local variables and parameters Introduce static variables See how they work and how they can be applied Generating unique identifiers Applying specific algorithms

3 Persistent variables? Functions have both parameters and local variables All this information is lost when the function exists Could you determine, for example, how often a function is called? You would have a counter initialized to zero Each time the function is called this variable would be incremented Problem: this variable cannot be either a local variable or a parameter… You could pass an argument by reference, but that would be ugly…

4 Static variables A variable that is declared static is shared by all calls to that function It can only be accessed inside the function call void counting() { // The declaration and initailization is only // executed the first time the function is called static unsigned int count{0}; // The rest of the function is always executed, as per normal ++count; if ( count == 1 ) { std::cout << "This function has been called once" << std:endl; } else { std::cout << "This function has been called " << count << " times" << std::endl; }

5 Static variables A variable that is declared static is shared by all calls to that function It can only be accessed inside the function call #include <iostream> int main(); void f(); int main() { for ( int k{0}; k < 10; ++k ) { counting(); } return 0; Output: This function has been called once This function has been called 2 times This function has been called 3 times This function has been called 4 times This function has been called 5 times This function has been called 6 times This function has been called 7 times This function has been called 8 times This function has been called 9 times This function has been called 10 times

6 Example Suppose you are required to give unique identifying numbers to different items unsigned int generate_unique_id() { unsigned int const FIRST_ID{ }; static unsigned int next_id{FIRST_ID}; unsigned int returned_id{next_id}; ++next_id; return returned_id; }

7 Random normal numbers In the lesson on pseudo-random numbers, we saw that unsigned int grade{rand() % 101}; would return a random integer between 0 and 100 Each grade is equally likely Question: Is this a reasonable means of generating a likely grade? That is, a random grade of 17 or 75 or 99 is equally likely Real grades, however, are not uniformly distributed If you picked a random student and asked for that student’s grade, that grade would more likely be closer to the class average and less likely to be further from the class average Suppose that The class average is 75 About two-thirds of students have a grade within 13

8 Random normal numbers In this case, the grades may be approximated by a normal distribution with a mean 70 and a standard deviation of 13 If you read through the literature, a good approximation of this is a normal distribution (bell curve) with mean (average) 70 and a standard deviation (distance from the mean) of 13

9 Random normal numbers You further go through the literature and you find these formulas: If x and y are uniformly distributed random numbers on (0, 1), then both of the values are random with a mean of 70 and a standard deviation of 13

10 Random normal numbers The natural logarithm ln(x) is
calculated with std::log(x) Let’s code this: double normal( double mean, double std_dev ) { double const PI{std::acos( -1.0 )}; double x{(rand() + 1.0)/(RAND_MAX + 2.0)}; double y{(rand() + 1.0)/(RAND_MAX + 2.0)}; return mean + std_dev*std::sqrt( -2.0*std::log(x) ) *std::cos(2*PI*y); } Each time, we must calculate two new random numbers, but each time, we throw away the value mean + std_dev*std::sqrt( -2.0*std::log(x) ) *std::sin(2*PI*y);

11 Random normal numbers Not only are we creating two new random numbers, but we are also recalculating another square root function and a natural logarithm Wouldn’t it be nice if we could either: Return two random numbers? Return one on one call, and the other on the next? With static variables, we can achieve this second idea: The run-time will be reduced by 30% We must still calculating a trigonometric function In simulations, this can make a significant impact

12 Random normal numbers Let’s code this using static variables:
double normal( double mean, double std_dev ) { static double multiplier{}; static double trig_arg{0.0}; double const PI{std::acos( -1.0 )}; if ( trig_arg == 0.0 ) { double x{(rand() + 1.0)/(RAND_MAX + 2.0)}; double y{(rand() + 1.0)/(RAND_MAX + 2.0)}; multiplier = std::sqrt( -2.0*std::log(x) ); trig_arg = 2*PI*y; // ('y' != 0) => ('trig_arg' != 0) return mean + std_dev*multiplier*std::cos( trig_arg ); } else { double result{mean + std_dev*multiplier*std::sin( trig_arg )}; trig_arg = 0.0; return result; }

13 Random normal numbers Let’s run this new code: Output: 64 75 73 61 72
#include <iostream> #include <cmath> double normal( double mean, double std_dev ); int main(); int main() { for ( int k{0}; k < 10; ++k ) { std::cout << std::round( normal( 70, 13 ) ) << std::endl; } return 0; Output: 64 75 73 61 72 51 50 63

14 Random normal numbers These ten numbers look a little low
Their average is 65.6 If we print out 360 of them, their average with a standard deviation of 12.25: Problem: we have some grades greater than 100

15 Random normal numbers Note:
You don’t have to memorize the formula for approximating the normal distribution You do have to understand how static variables are used to achieve the desired result

16 Summary Following this lesson, you now
Understand how to use static variables Permanent memory for functions You cannot access them outside of the function in which they are defined Know they are initialized only once Understand some applications of these variables

17 References [1] No references?

18 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

19 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "Static variables."

Similar presentations


Ads by Google