Traveling Sales Man Fortran 90 program. My program compiles and runs just the ou
ID: 3565872 • Letter: T
Question
Traveling Sales Man Fortran 90 program. My program compiles and runs just the output is not what I want. It needs very minor tweeking. please fix and submit your answer. program will read from a data file similar to the one below and will output the shortest route from the datafile.
This is my code:
PROGRAM p4
INTEGER :: number, status, I, i, j, permutation = 0, distance = 0, distance = 999999
CHARACTER(50) :: filename, test ! Filenames longer than 50 are truncated
CHARACTER(20), DIMENSION(10) :: city
INTEGER, DIMENSION(10,10) :: d_table
INTEGER, DIMENSION(100) :: path, best_path
WRITE (*, '(1x,A)', ADVANCE="NO") "Enter filename: "
READ *, filename
! Open the file we created and read the contents
OPEN(UNIT=15, FILE=filename, STATUS="OLD", ACTION="READ",&
IOSTAT=status)
IF(status /= 0) THEN
PRINT *, "ERROR, could not open file for reading."
STOP
END IF
read(15,*) number
do i = 1, number
path(i-1) = i
read(15,*, IOSTAT=status) city(i)
IF(status < 0) THEN ! Stop at EOF
EXIT
END IF
do j = 1, number
read(15, *) d_table(i,j)
end do
end do
CLOSE(15)
CALL permute(0,number)
CONTAINS
RECURSIVE SUBROUTINE permute(first, last)
INTEGER, INTENT(IN) :: first, last
INTEGER :: i, temp
IF(first == last) THEN
distance = d_table(1,path(2))
PRINT *, city(1), city(path(2)), " ", d_table(1, path(2))
DO i=2, last-1
distance = distance + d_table(path(i),path(i+1))
print *, city(path(i)), " ", city(path(i+1)),&
d_table(path(i),path(i+1))
END DO
distance = distance + d_table(path(last),path(1))
PRINT *, city(path(last))," ",city(path(1)), &
d_table(path(last),path(1))
PRINT *, "Distance is ",distance,"miles"
PRINT *
IF(distance < best_distance) THEN
best_distance = distance
DO i=2, count
best_path(i) = path(i)
END DO
END IF
ELSE
DO i=first, last
temp = path(first)
path(first) = path(i)
path(i) = temp
call permute(first+1,last)
temp = path(first)
path(first) = path(i)
path(i) = temp
END DO
END IF
END SUBROUTINE permute
END PROGRAM p4
Out needs to be like this
The Data File is this below
To make processing easier, there is only one item per line in the file. The above data would appear in the file this way: Output: Your program must print the shortest route found, including the name of each city in the order visited the distance traveled to get there, and the total number of miles traveled. Sample output from the above data file:Explanation / Answer
INTEGER :: number, status, I, ix, jx, permutation = 0, distance = 0, distance = 999999 CHARACTER(50) :: filename, test ! Filenames longer than 50 are truncated CHARACTER(20), DIMENSION(10) :: city INTEGER, DIMENSION(10,10) :: d_table INTEGER, DIMENSION(100) :: path, best_path WRITE (*, '(1x,A)', ADVANCE="NO") "Enter filename: " READ *, filename ! Open the file we created and read the contents OPEN(UNIT=15, FILE=filename, STATUS="OLD", ACTION="READ",& IOSTAT=status) IF(status /= 0) THEN PRINT *, "ERROR, could not open file for reading." STOP END IF read(15,*) number do ix = 1, number path(ix-1) = ix read(15,*, IOSTAT=status) city(ix) IF(status < 0) THEN ! Stop at EOF EXIT END IF do jx = 1, number read(15, *) d_table(ix,jx) end do end do CLOSE(15) CALL permute(0,number) CONTAINS RECURSIVE SUBROUTINE permute(first, last) INTEGER, INTENT(IN) :: first, last INTEGER :: i, temp IF(first == last) THEN distance = d_table(1,path(2)) PRINT *, city(1), city(path(2)), " ", d_table(1, path(2)) DO i=2, last-1 distance = distance + d_table(path(i),path(i+1)) print *, city(path(i)), " ", city(path(i+1)),& d_table(path(i),path(i+1)) END DO distance = distance + d_table(path(last),path(1)) PRINT *, city(path(last))," ",city(path(1)), & d_table(path(last),path(1)) PRINT *, "Distance is ",distance PRINT * permutations = permutations + 1 IF(distanceRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.