Download Lab 4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Halting problem wikipedia , lookup

Transcript
CS141 Lab #4
You will write two programs to complete this lab.
The first program will be able to perform two different tasks.
1) Given an annual interest rate and an initial balance, and a number of months,
determine the final balance of a savings account. Interest should be compound monthly.
The program should insist that the interest rate is larger than zero. The program should
similarly insist that the initial balance is greater than zero and the number of month are
greater than or equal to zero.
2.) Given an annual interest rate and an initial balance, determine how many months it
will take for the balance to exceed $1,000,000.00.
Requests for input should appear exactly as they do in the examples.
Example 1:
1) Find final balance
2) Find months for balance to reach $1,000,000.00
Select problem type: 3
Problem types must be in the range 1 to 2.
Select problem type: 1
Initial balance: 100
Number of months: 12
Yearly interest rate: .1
Final balance: $110.471
Example 2:
1) Find final balance
2) Find months for balance to reach $1,000,000.00
Select problem type: 2
Initial balance: 100
Yearly interest rate: .1
Balance will exceed $1,000,000.00 in 92 years and 6 months.
You must use loops to solve each of these two problems. Inside the loop you will
calculate the interest rate for each month. The monthly interest rate is just the yearly rate
divided by 12.
Here is an algorithm for problem type 2.
Prompt the user for the balance until the user provides a balance > 0
Prompt the user for an annual interest rate until the user provides a rate > 0
Compute the monthly interest rate by dividing the annual rate by MONTHS_PER_YEAR
Set the monthCounter = 0
While the balance is less than the TARGET_BALANCE
Update the balance to balance + balance * monthly interest rate
Add one to the month counter
Write the algorithm for problem type 1 before you write the program, and include the
algorithm in your comments.
The second program will be able to compute a Fibonacci sequence. A Fibonacci
sequence begins with 0, 1 and computes each new number in the sequence as the sum of
the previous two number in the sequence. So the third number in the sequence is 0 + 1 =
1. The next numbers are 1+1=2, 1+2=3, and 3+2 = 5.
So the sequence proceeds:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …
As we did in the previous program, we want to give the user two options:
1) Generate a Fibonacci sequence of size n, where n is a positive integer input from
the user.
2) Determine the first number in the Fibonacci sequence that exceeds a user
specified integer. Report both the Fibonacci number, and its position in the
sequence (e.g. 0 has position 1, 1 has position 2, 3 has position 5, etc.).
Write the algorithm for the program first. Include the algorithm in your comments.