pset3: Fifteen fi · PDF filepset3: Fifteen Tommy MacWilliam Generate Makefiles find...

Click here to load reader

  • date post

    06-Jul-2018
  • Category

    Documents

  • view

    215
  • download

    0

Embed Size (px)

Transcript of pset3: Fifteen fi · PDF filepset3: Fifteen Tommy MacWilliam Generate Makefiles find...

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    pset3: Fifteen

    Tommy MacWilliam

    [email protected]

    September 25, 2011

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Todays Music

    I Ke$haI Dancing with Tears in my EyesI We R Who we RI Kiss N TellI Blow

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Today

    I generateI MakefilesI findI fifteen

    I init()I draw()I move()I won()

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    TODO

    1. comment generate.c!

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Generate

    I ./generate n [s]I n: number of random numbers to generateI s: seed value

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    rand

    I generate uses a pseudo-random number generator(rand())

    I generates a random sequence of numbers given a seedvalue

    I same seed? same sequence of numbersI helpful for debugging!

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Piping

    I program > fileI send the output of program to a file called file

    I program < fileI send the contents of file to the input of program

    I program1 | program2I send the output of program1 to the input of program2

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Piping Examples

    I ./generate 1024 > numbers.txtI write the output of generate to a file called numbers.txt

    I ./find 13 < numbers.txtI use the contents of the file numbers.txt as input to

    find

    I ./generate 1024 | ./find 13I send the output of generate to the input of find

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Makefile

    I specify what happens when you make somethingI make will look for a file named Makefile in the current

    directory

    find: find.c helpers.c helpers.hgcc -ggdb -std=c99 ... -o find find.c

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Makefile

    name_of_target: files we need to usecommand_to_run

    I not just for compiling code!

    clean:rm -f *.o a.out core find generate

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Makefile

    I Makefiles require tabs, not spacesI gedit in the appliance will default to spaces!

    I weve provided you Makefiles, so no need to edit

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    TODO

    1. implement sort2. implement search

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    find.c

    I prompts the user for numbers to fill the haystackI Ctrl-d tells find to stop asking

    I then, searches haystack for the given needleI calls sort and search, defined in helpers.c

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    helpers.c

    I sort: sorts the values[] arrayI n is the size of values

    I search: returns true if value is found in haystack, elsefalse

    I n is the size of the haystack array

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    sort

    I sort values[] destructivelyI when sort returns, the array passed as an argument

    will be changedI possible because arrays are passed by reference

    I more about pass by reference this week!

    I do NOT return an array (since type is void)

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    I iterate over list, swapping elements in the wrong orderI elements bubble to their correct position with each

    iteration

    I once no elements have been swapped, list must besorted!

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    while elements have been swappedswapped = falsefor i = 0 to n - 2

    if array[i] > array[i + 1]swap array[i] and array[i + 1]swapped = true

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    5 0 1 6 4

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    0 5 1 6 4

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    0 1 5 6 4

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    0 1 5 6 4

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    0 1 5 4 6

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    0 1 5 4 6

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    0 1 5 4 6

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    0 1 4 5 6

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Bubble Sort

    0 1 4 5 6

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Selection Sort

    I build sorted list one element at a timeI start at beginning of list, find smallest elementI swap smallest element and first elementI move to second element, find smallest element, swap

    with secondI no longer need to look at first element, since we know

    its sorted!

    I continue for every element

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Selection Sort

    for i = 0 to n - 1min = ifor j = i + 1 to n

    if array[j] < array[min]min = j

    if array[min] != array[i]swap array[min] and array[i]

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Selection Sort

    5 0 1 6 4

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Selection Sort

    0 5 1 6 4

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Selection Sort

    0 1 5 6 4

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Selection Sort

    0 1 4 6 5

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Selection Sort

    0 1 4 5 6

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    TODO

    1. implement sort2. implement search

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    search

    I currently implemented as a linear searchI does not require array to be sorted, which is why find

    works fineI O(n), slow!

    I need to implement as binary searchI O(log n), fast!

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Binary Search

    while length of list > 0look at middle of listif number found, return trueelse if number is too high, only considerleft half of listelse if number is too low, only considerright half of list

    return false

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Binary Search

    50 61 121 124 143 161 164 171 175 182

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Binary Search

    164 171 175 182

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Binary Search

    161 164

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Binary Search

    I can be done iteratively or recursivelyI iterative: keep moving left and right boundsI recursive: keep calling search, but with different

    parameters each timeI more about recursion this week!

    I in both cases, need to determine middle element andwhich half to cut off

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    TODO

    1. implement sort2. implement search

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    TODO

    1. init()2. draw()3. move()4. won()

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    Where are we?

    I main() written for you in fifteen.cI accepts/parses command-line argumentI creates boardI checks if game is won and exits accordinglyI gets input, calls move tile function

  • pset3: Fifteen

    TommyMacWilliam

    Generate

    Makefiles

    find

    Fifteen

    init

    I int board[DIM_MAX][DIM_MAX];I 2D array representing board state

    I size of board given by dI board array potentially larger than actual board (