CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions...
-
Upload
tracy-powers -
Category
Documents
-
view
230 -
download
0
Transcript of CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions...
![Page 1: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/1.jpg)
CS1010E Programming MethodologyTutorial 4
Modular Programming with Functions
![Page 2: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/2.jpg)
Question 1a) What is the output ?
0
1
0
Line Main Func1 Console
4 X = 0
5 X = 0 0
6 X = 0 X = 0
11 X = 0 X = 1
12 X = 0 X = 1 1
7 X = 0 0
![Page 3: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/3.jpg)
Question 1
What is the output?
1 2
100 200
200 100
1 2
Line Main Func2 Console
4 X=1,Y=2
5 X=1,Y=2 1 2
6 X=1,Y=2 Num1= 1, Num2= 2
12 X=1,Y=2 Num1= 100, Num2= 2
13 X=1,Y=2 Num1= 100, Num2= 200
14 X=1,Y=2 Num1= 100, Num2= 200 100 200
7 X=1,Y=2 Num1=2,Num2=1
12 X=1,Y=2 Num1= 200, Num2= 1
13 X=1,Y=2 Num1= 200, Num2= 100
14 X=1,Y=2 Num1= 200, Num2= 100 200 100
8 X=1,Y=2 1 2
![Page 4: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/4.jpg)
Question 2
Random 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 ?
![Page 5: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/5.jpg)
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
![Page 6: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/6.jpg)
Question 3
![Page 7: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/7.jpg)
Question 3
![Page 8: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/8.jpg)
Storage Class and Scope
• Global variables: outside functions•Global Variables are consider BAD !!
• Local variables: within functions
![Page 9: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/9.jpg)
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
![Page 10: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/10.jpg)
Some Questions from Past Year Paper
Note that “%f” by default outputs to 6 decimal places!
![Page 11: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/11.jpg)
Some Questions from Past Year Paper
When evaluation, anything not 0 is TRUE, 0 is FALSEWhen output, TRUE is 1, FALSE is 0
![Page 12: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/12.jpg)
Some Questions from Past Year Paper
When use ++x, you increment before you fetchWhen use x++, you increment after fetch
![Page 13: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/13.jpg)
Some Questions from Past Year Paper
Be careful with the stopping condition
![Page 14: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/14.jpg)
Some Questions from Past Year Paper
Don’t get confused by the indentation!
![Page 15: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/15.jpg)
Some Questions from Past Year Paper
Macron is always direct substitution!
Z=((++x) >= (y++)? (++x): (y++));
![Page 16: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/16.jpg)
Some Questions from Past Year Paper
(D) X = I = 0 cannot be used in variable declaration
![Page 17: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/17.jpg)
Some Questions from Past Year Paper
(A) Short-cut evaluations
![Page 18: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/18.jpg)
Some Questions from Past Year Paper
(B) Careful with (int) coercion
![Page 19: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/19.jpg)
Some Questions from Past Year Paper
(E) Direct substitution!
![Page 20: CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.](https://reader035.fdocuments.us/reader035/viewer/2022062217/56649ef25503460f94c04573/html5/thumbnails/20.jpg)
Good Luck to your exam!See you next week!!
Download Slides and Sourcecode from:www.comp.nus.edu.sg/~wangzk/cs1010e.html