Write a FORTRAN program that accepts from a valid file a lis
Write a FORTRAN program that accepts from a valid file a list of real numbers into a single dimensioned array, sorts that array in ascending order using the bubble sort algorithm, and display the sorted results appropriately formatted. The program shall obtain and validate an input file name specified by the program user. Number of values in the file can not exceed 1000. Assume that the values will be between -999.99 and +999.99 with two digits of fractional precision.
Solution
INTEGER :: i, j, x,temp, y
INTEGER :: a(100)
PRINT*, “ENTER THE SIZE OF THE ARRAY”
READ*, x
PRINT*, “SIZE OF THE ARRAY IS “,x
PRINT*, “ENTER THE ELEMENTS OF THE ARRAY”
DO i=1, x, 1
READ*,a(i)
END DO
PRINT*, “THE ARRAY IS AS FOLLOW:”
DO i=1, x, 1
PRINT*, a(i)
END DO
PRINT*, “PRESS 1 TO SORT THE ARRAY IN ASCENDING ORDER”
PRINT*, “PRESS 2 TO SORT THE ARRAY IN DESCENDING ORDER”
READ*, y
IF (y = = 1) THEN
DO i =1, x-1, 1
DO j =1, x-1, 1
IF (a(j)>a(j+1)) THEN
temp = a(j)
a(j) = a(j+1)
a(j+1) = temp
END IF
END DO
END DO
END IF
IF (y = = 2) THEN
DO i =1, x-1, 1
DOj=1, x-1, 1
if(a(j)<a(j+1)) THEN
temp = a(j)
a(j) = a(j+1)
a(j+1) = temp
END IF
END DO
END DO
END IF
PRINT*, “AFTER SORTING”
DO j=1, x, 1
PRINT*, a(j)
END DO
STOP
END
OUTPUT OF THE PROGRAM:
INPUT:
ENTER THE SIZE OF THE ARRAY
5
SIZE OF THE ARRAY IS 5
ENTER THE ELEMENTS OF THE ARRAY
1
2
3
4
5
THE ARRAY IS AS FOLLOW:
1
2
3
4
5
PRESS 1 TO SORT THE ARRAY IN ASCENDING ORDER
PRESS 2 TO SORT THE ARRAY IN DESCENDING ORDER
2
OUTPUT:
AFTER BUBBLE SORTING:
5
4
3
2
1

