Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2007 Pearson Education, Inc. All rights reserved. 1 5.10 Random Number Generation  rand function – Load – Returns "random" number between 0 - 32767.

Similar presentations


Presentation on theme: " 2007 Pearson Education, Inc. All rights reserved. 1 5.10 Random Number Generation  rand function – Load – Returns "random" number between 0 - 32767."— Presentation transcript:

1  2007 Pearson Education, Inc. All rights reserved. 1 5.10 Random Number Generation  rand function – Load – Returns "random" number between 0 - 32767 i = rand(); – Pseudorandom - Preset sequence of "random" numbers - Same sequence for every function call  Scaling – To get a random number between 1 and n 1 + ( rand() % n ) - rand() % n returns a number between 0 and n - 1 - Add 1 to make random number between 1 and n 1 + ( rand() % 6) number between 1 and 6

2  2007 Pearson Education, Inc. All rights reserved. 2 Outline fig05_07.c

3  2007 Pearson Education, Inc. All rights reserved. 3 Outline fig05_08.c (1 of 3 )

4  2007 Pearson Education, Inc. All rights reserved. 4 Outline fig05_08.c (2 of 3 )

5  2007 Pearson Education, Inc. All rights reserved. 5 Outline fig05_08.c (3 of 3 )

6  2007 Pearson Education, Inc. All rights reserved. 6 5.12 Storage Classes  Storage class specifiers – Storage duration = Lifetime how long an object exists in memory – Scope – where object can be referenced in program – Linkage – specifies the files in which an identifier is known (more in Chapter 14)  Automatic storage – Object created and destroyed within its block – auto: default for local variables auto double x, y; – register: tries to put variable into high-speed registers - Can only be used for automatic variables register int counter = 1;

7  2007 Pearson Education, Inc. All rights reserved. 7 5.12 Storage Classes  Static storage – Variables exist for entire program execution – Default value of zero – static: local variables defined in functions. - Keep value after function ends - Only known in their own function – extern: default for global variables and functions - Known in any function

8  2007 Pearson Education, Inc. All rights reserved. Variable types  Local variable: scope=lifetime Local variables are declared within the body of a function, and can only be used within that function.  Static variable: scope<lifetime Another class of local variable is the static type. It is specified by the keyword static in the variable declaration. The most striking difference from a non-static local variable is, a static variable is not destroyed on exit from the function.  Global variable: scope =<lifetime A global variable declaration looks normal, but is located outside any of the program's functions. So it is accessible to all functions. 8

9  2007 Pearson Education, Inc. All rights reserved. 9 5.13 Scope Rules  File scope – Identifier defined outside function, known in all functions – Used for global variables, function definitions, function prototypes  Function scope – Can only be referenced inside a function body – Used only for labels ( start:, case:, etc.)

10  2007 Pearson Education, Inc. All rights reserved. 10 5.13 Scope Rules  Block scope – Identifier declared inside a block - Block scope begins at definition, ends at right brace – Used for variables, function parameters (local variables of function) – Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block  Function prototype scope – Used for identifiers in parameter list

11  2007 Pearson Education, Inc. All rights reserved. 11 Outline fig05_12.c (1 of 4 )

12  2007 Pearson Education, Inc. All rights reserved. 12 Outline fig05_12.c (2 of 4 )

13  2007 Pearson Education, Inc. All rights reserved. 13 Outline fig05_12.c (3 of 4 )

14  2007 Pearson Education, Inc. All rights reserved. 14 Outline fig05_12.c (4 of 4 )

15  2007 Pearson Education, Inc. All rights reserved. char g = 'S’; void f1(){ char fred = 'B'; printf( " %cad\n", g ); g = fred; } void f2(char cg){ g=cg; } void main( void ){ char k1 = 'D‘; char k2 = ‘d’; f1(); f1(); g = k1; f1(); f2(k2); printf( " %cad\n", g ); } 15

16  2007 Pearson Education, Inc. All rights reserved. int i=33; void ff(){ printf(“\n i= %d”,i); } void main(){ int i = 99; int sum; { int i; sum = 0; for ( i=1; i<=10; ++i ) sum+= i*i; } printf( "%d\n", sum ); printf( "%d\n", i ); ff(); } 16

17  2007 Pearson Education, Inc. All rights reserved. int id = 1; int f1( int a){ int id = a; return ++id; } void f2(){ static int a = 4; id = ++a; printf(“\n%d - %d”,a, id); } void f3(int b){ f2(); id = f1(b); f2(); id = f1(b); } 17 void main(){ int c = 10; { int c; c=11; f3(c); c = id; printf(“\n%d”, c, id); } printf(“\n%d”, c, id); } /* What is the output*/

18  2007 Pearson Education, Inc. All rights reserved. int id = 1; int f1( int a){ int id = a; return ++id; } void f2(){ static int a = 4; id = ++a; printf(“\n%d - %d”,a, id); } void f3(int b){ f2(); id = f1(b); { int id = 9; id = f1(b); printf(“\n%d - %d”, id, b); } f2(); } 18 void main(){ int c = 10; { int c; c=11; f3(c); c = id; printf(“\n%d”, c, id); } printf(“\n%d”, c, id); } /*ASS - What is the output*/


Download ppt " 2007 Pearson Education, Inc. All rights reserved. 1 5.10 Random Number Generation  rand function – Load – Returns "random" number between 0 - 32767."

Similar presentations


Ads by Google