1 / 20

CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions

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

frisco
Download Presentation

CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010E Programming MethodologyTutorial 4Modular Programming with Functions C14,A15,D11,C08,C11,A02

  2. Question 1 • What is the output ? • 0 • 1 • 0

  3. Question 1 • What is the output? • 1 2 • 100 200 • 200 100 • 1 2

  4. Question 2 • Random Function Rand() • rand() returns a pseudo random Integer • rand() number needs a SEED • srand(intseed) 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 ?

  5. 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

  6. Question 3

  7. Question 3

  8. Storage Class and Scope • Global variables: outside functions • Global Variables are consider BAD !! • Local variables: within functions

  9. Storage classes - Static • Static can also be defined within a function • Static variable • Is initialized at compilation time and retains its value between calls voidfunc(){ staticint count =0; count++; printf("%d",count); } int main(void){ func(); func(); func(); } • The function called with the same parameter may return different result

  10. Some Questions from Past Year Paper Note that “%f” by default outputs to 6 decimal places!

  11. Some Questions from Past Year Paper When evaluation, anything not 0 is TRUE, 0 is FALSE When output, TRUE is 1, FALSE is 0

  12. Some Questions from Past Year Paper When use ++x, you increment before you fetch When use x++, you increment after fetch

  13. Some Questions from Past Year Paper Be careful with the stopping condition

  14. Some Questions from Past Year Paper Don’t get confused by the indentation!

  15. Some Questions from Past Year Paper Z=((++x) >= (y++)? (++x): (y++)); Macron is always direct substitution!

  16. Some Questions from Past Year Paper (D) X = I = 0 cannot be used in variable declaration

  17. Some Questions from Past Year Paper (A) Short-cut evaluations

  18. Some Questions from Past Year Paper (B) Careful with (int) coercion

  19. Some Questions from Past Year Paper (E) Direct substitution!

  20. Good Luck to your exam! See you next week!! Download Slides and Sourcecode from: www.comp.nus.edu.sg/~wangzk/cs1010e.html

More Related