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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Modular Programming With Functions
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 7: User-Defined Functions II
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Storage Classes.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CHAPTER 8 Scope, Lifetime, and More on Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
Dynamic memory allocation and Intraprogram Communication.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
CISC181 Introduction to Computer Science Dr
Functions and an Introduction to Recursion
C Functions -Continue…-.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Dynamic memory allocation and Intraprogram Communication
Chapter 5 - Functions Outline 5.1 Introduction
Dynamic memory allocation and Intraprogram Communication
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
Storage class.
Scope Rules Of Variables
1-6 Midterm Review.
C Programming Lecture-17 Storage Classes
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Scope Rules.
Presentation transcript:

 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between 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 Add 1 to make random number between 1 and n 1 + ( rand() % 6) number between 1 and 6

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

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

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

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

 2007 Pearson Education, Inc. All rights reserved 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;

 2007 Pearson Education, Inc. All rights reserved 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

 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

 2007 Pearson Education, Inc. All rights reserved 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.)

 2007 Pearson Education, Inc. All rights reserved 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

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

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

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

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

 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

 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

 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*/

 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*/