Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

• Program must compile with both -Wall and -Werror options enabled • Submit only

ID: 3602904 • Letter: #

Question

• Program must compile with both -Wall and -Werror options enabled • Submit only the files requested • Use doubles to store real numbers • Print all doubles to 2 decimal points unless stated otherwise Restrictions • No global variables may be used • Your main function may only declare variables and call other functions Description For this problem you will be implementing a Monte Carlo method for the estimation of pi. Imagine that you have a circle of radius 1 whose center is at (0,0) surrounded by a square with sides of length 2 as pictured below
If we randomly choose points within the square the probability of a point being inside the circle is proportional to the area of the square that it occupies. This means that
Solving for pi we have The probability for pi can be approximiated by picking a large number of points between [-1, 1] and counting the number that are inside the circle. The number of points inside the circle divided by the the total number of points picked is the probability of being inside the circle
Report your answer to 3 digits after the decimal place Assumptions • Input will not always be valid o If invalid input is received your program should continue to ask for more input until a valid value is entered o White space after desired input is allowed You will probably want to wait until after we cover how to do this in class before handling it Valid values for inputs • Seed: an integer • Number iterations: an integer greater than 0 Randomness • In order to match the outputs on Mimir you will need to make calls to rand in the exact order that I do o First generate a random number between [-1, 1] for the x axis o Then generate a random number between [-1,1] for the y axis o You should the following expression for generating a random double between LO and HI LO + ((double) rand()) / (((double) RAND_MAX) / (HI - LO)) Hint it would be a good idea to make the above expression into a function o Numbers that fall exactly on the edge of the circle are considered to be inside the circle Examples Example 1 Enter the seed for the random number generator: 4 Enter the number of iterations to run: 10 The value of pi is 3.200. Example 2 Enter the seed for the random number generator: -7 Enter the number of iterations to run: 2 The value of pi is 4.000. Example 3 Enter the seed for the random number generator: -7 Enter the number of iterations to run: 10000 The value of pi is 3.144. Example 4 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.147. Submit ProjectOpen IDE Files to submit: investments.c Time it took Matthew to Complete: 30 mins Requriements • Program must compile with both -Wall and -Werror options enabled • Submit only the files requested • Use doubles to store real numbers • Print all doubles to 2 decimal points unless stated otherwise Restrictions • No global variables may be used • Your main function may only declare variables and call other functions • You must use loops to solve this problem Description For this problem you will be figuring out if it is more beneficial to pay off your loans first before investing or if you should only make the minimum payments and invest the rest. Some things to pay attention to • Interest rates given are annual interests rates but we will be assuming that interest is compounded monthly so the real rates to use will be 1/12 of what we are given • 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 before they retire a warning message should be printed Cautions about using this program for your own investment startegies • This program assumes you are putting your money in a Roth IRA • There is an easier way to solve this problem in real life. Remind me to tell you about it. Assumptions • Input will not always be valid o If invalid input is received your program should continue to ask for more input until a valid value is entered The exception for this is if the money set aside for investments each month is less than the minimum payment on the loan. If it is the program should terminate. o White space after desired input is allowed You will probably want to wait until after we cover how to do this in class before handling it Valid values for inputs • Investment money: A real number >= minimum payment o If it is not the program should terminate • Loans: A real number >= 0 • Annual loan interest rate: a real number >= 0 • Minimum payment: a real number >= 0 • Current age: an integer >= 0 • Retirement age: an integer >= current age • Annual rate of return: a real number >= 0 Hints • This problem has no relationship to the monthly payment formula that we did in homework 1. Don't try and use it • Don't try and look up a closed form solution to this problem. I'm not sure if there is one and the program requires you to use loops, so if you find one you couldn't use it any way • When solving this problem think about what you would do in real life when paying off your loans / savings and have your program do that as well Examples Example 1 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. Example 2 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. Example 3 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. Example 4 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. Example 5 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 • Program must compile with both -Wall and -Werror options enabled • Submit only the files requested • Use doubles to store real numbers • Print all doubles to 2 decimal points unless stated otherwise Restrictions • No global variables may be used • Your main function may only declare variables and call other functions Description For this problem you will be implementing a Monte Carlo method for the estimation of pi. Imagine that you have a circle of radius 1 whose center is at (0,0) surrounded by a square with sides of length 2 as pictured below
If we randomly choose points within the square the probability of a point being inside the circle is proportional to the area of the square that it occupies. This means that
Solving for pi we have The probability for pi can be approximiated by picking a large number of points between [-1, 1] and counting the number that are inside the circle. The number of points inside the circle divided by the the total number of points picked is the probability of being inside the circle
Report your answer to 3 digits after the decimal place Assumptions • Input will not always be valid o If invalid input is received your program should continue to ask for more input until a valid value is entered o White space after desired input is allowed You will probably want to wait until after we cover how to do this in class before handling it Valid values for inputs • Seed: an integer • Number iterations: an integer greater than 0 Randomness • In order to match the outputs on Mimir you will need to make calls to rand in the exact order that I do o First generate a random number between [-1, 1] for the x axis o Then generate a random number between [-1,1] for the y axis o You should the following expression for generating a random double between LO and HI LO + ((double) rand()) / (((double) RAND_MAX) / (HI - LO)) Hint it would be a good idea to make the above expression into a function o Numbers that fall exactly on the edge of the circle are considered to be inside the circle Examples Example 1 Enter the seed for the random number generator: 4 Enter the number of iterations to run: 10 The value of pi is 3.200. Example 2 Enter the seed for the random number generator: -7 Enter the number of iterations to run: 2 The value of pi is 4.000. Example 3 Enter the seed for the random number generator: -7 Enter the number of iterations to run: 10000 The value of pi is 3.144. Example 4 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.147. Submit ProjectOpen IDE Files to submit: investments.c Time it took Matthew to Complete: 30 mins Requriements • Program must compile with both -Wall and -Werror options enabled • Submit only the files requested • Use doubles to store real numbers • Print all doubles to 2 decimal points unless stated otherwise Restrictions • No global variables may be used • Your main function may only declare variables and call other functions • You must use loops to solve this problem Description For this problem you will be figuring out if it is more beneficial to pay off your loans first before investing or if you should only make the minimum payments and invest the rest. Some things to pay attention to • Interest rates given are annual interests rates but we will be assuming that interest is compounded monthly so the real rates to use will be 1/12 of what we are given • 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 before they retire a warning message should be printed Cautions about using this program for your own investment startegies • This program assumes you are putting your money in a Roth IRA • There is an easier way to solve this problem in real life. Remind me to tell you about it. Assumptions • Input will not always be valid o If invalid input is received your program should continue to ask for more input until a valid value is entered The exception for this is if the money set aside for investments each month is less than the minimum payment on the loan. If it is the program should terminate. o White space after desired input is allowed You will probably want to wait until after we cover how to do this in class before handling it Valid values for inputs • Investment money: A real number >= minimum payment o If it is not the program should terminate • Loans: A real number >= 0 • Annual loan interest rate: a real number >= 0 • Minimum payment: a real number >= 0 • Current age: an integer >= 0 • Retirement age: an integer >= current age • Annual rate of return: a real number >= 0 Hints • This problem has no relationship to the monthly payment formula that we did in homework 1. Don't try and use it • Don't try and look up a closed form solution to this problem. I'm not sure if there is one and the program requires you to use loops, so if you find one you couldn't use it any way • When solving this problem think about what you would do in real life when paying off your loans / savings and have your program do that as well Examples Example 1 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. Example 2 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. Example 3 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. Example 4 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. Example 5 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 • Submit only the files requested • Use doubles to store real numbers • Print all doubles to 2 decimal points unless stated otherwise Restrictions • No global variables may be used • Your main function may only declare variables and call other functions Description For this problem you will be implementing a Monte Carlo method for the estimation of pi. Imagine that you have a circle of radius 1 whose center is at (0,0) surrounded by a square with sides of length 2 as pictured below
If we randomly choose points within the square the probability of a point being inside the circle is proportional to the area of the square that it occupies. This means that
Solving for pi we have The probability for pi can be approximiated by picking a large number of points between [-1, 1] and counting the number that are inside the circle. The number of points inside the circle divided by the the total number of points picked is the probability of being inside the circle
Report your answer to 3 digits after the decimal place Assumptions • Input will not always be valid o If invalid input is received your program should continue to ask for more input until a valid value is entered o White space after desired input is allowed You will probably want to wait until after we cover how to do this in class before handling it Valid values for inputs • Seed: an integer • Number iterations: an integer greater than 0 Randomness • In order to match the outputs on Mimir you will need to make calls to rand in the exact order that I do o First generate a random number between [-1, 1] for the x axis o Then generate a random number between [-1,1] for the y axis o You should the following expression for generating a random double between LO and HI LO + ((double) rand()) / (((double) RAND_MAX) / (HI - LO)) Hint it would be a good idea to make the above expression into a function o Numbers that fall exactly on the edge of the circle are considered to be inside the circle Examples Example 1 Enter the seed for the random number generator: 4 Enter the number of iterations to run: 10 The value of pi is 3.200. Example 2 Enter the seed for the random number generator: -7 Enter the number of iterations to run: 2 The value of pi is 4.000. Example 3 Enter the seed for the random number generator: -7 Enter the number of iterations to run: 10000 The value of pi is 3.144. Example 4 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.147. Submit ProjectOpen IDE Files to submit: investments.c Time it took Matthew to Complete: 30 mins Requriements • Program must compile with both -Wall and -Werror options enabled • Submit only the files requested • Use doubles to store real numbers • Print all doubles to 2 decimal points unless stated otherwise Restrictions • No global variables may be used • Your main function may only declare variables and call other functions • You must use loops to solve this problem Description For this problem you will be figuring out if it is more beneficial to pay off your loans first before investing or if you should only make the minimum payments and invest the rest. Some things to pay attention to • Interest rates given are annual interests rates but we will be assuming that interest is compounded monthly so the real rates to use will be 1/12 of what we are given • 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 before they retire a warning message should be printed Cautions about using this program for your own investment startegies • This program assumes you are putting your money in a Roth IRA • There is an easier way to solve this problem in real life. Remind me to tell you about it. Assumptions • Input will not always be valid o If invalid input is received your program should continue to ask for more input until a valid value is entered The exception for this is if the money set aside for investments each month is less than the minimum payment on the loan. If it is the program should terminate. o White space after desired input is allowed You will probably want to wait until after we cover how to do this in class before handling it Valid values for inputs • Investment money: A real number >= minimum payment o If it is not the program should terminate • Loans: A real number >= 0 • Annual loan interest rate: a real number >= 0 • Minimum payment: a real number >= 0 • Current age: an integer >= 0 • Retirement age: an integer >= current age • Annual rate of return: a real number >= 0 Hints • This problem has no relationship to the monthly payment formula that we did in homework 1. Don't try and use it • Don't try and look up a closed form solution to this problem. I'm not sure if there is one and the program requires you to use loops, so if you find one you couldn't use it any way • When solving this problem think about what you would do in real life when paying off your loans / savings and have your program do that as well Examples Example 1 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. Example 2 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. Example 3 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. Example 4 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. Example 5 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

Explanation / Answer

Question has 2 parts or 2 questions are combined into one. Solving first as per the Chegg policy.

Please find the program to compute the pi value using monte carlo method below.

/* Program to compute Pi using Monte Carlo methods */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

/* generate a random double and return*/
double rand_no(int seed){
int a,HI,LO;
srand(seed);
HI = 1;
LO = -1;
a = LO + ((double) rand()) / (((double) RAND_MAX) / (HI - LO));
return a;
}

int main(int argc, char* argv)
{
int iter=10;
int seed=4;
double x,y;
int i,count=0; /* # of points in the 1st quadrant of unit circle */
double z;
double pi;

printf("Enter the seed for the random number generator: ");

while(scanf("%d",&seed) != 1){
printf("Enter the seed for the random number generator: ");
scanf("%*s");
}

printf("Enter the number of iterations to run: ");

while(scanf("%d",&iter) != 1){
printf("Enter the number of iterations to run: ");
scanf("%*s");
}

/* initialize random numbers */
count=0;
for ( i=0; i<iter; i++) {
x = rand_no(seed);
y = rand_no(seed);
z = x*x+y*y;
if (z<=1) count++;
}
pi=(double)count/iter*4;
printf("The value of pi is %g ",pi);

return 0;
}