! Based on some depreciation rate(s), this program calculates the depreciated price of ! a car at the end each year until the depreciated price is the same as or below ! some given price (the final depreciated price). It is assumed that ! depreciation of a car occurs in three different phases. During a phase, the ! depreciation rate is constant. The last phase (3rd phase) continues for the rest of ! the life of the car. ! Input file: rate.dat ! Line 1: Original price of the car and the final depreciated price ! Line 2: Depreciation rate and years for phase 1 ! Line 3: Depreciation rate and years for phase 2 ! Line 4: Depreciation rate for phase 3 ! Output file: price.out ! For each year, the program outputs a line with: ! year, price, depreciation rate, depreciation, and depreciated value ! Caution: You have to be careful in creating the input file for testing. ! If you type any depreciation rate 0.0 by mistake, the program may never end. ! Note: Depending on the input data set, the program may not execute all three loops ! to generate output. As soon as the condition for the final depreciated value is met, it ! skips other following loops. That is, not all cars will go through all three phases. PROGRAM calc_price_depreciation IMPLICIT NONE REAL :: depreciation_rate_1, depreciation_rate_2, & depreciation_rate_3, depreciation, & depreciated_price, car_price, final_depreciated_price INTEGER :: years_1, years_2, year INTEGER :: ios INTEGER :: i ! Open input file OPEN (9, FILE="rate.dat", STATUS ="OLD", IOSTAT=ios) IF (.NOT.(ios == 0)) THEN WRITE (*,*) "*** Cannot open input file ***" STOP END IF ! Open output file OPEN (10, FILE="price.out", STATUS="REPLACE",IOSTAT=ios) IF (.NOT.(ios == 0)) THEN WRITE (*,*) "*** Cannot open output file ***" STOP END IF ! Read depreciation rates and years READ(9,*,IOSTAT=ios) car_price, final_depreciated_price READ(9,*,IOSTAT=ios) depreciation_rate_1, years_1 READ(9,*,IOSTAT=ios) depreciation_rate_2, years_2 READ(9,*,IOSTAT=ios) depreciation_rate_3 IF (ios < 0) THEN WRITE(*,*) "*** INSUFFICIENT OR INVALID DATA IN INPUT FILE ***" STOP END IF ! Write heading lines WRITE (10,*) " Depreciation Table" WRITE (10,*) WRITE (10,*) "Year Price %Rate Depreciation Depreciated Value" WRITE (10,*) "-----------------------------------------------------------" ! Calculations for phase 1 i = 0 year = 0 DO WHILE (i < years_1 .AND. car_price > final_depreciated_price) depreciation = depreciation_rate_1 * car_price i = i + 1 year = year + 1 depreciated_price = car_price - depreciation WRITE (10, 50) year, car_price, & depreciation_rate_1 * 100, depreciation, & depreciated_price car_price = depreciated_price END DO ! Calculations for phase 2 i = 0 DO WHILE (i < years_2 .AND. car_price > final_depreciated_price) depreciation = depreciation_rate_2 * car_price i = i + 1 year = year + 1 depreciated_price = car_price - depreciation WRITE (10, 50) year, car_price, & depreciation_rate_2 * 100, depreciation, & depreciated_price car_price = depreciated_price END DO ! Calculations for phase 3 DO WHILE (car_price > final_depreciated_price) depreciation = depreciation_rate_3 * car_price year = year + 1 depreciated_price = car_price - depreciation WRITE (10, 50) year, car_price, & depreciation_rate_3 * 100, depreciation, & depreciated_price car_price = depreciated_price END DO CLOSE (9) CLOSE (10) STOP 50 FORMAT (I5, F10.2, F10.2, F10.2, TR12, F10.2) END PROGRAM calc_price_depreciation