Computer Programming Lab 11 Solution - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Computer Programming Lab 11 Solution

Description:

If an animal slips left before square 1, move the animal back to ... else if ( y == 3 ) // big slip. bunnyPtr -= 12; else if ( y = 4 && y = 6 ) // small hop ... – PowerPoint PPT presentation

Number of Views:189
Avg rating:3.0/5.0
Slides: 21
Provided by: shyhka
Category:

less

Transcript and Presenter's Notes

Title: Computer Programming Lab 11 Solution


1
Computer ProgrammingLab 11 Solution
  • Shyh-Kang Jeng
  • Department of Electrical Engineering/
  • Graduate Institute of Communication Engineering
  • National Taiwan University

2
Lab Exercise 10 Tortoise and the Hare (1/6)
  • In this problem you will recreate the classic
    race between the tortoise and the hare. You will
    use random number generation to develop a
    simulation of this memorable event.
  • Our contenders begin the race at square 1 of
    70 squares. Each square represents a possible
    position along the race course. The finish line
    is at square 70.

3
Lab Exercise 10 Tortoise and the Hare (2/6)
  • There is a clock that ticks once per second.
    With each tick of the clock, your program should
    adjust the position of the animals according to
    the following rules

4
Lab Exercise 10 Tortoise and the Hare (3/6)
5
Lab Exercise 10 Tortoise and the Hare (4/6)
  • Begin the race by printing
  • BANG ! ! ! ! !
  • AND THEYRE OFF ! ! ! ! !
  • Start each animal at position 1 (i.e., the
    starting gate). If an animal slips left before
    square 1, move the animal back to square 1. For
    each tick of the clock (i.e., each repetition of
    a loop), print a 70-position line showing the
    letter T in the Tortoises position and the
    letter H in the hares position. Occasionally,
    the contenders will land on the same square.

6
Lab Exercise 10 Tortoise and the Hare (5/6)
  • In this case, the tortoise bites the hare, and
    your program should print OUCH! ! ! beginning at
    that position. All print positions other than
    the T, the H or the OUCH! ! ! should be blank.
  • After printing each line, determine whether
    either animal has reached or passes square 70.
    If so, print the name of the winner, and
    terminate the simulation. If the tortoise wins,
    print TORTOISE WINS! ! ! YAY! ! !

7
Lab Exercise 10 Tortoise and the Hare (6/6)
  • If the hare wins, print Hare wins, Yuch. . If
    neither animal wins, perform the loop again to
    simulate the next tick of the clock.
  • In this program, you will be passing arguments
    via call by reference, using reference notation.
    After successfully completing the exercise,
    rewrite your solution to use pointer notation.

8
Sample Output
9
Solution (1/6)
  • // CPLab11\Main.cpp
  • include ltiostreamgt
  • using stdcout
  • using stdendl
  • include ltcstdlibgt
  • include ltctimegt
  • include ltiomanipgt
  • using stdsetw
  • const int RACE_END 70
  • void moveTortoise( int turtlePtr )
  • void moveHare( int bunnyPtr )
  • void printCurrentPositions( int turtlePtr, int
    bunnyPtr )

10
Solutio (2/6)
  • int main()
  • int tortoise 1
  • int hare 1
  • int timer 0
  • srand( time( 0 ) )
  • cout ltlt "ON YOUR MARK, GET SET\nBANG
    !!!!"
  • ltlt "\nAND THEY'RE OFF !!!!\n"
  • // control race
  • while ( tortoise ! RACE_END hare ! RACE_END
    )
  • moveTortoise( tortoise )
  • moveHare( hare )
  • printCurrentPositions( tortoise, hare )
  • timer
  • // end while

11
Solution (3/6)
  • if ( tortoise gt hare )
  • cout ltlt "\nTORTOISE WINS!!! YAY!!!\n"
  • else
  • cout ltlt "Hare wins. Yuch.\n"
  • cout ltlt "TIME ELAPSED " ltlt timer ltlt " seconds"
  • ltlt endl
  • return 0
  • // end main

12
Solution (4/6)
  • // move tortoise
  • void moveTortoise( int turtlePtr )
  • int x 1 rand() 10
  • // determine which move to make
  • if ( x gt 1 x lt 5 ) // fast plod
  • turtlePtr 3
  • else if ( x 6 x 7 ) // slip
  • turtlePtr - 6
  • else // slow plod
  • ( turtlePtr )
  • // ensure that tortoise remains within subscript
    range
  • if ( turtlePtr lt 1 )
  • turtlePtr 1
  • else if ( turtlePtr gt RACE_END )
  • turtlePtr RACE_END
  • // end moveTortoise

