Answered step by step
Verified Expert Solution
Question
1 Approved Answer
this is a fortran problem. Part A Tasks 1. Type the following program exactly as shown: ! This program generates a table of the square
this is a fortran problem.
Part A Tasks
1. Type the following program exactly as shown:
! This program generates a table of the square roots of integers
! in a user-specified range.
program squares
implicit none
integer start, finish, i
! ----------------------------------------------Prompt and Input print*, "Enter the range (two integers) ..."
read*, start, finish
! ----------------------------------------------Validation & Processing
if (start .GT. finish) then
print*, "Invalid: the start must be less than the end value!"
else if (start .LT. 0) then
print*, "Invalid: the range must not have negative values!"
else
do i = start, finish
write(*,*) i, sqrt(i*1.)
end do
end if
! ----------------------------------------------
end
2. Compile the program as you learned in Lab #1.
Experiments
1. When run, the program prompts you to specify a range of positive integers by entering two integers, being the beginning and end of the range. You can enter the two end-points by separating them by a space or a comma or by pressing ENTER. Try: 1,20 and examine the output.
2. Re-run the program and test invalid ranges; e.g. -10, 10 or 10, 2. Note how the program ends with an error message even though its code flows "smoothly"; without end statements in the middle of it.
3. The argument of the square root function looks strange: Why did we have to mutiply i by 1? Experiment, then explain.
Questions
4. Add the following statement before the prompt but after declarations:
open (unit=50, file='sqroot.txt'). Furthermore, change the first asterisk in the write statement (inside the loop) to 50. These changes cause the output of the write statement to be redirected: Instead of appearing on the screen, it goes to the named disk file. Verify this by viewing the generated sqroot.txt file.
Note:
o You can view the file by issuing the following DOS command: type sqroot.txt|more.
o If you prefer to create the file elsewhere on the hard drive; i.e. not in the same directory as the program, prefix the filename by an appropriate path; in Windows: e.g. 'C:\\sqroot.txt' will store it in the root directory of drive C: . (since the backslash character has a special meaning to this compiler, we type two backslashes to escape the special handling). On the Mac: use e.g. '/User//Documents/sqroot.txt'
5. Restore the program back to screen output and modify it so that the square root appears formatted with only two decimals.
6. Modify the program so that it does not generate any output for integers that are multiples of 5. For example, if the range is 1 to 18, then no lines will be printed for 5, 10 nor 15.
Part B Tasks
1. Type the following program exactly as shown:
! ------- The Effects of Compound Interest -------
! This program calculates the value of an investment
! year-by-year to age 65. The investment grows at a
! given interest rate and the same amount is added
! each year.
program Retire
implicit none
integer age, i
real annualAmt, investment, interestRate
print*, "Enter the amount of your annual contribution:"
read*, annualAmt
print*, "Enter the interest rate
read*, interestRate
print*, "Enter your start age: "
read*, age
print*, "Annual contribution is:
print*, "Annual interest rate :
print*, "Your current age is :
print*, "age investment"
investment = 0.0
do i = age+1, 65
! add annual contribution at beginning of the year
investment = investment + annualAmt
! calculate value of investment at end of the year
investment = investment + investment
interestRate / 100
write(*,12) i, investment
12 format(1x, I5, F12.2)
end do
stop
end
2. Compile the program as you learned in Lab #1.
Experiments
3. Run the program with various inputs to study the effect of compound interest.
Questions
4. Modify the program so that the stop age is also an input, and further modify the program that it checks that the start age is less than the stop age and both are greater than zero. The program will continue to compound the investment to age 65, but will stop adding annual contributions after the stop age.
Experiments
5. It is claimed that starting to save early and stopping after several years is better than starting late and saving to age 65. Explore various scenarios to validate or refute this claim. For example, how does starting at age 20 and stopping at age 25 compare to starting at 40 and not stopping until age 65.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started