cs1010e programming methodology tutorial 4 modular programming with functions

Post on 22-Feb-2016

75 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions. C14,A15,D11,C08,C11,A02. Question 1. What is the output ? 0 1 0. Question 1 . What is the output? 1 2 100 200 200 100 1 2. Question 2. Random Function Rand() rand() returns a pseudo random Integer - PowerPoint PPT Presentation

TRANSCRIPT

CS1010E Programming MethodologyTutorial 4

Modular Programming with Functions

Question 1a) What is the output ?010 Line Main Func1 Console4 X = 05 X = 0 06 X = 0 X = 011 X = 0 X = 112 X = 0 X = 1 17 X = 0 0

Question 1

What is the output?1 2100 200200 1001 2

Line Main Func2 Console4 X=1,Y=25 X=1,Y=2 1 26 X=1,Y=2 Num1= 1, Num2= 212 X=1,Y=2 Num1= 100, Num2= 213 X=1,Y=2 Num1= 100, Num2= 20014 X=1,Y=2 Num1= 100, Num2= 200 100 2007 X=1,Y=2 Num1=2,Num2=112 X=1,Y=2 Num1= 200, Num2= 113 X=1,Y=2 Num1= 200, Num2= 10014 X=1,Y=2 Num1= 200, Num2= 100 200 1008 X=1,Y=2 1 2

Question 2Random Function Rand()• rand() returns a pseudo random Integer• rand() number needs a SEED• srand(int seed) reset SEED to be seed• If srand() is not called, rand() uses default

Create random INTEGER in [a, b] Create random number in [0, 1]

How to generate random number in arbitrary interval ? How to generate other distributions ?

Question 2

Compare the two codes :

• The difference is where the srand(seed) is called• For code 1, different random number is generated• For code 2, the random number is always the same

• Because seed is reset in every iteration

Question 3

Question 3

Storage Class and Scope

• Global variables: outside functions•Global Variables are consider BAD !!

• Local variables: within functions

Storage classes - Static

• Static can also be defined within a function• Static variable

• Is initialized at compilation time and retains its value between calls

int main(void) { func(); func(); func();

}

void func(){ static int count = 0; count++; printf("%d",count);

}

• The function called with the same parameter may return different result

Some Questions from Past Year Paper

Note that “%f” by default outputs to 6 decimal places!

Some Questions from Past Year Paper

When evaluation, anything not 0 is TRUE, 0 is FALSEWhen output, TRUE is 1, FALSE is 0

Some Questions from Past Year Paper

When use ++x, you increment before you fetchWhen use x++, you increment after fetch

Some Questions from Past Year Paper

Be careful with the stopping condition

Some Questions from Past Year Paper

Don’t get confused by the indentation!

Some Questions from Past Year Paper

Macron is always direct substitution!

Z=((++x) >= (y++)? (++x): (y++));

Some Questions from Past Year Paper

(D) X = I = 0 cannot be used in variable declaration

Some Questions from Past Year Paper

(A) Short-cut evaluations

Some Questions from Past Year Paper

(B) Careful with (int) coercion

Some Questions from Past Year Paper

(E) Direct substitution!

Good Luck to your exam!See you next week!!

Download Slides and Sourcecode from:www.comp.nus.edu.sg/~wangzk/cs1010e.html

top related