13
Template (5/6)
  • // move hare
  • void moveHare( int bunnyPtr )
  • int y 1 rand() 10
  • if ( y 1 y 2 ) // big hop
  • bunnyPtr 9
  • else if ( y 3 ) // big slip
  • bunnyPtr - 12
  • else if ( y gt 4 y lt 6 ) // small hop
  • bunnyPtr 1
  • else if ( y 7 y 8 ) // small slip
  • bunnyPtr - 2
  • else // sleep
  • return

14
Solution (6/6)
  • if ( bunnyPtr lt 1 )
  • bunnyPtr 1
  • else if ( bunnyPtr gt RACE_END )
  • bunnyPtr RACE_END
  • // end moveHare
  • // output positions of animals
  • void printCurrentPositions( int snapperPtr, int
    bunnyPtr )
  • if ( bunnyPtr snapperPtr )
  • cout ltlt setw( bunnyPtr ) ltlt "OUCH!!!"
  • else if ( bunnyPtr lt snapperPtr )
  • cout ltlt setw( bunnyPtr ) ltlt "H"
  • ltlt setw( snapperPtr - bunnyPtr ) ltlt "T"
  • else
  • cout ltlt setw( snapperPtr ) ltlt "T"
  • ltlt setw( bunnyPtr - snapperPtr ) ltlt "H"
  • cout ltlt "\n"
  • // end printCurrentPositions

15
Solution (1/6)
  • // CPLab11\Main.cpp
  • include ltiostreamgt
  • using stdcout
  • using stdendl
  • include ltcstdlibgt
  • include ltctimegt
  • include ltiomanipgt
  • using stdsetw
  • const int RACE_END 70
  • void moveTortoise( int turtlePtr )
  • void moveHare( int bunnyPtr )
  • void printCurrentPositions( int turtlePtr, int
    bunnyPtr )

16
Solution (2/6)
  • int main()
  • int tortoise 1
  • int hare 1
  • int timer 0
  • srand( time( 0 ) )
  • cout ltlt "ON YOUR MARK, GET SET\nBANG
    !!!!"
  • ltlt "\nAND THEY'RE OFF !!!!\n"
  • // control race
  • while ( tortoise ! RACE_END hare ! RACE_END
    )
  • moveTortoise( tortoise )
  • moveHare( hare )
  • printCurrentPositions( tortoise, hare )
  • timer
  • // end while

17
Solution (3/6)
  • if ( tortoise gt hare )
  • cout ltlt "\nTORTOISE WINS!!! YAY!!!\n"
  • else
  • cout ltlt "Hare wins. Yuch.\n"
  • cout ltlt "TIME ELAPSED " ltlt timer ltlt " seconds"
    ltlt endl
  • return 0
  • // end main

18
Solution (4/6)
  • // move tortoise
  • void moveTortoise( int turtlePtr )
  • int x 1 rand() 10
  • // determine which move to make
  • if ( x gt 1 x lt 5 ) // fast plod
  • turtlePtr 3
  • else if ( x 6 x 7 ) // slip
  • turtlePtr - 6
  • else // slow plod
  • ( turtlePtr )
  • // ensure that tortoise remains within subscript
    // range
  • if ( turtlePtr lt 1 )
  • turtlePtr 1
  • else if ( turtlePtr gt RACE_END )
  • turtlePtr RACE_END
  • // end moveTortoise

19
Solution (5/6)
  • // move hare
  • void moveHare( int bunnyPtr )
  • int y 1 rand() 10
  • if ( y 1 y 2 ) // big hop
  • bunnyPtr 9
  • else if ( y 3 ) // big slip
  • bunnyPtr - 12
  • else if ( y gt 4 y lt 6 ) // small hop
  • bunnyPtr 1
  • else if ( y 7 y 8 ) // small slip
  • bunnyPtr - 2
  • else // sleep
  • return

20
Solution (6/6)
  • if ( bunnyPtr lt 1 )
  • bunnyPtr 1
  • else if ( bunnyPtr gt RACE_END )
  • bunnyPtr RACE_END
  • // end moveHare
  • // output positions of animals
  • void printCurrentPositions( int snapperPtr, int
    bunnyPtr )
  • if ( bunnyPtr snapperPtr )
  • cout ltlt setw( bunnyPtr ) ltlt "OUCH!!!"
  • else if ( bunnyPtr lt snapperPtr )
  • cout ltlt setw( bunnyPtr ) ltlt "H"
  • ltlt setw( snapperPtr - bunnyPtr ) ltlt "T"
  • else
  • cout ltlt setw( snapperPtr ) ltlt "T"
  • ltlt setw( bunnyPtr - snapperPtr ) ltlt "H"
  • cout ltlt "\n"
  • // end printCurrentPositions
Write a Comment
User Comments (0)
About PowerShow.com