CS 151 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CS 151

Description:

CS 151 – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 20
Provided by: paul313
Category:
Tags:

less

Transcript and Presenter's Notes

Title: CS 151


1
CS 151
  • Midterm exam Friday (Nov 7)
  • A practice midterm is located on web pags
  • Answers will be posted later
  • No quiz next week.
  • No programming assignment next week.

2
More functions
  • time functions
  • "random numbers"

3
The time() Function
  • Returns the number of seconds since 1/1/1970.
  • At 545 p.m., 4/27/2003, the value was
    1,051,465,590
  • Usage (requires include lttime.hgt)
  • int t
  • t time(NULL)

null pointer
4
Tick Tock
  • include ltstdio.hgt
  • include lttime.hgt
  • int main(void)
  • int i, t
  • for(i0 i lt 10 i) / loop for ticks and
    tocks /
  • t time(NULL) 1 / t is 1 second from now
    /
  • while (time(NULL) lt t) / wait /
  • printf("TICK\n") / then print TICK /
  • t time(NULL) 1 / again 1 sec in future
    /
  • while(time(NULL) lt t) / wait again /
  • printf("TOCK\n") / print TOCK and repeat /
  • return 0

5
The clock() Function
  • Also in the lttime.hgt library
  • Returns number of msec since computer was turned
    on.
  • Useful for timing things, e.g. reaction time.
  • wait random time up to 10 sec
  • get clock value and print a ''
  • wait for key press
  • subtract current clock val from previous and print

6
Reaction Time
  • include ltstdio.hgt
  • include lttime.hgt
  • include ltstdlib.hgt
  • int main(void)
  • int ranTime, reactTime
  • ranTime clock() 1000 rand() 10000
  • while(clock() lt ranTime) / wait 1 to 11 sec /
  • reactTime clock() / start measuring time /
  • printf("")
  • getchar() / wait for ENTER to be pressed /
  • reactTime clock() - reactTime / calculate
    the reaction time /
  • printf("Your reaction time was d msec",
    reactTime)
  • return 0

7
The rand() Function
  • Use
  • i rand() /requires include ltstdlib.hgt /
  • Returns different (random) number each time
    called (between 0 and RAND_MAX)
  • RAND_MAX 32,767 in CodeWarrior.
  • Always produces same sequence.
  • 16838 5758 10113 17515 31051 5627 23010 7419
    16212 4086 etc.

8
The srand() function
  • Sets starting point for random sequence, e.g.
  • srand(seed)
  • 'seed' will determine the first number rand()
    returns. (It won't be the value of 'seed').

9
A Random Number ExampleSuppose that I want a
list of 20 random 3-digit numbers.
  • What do I want to do?
  • Initialize the random number generator
  • Generate and print 20 3-digit numbers
  • Design decisions
  • Loop in main or loop in function?
  • Constants?

10
/ Program to generate 20 random 3-digit
numbers./ include ltstdio.hgt include
ltstdlib.hgt define LOVAL 100 define HIVAL
999 / prototypes / void initRandom() int
myRandom(int, int) int main(void) int
k initRandom() for (k 1 k lt 20
k) printf("d\n", myRandom(LOVAL,
HIVAL)) return 0 / stubs / void
initRandom() int myRandom(int, int) return 0
11
/ Program to generate 20 random 3-digit
numbers./ include ltstdio.hgt include
ltstdlib.hgt include lttime.hgt / / define LOVAL
100 define HIVAL 999 int myRandom(int,
int) void initRandom() int main(void) int
k initRandom() for (k 1 k lt 20
k) printf("d\n", myRandom(LOVAL,
HIVAL)) return 0 void initRandom()
srand(time(NULL)) int myRandom(int low,
int high) int range high low 1 return
rand() range low
12
Program Style
  • Use comments.
  • Line up code in loops and functions (use tabs to
    indent).
  • Use blank lines to separate programming
    ideas.

13
Indentation shows Nesting
  • int i,t
  • for(i0 i lt 10 i) / loop for ticks and
    tocks /
  • t time(NULL) 1 / t is 1 second from now
    /
  • while (time(NULL) lt t) / wait /
  • printf("TICK\n") / then print TICK /
  • t time(NULL) 1 / again 1 sec in future /
  • while(time(NULL) lt t) / wait again /
  • printf("TOCK\n") / print TOCK and repeat /

14
Style 2
  • int i,t
  • for(i0 i lt 10 i)
  • / loop for ticks and tocks /
  • t time(NULL) 1 / t is 1 second from now
    /
  • while (time(NULL) lt t) / wait /
  • printf("TICK\n") / then print TICK /
  • t time(NULL) 1 / again 1 sec in future /
  • while(time(NULL) lt t) / wait again /
  • printf("TOCK\n") / print TOCK and repeat /

15
Style 3
  • int i,t
  • for(i0 i lt 10 i)
  • / loop for ticks and tocks /
  • t time(NULL) 1 / t is 1 second from now
    /
  • while (time(NULL) lt t) / wait /
  • printf("TICK\n") / then print TICK /
  • t time(NULL) 1 / again 1 sec in future /
  • while(time(NULL) lt t) / wait again /
  • printf("TOCK\n") / print TOCK and repeat /

16
Program Design
  • Top-down design (or step-wise refinement)
  • Start with total problem and break into
    sub-problems, then into sub-sub-problems, etc.
  • Make functions for each sub-problem.
  • main() solves whole by calling f1(), f2(), f3().
  • f1() solves sub-problem by calling f4(), f5(),
    etc.

17
Parameter passing by reference
  • Address of actual parameter is passed to formal
    parameter.
  • Pass exactly one address to a formal parameter
  • Constants, literals, and expressions are not
    allowed.
  • Types must match exactly.
  • Actual parameter has (remember scanf ?)
  • Formal parameter has .
  • Formal parameter is an alias for the actual
    parameter.
  • Inside the function, the value of a variable is
    accessed by dereferencing with .
  • Changes in formal reference parameters are
    changes in the associated actual parameters.

18
What gets printed?
  • / prototypes, etc. defined here /
  • int main(void)
  • int a 7, b 4, c 5
  • int z testRef(a, b, c)
  • printf("d d d d\n", a, b, c, z)
  • int testRef(int p, int q, int r)
  • int a p q
  • int b a r
  • p 0
  • q 2
  • r q
  • return b

0 4 10 16
19
Questions?
Write a Comment
User Comments (0)
About PowerShow.com