CS 261 Recitation 3 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CS 261 Recitation 3

Description:

Common specifier: specifier is the conversion specifier, indicating ... Note that specifier must correspond to the actual type of the argument to be converted. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 20
Provided by: duc56
Category:

less

Transcript and Presenter's Notes

Title: CS 261 Recitation 3


1
CS 261 Recitation 3
Oregon State University School of Electrical
Engineering and Computer Science
  • Fall 2009

2
Outline
  • Review Programming Assignment 2
  • Worksheet 20
  • Programming in C (cont.)
  • Programming Assignment 3

3
Programming Assignment 2
  • Dynamic Array stack
  • ifndef EleType
  • define EleType double
  • endif
  • struct dyArray / dyArray Structure /
  • EleType data
  • int size
  • int capacity
  • void _dyArrayDoubleCapacity (struct dyArray da)

4
Programming Assignment 2
  • void dyArrayInit(struct dyArray da, int
    initialCapacity)
  • /check whether initialCapacity is correct/
  • assert (initialCapacity gt 0)
  • /init size and capacity/
  • da-gtsize 0
  • da-gtcapacity initialCapacity
  • /allocate memory for da-gtdata/
  • da-gtdata (EleType ) malloc (da-gtcapacity
    (sizeof (EleType)))
  • /check if the allocation successes/
  • assert(da-gtdata ! 0)

5
Programming Assignment 2
  • void dyArrayFree (struct dyArray v)
  • /deallocate dyArray's data/
  • free(v-gtdata)
  • /set size and capacity to Zero/
  • v-gtcapacity 0
  • v-gtsize 0

6
Programming Assignment 2
  • void dyArrayPush(struct dyArray da, EleType e)
  • /check whether the size is
  • greater or equal to the capacity/
  • if (da-gtsize gt da-gtcapacity)
  • _dyArrayDoubleCapacity(da)
  • /push new element at the top of the stack/
  • da-gtdatada-gtsize e
  • da-gtsize 1

7
Programming Assignment 2
  • void dyArrayPop(struct dyArray da)
  • /check whether the size greater than 0/
  • assert(da-gtsizegt0)
  • /remove the top element/
  • da-gtsize--

8
Programming Assignment 2
  • EleType dyArrayTop(struct dyArray da)
  • /check whether the size greater than 0/
  • assert (da-gtsize gt0)
  • /get the top element/
  • return (da-gtdatada-gtsize-1)

9
Programming Assignment 2
  • int dyArrayIsEmpty(struct dyArray da)
  • /compare size and 0/
  • return (da-gtsize 0)

10
Worksheet 20
  • Deque
  • struct deque
  • EleType data
  • int capacity
  • int size
  • int start
  • void _dequeDoubleCapacity (struct deque d)

start 3
size 4
11
Worksheet 20
  • void dequeAddFront (struct deque d, EleType
    newValue)
  • /check whether size gt capacity/
  • if (d-gtsize gt d-gtcapacity)
    _dequeDoubleCapacity(d)
  • /if start 0 gt start ? /
  • if (d-gtstart 0)
  • d-gtstart d-gtcapacity -1
  • /otherwise gt start ? /
  • else
  • d-gtstart d-gtstart -1
  • /add new value at d-gtstart /
  • d-gtdatad-gtstart newValue
  • /increase the queue's size /
  • d-gtsize

12
Worksheet 20
  • void dequeAddBack (struct deque d, EleType
    newValue)
  • /check whether size gt capacity /
  • if (d-gtsize gt d-gtcapacity)
    _dequeDoubleCapacity(d)
  • /if start size gt capacity /
  • if ((d-gtstart d-gtsize)gtd-gtcapacity)
  • d-gtdatad-gtstart d-gtsize - d-gtcapacity
    newValue
  • /otherwise /
  • else
  • d-gtdatad-gtstart d-gtsize newValue
  • /increase the queue's size /
  • d-gtsize

13
Worksheet 20
  • EleType dequeFront (struct deque d)
  • /check if size gt 1 /
  • assert (d-gtsizegt1)
  • /return front element /
  • return (d-gtdatad-gtstart)
  • EleType dequeBack (struct deque d)
  • /check if size gt 1 /
  • assert (d-gtsizegt1)
  • /return back element /
  • return (d-gtdata(d-gtstart d-gtsize
    -1)d-gtcapacity)

14
Worksheet 20
  • void dequeRemoveFront (struct deque d)
  • /check if size gt 1 /
  • assert (d-gtsizegt1)
  • /update d-gtstart /
  • d-gtstart (d-gtstart 1) d-gtcapacity
  • /update size /
  • d-gtsize--
  • void dequeRemoveBack (struct deque d)
  • /check if size gt 1 /
  • assert (d-gtsizegt1)
  • /update size /
  • d-gtsize--

15
Programming in C
  • printf function
  • int printf (const char format ,.../arg1 ,...,
    argn /)
  • Purpose Writes the format string pointed to by
    format to the standard output stream, replacing
    conversion specifications with values from the
    argument list arg1, ... , argn.
  • Common syntax
  • printf (specifier1 specifier2 ...
    specifierN , arg1, arg2, ..., argN)
  • E.G
  • printf (The value of n is d, n)
  • printf (g g g, d1, d2, d1 d2)

16
Programming in C
  • Common specifier
  • specifier is the conversion specifier, indicating
    how the given argument is to be interpreted and
    converted. Note that specifier must correspond to
    the actual type of the argument to be converted.

17
Programming in C
  • rand() function
  • Syntax
  • int rand( void )
  • Generates a random number between 0 and
    RAND_MAX. The constant RAND_MAX has a value of at
    least 32767, or 215 - 1.
  • Usage
  • double d rand()
  • printf ("g", d)
  • the result is always 41 for rand() produces the
    same sequence over and over
  • To produce truly random values you can reset the
    random number generator using the time function,
    as in
  • srand(time(NULL))
  • double d rand()
  • printf ("g", d)

18
Programming in C
  • Calculating execution time spent in an algorithm
  • include lttime.hgt
  • double getMilliseconds()
  • return 1000.0 clock() / CLOCKS_PER_SEC
  • double elapsed
  • elapsed -getMilliseconds()
  • j 0
  • for(i 0 i lt 100000000 i)
  • j j i / 3.0 / nonsense computation
  • that just takes time /
  • elapsed getMilliseconds()
  • printf("Elapsed Milliseconds lf\n", elapsed)

19
Programming Assignment 3
  • Require much more work than the 2 previous
    programming assignments!
  • Need to work on worksheet 19 and 22
  • Implement insertion sort on doubly-linked list
    data structure
  • Must perform a swap by removing the entire link
    from a list, and re-inserting it at the correct
    location
  • Calculate running time of your program
Write a Comment
User Comments (0)
About PowerShow.com