Starting from:
$30

$24

Homework 3 Solution

Files to submit: meet_up.c

Time it took Matthew to Complete: 40 mins




All programs must compile without warnings when using the -Wall and -Werror options



Submit only the files requested



Do NOT submit folders or compressed files such as .zip, .rar, .tar, .targz, etc



Your program must match the output exactly to receive credit.



Make sure that all prompts and output match mine exactly.



Easiest way to do this is to copy and paste them



All input will be valid unless stated otherwise



Print all real numbers to two decimal places unless otherwise stated



The examples provided in the prompts do not represent all possible input you can receive.



All inputs in the examples in the prompt are underlined



You don't have to make anything underlined it is just there to help you differentiate between what you are supposed to print and what is being given to your program
If you have questions please post them on Piazza



Restrictions




No global variables are allowed



Your main function may only declare variables, call other functions, and assign variables values.



Assumptions




Input is NOT guaranteed to be valid



If invalid input is entered your program should continue to ask the user for input until valid input is entered
For this problem we have two pieces, P1 and P2, randomly placed on a R X C board. R is the number of rows and C is the number of columns. Each round P1 and P2 attempt to move closer together by moving a random amount towards the other piece's current position. Your job is to figure out the average number of rounds it takes for P1 and P2 to arrive at the same spot.







Input Constraints




Seed: an integer



Number of rows: an integer greater than 0



Number of columns: an integer greater than 0



Number of rounds: an integer greater than 0



Randomness




In order to match the tester output you must make calls to rand in the exact order that I do



For each simulation



Generate the starting row number for P1



Generate the starting column number for P1



Generate the starting row number for P2



Generate the starting column number for P2



For each round in a simulation



If P1 and P2 are on different rows generate a random new row position for P1



This value should be between [ P1' s row ,∣P1' s row− P2' s row∣]



If P1 and P2 are on different columns generate a random new column position for P1



This value should be between [ P1' scolumn ,∣P1' s column− P2 ' s column∣]



If P1 and P2 are on different rows generate a random new row position for P2



This value should be between [ P2 ' s row ,∣P2' s row− P1' s row∣]



If P1 and P2 are on different columns generate a random new column position for P2



This value should be between [ P2 ' s column ,∣P2' s column− P1' s column∣]



Additional Requirements




In order to help with debugging if the number of simulations to run is less than or equal to 5 your program should print out the following information



The starting positions of P1 and P2



Where P1 and P2 move to each round
Worked Examples




Here are some examples on a 3 X 3 board that show you the state of the board after each move. Piece 1 is represented by X and Piece 2 by O. When they land on the same spot only X is shown.




Example 1




Since the pieces move towards the other pieces' current position sometimes they can end up passing each other.




Piece one starts at: 0, 1

Piece two starts at: 1, 2

0*X*

1**O

2***

0 1 2

First piece moves from 0,1 to 0,2

Second piece moves from 1,2 to 1,1

0**X

1*O*

2***

0 1 2

First piece moves from 0,2 to 0,2

Second piece moves from 1,1 to 0,2

0**X

1***

2***

0 1 2




Example 2

Piece one starts at: 1, 2

Piece two starts at: 0, 0

0O**

1**X

2***

0 1 2

First piece moves from 1,2 to 1,1

Second piece moves from 0,0 to 0,0

0O**

1*X*

2***

0 1 2

First piece moves from 1,1 to 0,0

Second piece moves from 0,0 to 0,0

0X**

1***

2***

0 1 2

Example 3




Since the pieces move towards the other pieces' current position sometimes they can end up passing each other. Sometimes a piece might not choose to move at all




Piece one starts at: 1, 1

Piece two starts at: 0, 1

0*O*

1*X*

2***

0 1 2

First piece moves from 1,1 to 0,1 Second piece moves from 0,1 to 1,1

0*X*

1*O*

2***

0 1 2

First piece moves from 0,1 to 0,1 Second piece moves from 1,1 to 1,1

0*X*

1*O*

2***

0 1 2

First piece moves from 0,1 to 1,1 Second piece moves from 1,1 to 0,1

0*O*

1*X*

2***

0 1 2

First piece moves from 1,1 to 1,1 Second piece moves from 0,1 to 1,1

0***

1*X*

2***

0 1 2

Real Examples

Enter the seed for the random number generator: 10
Enter the number of rows on the board: 5

Enter the number of columns on the board: 7

Enter the number of simulations to run: 2 Simulation 0

Piece one starts at: 0, 0

Piece two starts at: 3, 0

First piece moves from 0,0 to 2,0 Second piece moves from 3,0 to 2,0 Simulation 1

Piece one starts at: 0, 2

Piece two starts at: 2, 5

First piece moves from 0,2 to 2,3 Second piece moves from 2,5 to 2,4 First piece moves from 2,3 to 2,3 Second piece moves from 2,4 to 2,3




On average it takes 1.50 rounds on a board 5 X 7 for the pieces to meet.

Enter the seed for the random number generator: 12
Enter the number of rows on the board: 5

Enter the number of columns on the board: 2

Enter the number of simulations to run: 3 Simulation 0

Piece one starts at: 0, 0

Piece two starts at: 4, 0

First piece moves from 0,0 to 3,0 Second piece moves from 4,0 to 4,0 First piece moves from 3,0 to 4,0 Second piece moves from 4,0 to 3,0 First piece moves from 4,0 to 3,0 Second piece moves from 3,0 to 3,0 Simulation 1




Piece one starts at: 3, 1

Piece two starts at: 2, 0

First piece moves from 3,1 to 3,1 Second piece moves from 2,0 to 3,1 Simulation 2

Piece one starts at: 2, 0

Piece two starts at: 2, 0




On average it takes 1.33 rounds on a board 5 X 2 for the pieces to meet.

Enter the seed for the random number generator: -34
Enter the number of rows on the board: 10

Enter the number of columns on the board: 10

Enter the number of simulations to run: 50

On average it takes 3.22 rounds on a board 10 X 10 for the pieces to meet.

Enter the seed for the random number generator: bob
Enter the seed for the random number generator: what

Enter the seed for the random number generator: -12

Enter the number of rows on the board: cats

Enter the number of rows on the board: dogs

Enter the number of rows on the board: 0

Enter the number of rows on the board: -2

Enter the number of rows on the board: 20

Enter the number of columns on the board: man

Enter the number of columns on the board: woman

Enter the number of columns on the board: 0

Enter the number of columns on the board: -2

Enter the number of columns on the board: 3-

Enter the number of columns on the board: 30

Enter the number of simulations to run: 100

On average it takes 3.98 rounds on a board 20 X 30 for the pieces to meet.

More products