Question

Generate a sub/function to simulate the roll of a single die. It should return a random...

Generate a sub/function to simulate the roll of a single die. It should return a random number
between 1 and the maximum number of faces on that die. Allow the user to specify different
number of faces.                                                                                                                                                              20 pts

Generate a sub/function to determine the number of years it takes to double your money if you invest it, given a user specified interest rate. See attached spread sheet for examples.                                    20 pts

Example results (starting at $1)

1.5%          48 yrs           $2.013

2.0%          37 yrs           $2.040 (36 years gives you $1.99989)

2.5%          30 yrs           $2.046

0 0
Add a comment Improve this question Transcribed image text
Answer #1

FIRST QUESTION.

#include<bits/stdc++.h>

using namespace std;

int main()

{

srand(time(0));

int x;

cout<<"\nEnter the number of faces : ";

cin>>x;

if(x < 1)

{

cout<<"\nIncorrect input.";

exit(0);

}

int val = 1 + rand() % x;

cout<<"\nResult is : "<<val;

}

SECOND QUESTION

In Compound interest the formula for resulting amount is A = P * (1 + r/n)^(nt)

A = Amount

P = Principal value

r = Interest rate

n = No. of times compounded in a year,

t = time

***For amount to be doubled, A = 2P and in this case n = 1 as it is compounded annually.

CODE

#include<bits/stdc++.h>

using namespace std;

int main()

{

double rate;

cout<<"\nEnter the interest rate in % : ";

cin>>rate;

double P=1.0;

//Formula is A = P * (1 + r/n)^(n*t)

//n = 1; A = 2P;

//double time = 72.0 / rate;

double time = log(2.0) / log(1.00 + 0.01 * rate);

double x = P * ( 1.0 + (0.01 * rate));

double val = pow(x,time);

if(val < 2.0000005) //2.0000005 To compensate for the error.

{

time++;

}

cout<<"\nTime taken is : "<<ceil(time);

}

NOTE - For any other query, Pls use the comment section

Add a comment
Know the answer?
Add Answer to:
Generate a sub/function to simulate the roll of a single die. It should return a random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT