Starting from:
$30

$24

Homework 3 Solution

Files to submit: investments.c

Time it took Matthew to Complete: 30 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.
You must use loops to solve this problem.



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 program you will be determining if it is better to put as much money as you can towards your student loans before saving for retirement or if it is better to only pay the minimum payment on your loan and invest the rest.




Points to pay attention to:




Interest rates given are annual interest rates but we will assume that interest is
compounded monthly so the real rates to use will be
1
of what are given
12




We will assume that interest is compounded on our accounts before any payments or contributions are made to them.
If the user has not finished paying off all of their loans by the time they retire a warning message should be printed






Cautions about using this program for your own investment strategy




This program is assuming you would be investing your money in a Roth IRA



Turns out there is a simpler answer than this program to finding out how you should invest. Remind me to tell you about it you finish it.



Examples:




Enter how much money you will be putting towards loans/retirement each month: 500
Enter how much you owe in loans: 40000

Enter the annual interest rate of the loans: 0.03

Enter your minimum monthly loan payment: 405.32

Enter your current age: 22

Enter the age you plan to retire at: 65

Enter the annual rate of return you predict for your investments: .05

You should only make the minimum payments on your loan and apply the rest towards retirement.

If you do you will have $592888.96 when you retire as opposed to $587281.54 if you payed off your loan before investing.

Enter how much money you will be putting towards loans/retirement each month: 1053
Enter how much you owe in loans: 50000

Enter the annual interest rate of the loans: 0.06

Enter your minimum monthly loan payment: 350

Enter your current age: 25

Enter the age you plan to retire at: 70

Enter the annual rate of return you predict for your investments: 0.05

You should apply all $1053.00 towards your loan before making any investments.

If you do you will have $1651149.44 when you retire as opposed to $1619732.68 if you only made minimum payments.Enter how much money you will be putting towards loans/retirement each month: 50

Enter how much you owe in loans: 1000

Enter the annual interest rate of the loans: 0.05

Enter your minimum monthly loan payment: 400

You didn't set aside enough money to pay off our loans. Ending program.

Enter how much money you will be putting towards loans/retirement each month: 500
Enter how much you owe in loans: 10000

Enter the annual interest rate of the loans: .02

Enter your minimum monthly loan payment: 100

Enter your current age: 18

Enter the age you plan to retire at: 20

Enter the annual rate of return you predict for your investments: 0.07

Warning! After you retire you will still have $7961.19 in loans left.

You should only make the minimum payments on your loan and apply the rest towards retirement.

If you do you will have $10272.41 when you retire as opposed to $1835.38 if you payed off your loan before investing.

Enter how much money you will be putting towards loans/retirement each month: bob
Enter how much money you will be putting towards loans/retirement each month: cat

Enter how much money you will be putting towards loans/retirement each month: -3

Enter how much money you will be putting towards loans/retirement each month: 250

Enter how much you owe in loans: something

Enter how much you owe in loans: 25 boys

Enter how much you owe in loans: 1000

Enter the annual interest rate of the loans: ziggy

Enter the annual interest rate of the loans: -3

Enter the annual interest rate of the loans: .1

Enter your minimum monthly loan payment: 50 50

Enter your minimum monthly loan payment: 25

Enter your current age: -5

Enter your current age: 20

Enter the age you plan to retire at: 18

Enter the age you plan to retire at: 65

Enter the annual rate of return you predict for your investments: -8

Enter the annual rate of return you predict for your investments: 0.04

You should apply all $250.00 towards your loan before making any

investments.

If you do you will have $371259.10 when you retire as opposed to $370579.15 if you only made minimum payments.







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.




Files to submit: pi.c

Time it took Matthew to Complete: 20 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 you will be implementing a Monte Carlo method for estimating the value of PI. Imagine that you have a circle of radius 1 whose center is at (0,0) surrounded by a square with sides length 2 (Pictured below).


















































































If we randomly choose points within the square the probability of a point being within the circle is proportional to the area of the square it occupies. This means that

ProbabilityInside Circle=
AreaCircle
=
pi∗r
2
=
pi∗12
=
pi
.
AreaSquare
2∗2


4
4












Solving for pi we have pi=Probability Inside Circle∗4
. The probability of being inside the circle can be
approximated by randomly picking a large number of points between [-1,1] and counting the number that are inside the circle. The number of points that end up inside the circle divided by the total number of points picked is the probability of being inside the circle.

Probabliity = number of points in the circle

Inside Cicle number of points picked







Input Constraints




Seed: an integer



Number iterations: 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



First generate a random number from [-1, -1] for the X axis



Then generate a random number from [-1, -1] for the Y axis



You should use this expression for generating a random double between LO and HI
◦ LO + ((double) (rand()) /(((double) RAND_MAX)/(HI-LO));




Numbers that fall exactly on the edge of the circle are considered to be inside the circle
Examples
the random number generator: 4
1.
Enter the seed for


Enter the number of iterations to run: 10
2.
The value of pi is
3.20000.
Enter the seed for
the random number generator: -7


Enter the number of iterations to run: 2
3.
The value of pi is
4.00000.
Enter the seed for
the random number generator: -7


Enter the number of iterations to run: 10000
4.
The value of pi is
3.14360.
Enter the seed for
the random number generator: 3.45


Enter the seed for
the random number generator: why


Enter the seed for
the random number generator: 12


Enter the number of iterations to run: cat


Enter the number of iterations to run: -1


Enter the number of iterations to run: 3.141


Enter the number of iterations to run: 75


The value of pi is
3.14667.

More products