Set-IV
C input1.f WAP in Fortran to calculate the sum of the columnwise arranged
C data of an input file input1.dat where input1.dat is given.
! Set IV, question 1, input1.f
Real a(20), s
Open (unit-1, file = ‘input1.dat’)
Do I = 1, 5
Read(1,*) a(i)
enddo
s=0
S=0
Do I = 1, 5
s=s+a(i)
enddo
write(*,*) ‘The required sum is’, s
stop
end
C output
C The output
C The required sum is 15.00
C input1.dat
C 1
C 2
C 3
C 4
C 5
C input2.dat
C WAP in Fortran to take data from an input data file *input2.dat) where the data are
C arranged in two columns. Perform the row-wise sum of the data and construct the third
C column using these sum and save all the three columns using in an output data file
C (output2.dat)
Integer a(10,10), s(10)
Open (unit = 1, file = ‘input2.dat’)
Open (unit = 2, file = ‘output2.dat’)
Do I = 1, 5
Read (1, *) (a(I,j), j = 1, 2)
Enddo
Do i=1, 5
a(i,3) = a(i,1)+a(i,2)
enddo
do i = 1, 5
write(2, *) (a(i,j), j = 1, 3)
enddo
stop
end
C ---------------------------------------------
C input2.dat
C 1 5
C 2 4
C 3 3
C 4 2
C 5 1
C --------------------------------------------------------------
C output2.dat
C 1 5 6
C 2 4 6
C 3 3 6
C 4 2 6
C 5 1 6
C ---------------------------------------------------------------------
C ----------------------------------------------------------------------------------
C input3.f
C WAP in Fortran to take data from an input file (input3.dat) where the data are
C haphazardly arranged. Arrange these data in a row-wise format and save the
C result in an output file (output3.dat)
C
Integer a(100), n
Open(unit=1, file= ‘input3.dat’)
Open(unit=2, file = ‘output3.dat’)
Write(*,*) ‘Give the number of term’
Read(*,*) n
Read (1, *) (a(i), i=1, n)
Write(2,*) (a(i), I = 1, n)
Write(*,*) ‘Your output file has been saved in output3.dat
Stop
End
C output
C Give the number of terms
C 10
C Your output file has been successfully saved in output3.dat
C input3.dat
C 1, 2, 3, 4, 5, 6
C 7, 8, 9, 10
C 45, 61, 90, 21, 67, 100, 36, 9
C ----------------------------------------------------------------------
C output3.dat
C 1 2 3 4 5 6 7 8 9 10
C -----------------------------------------------------------------------------------------
C output4.f
C WAP in Fortran to take data from an input file (input4.dat) (where the data are
C kept haphazardly) amd arrange the data in two columns and save the result in an
C output data file (output4.dat)
Integer a(100), b(50, 50)
Open(unit=1, file= ‘input4.dat’)
Open(unit=2, file=’output4.dat’)
Read(1,*) (a(i), i=1, 14)
K=1
Do I = 1, 2
Do j = 1, 7
b(i,j) = a(k)
k = k +1
enddo
enddo
do j = 1, 7
write(2,*) (b(i,j), i = 1, 2)
enddo
write(*,*) ‘Your data are saved in output4.dat
stop
end
C -----------------------------------------------------------------------------
C output
C your output file has been successfully created as output4.dat
C input4.dat
C 1, 2, 3, 4, 5, 6
7, 8, 9, 10
45, 61, 90, 21, 67, 100, 36, 9
-------------------------------------------------------------------
C output4.dat
C 1 8
C 2 9
C 3 10
C 4 45
C 5 61
C 6 90
C 7 21
---------------------------------------------------
C input5.f
C WAP in Fortran, which takes data from an input file input5.dat (where the data
C are kept row-wise). Arrange these data into ascending and descending order
C both. Save all the three results in three columns in an output data file
C (output5.dat)
Integer i, j, a, b, c, d, n
Real x(100), y(100), w(100), z(100, 100)
Open (unit=1, file = ‘input5.dat’)
Open (unit=2, file = ‘output5.dat’)
Write(*,*) ‘Give the term number’
Read (*,*) n
Read(1,*) (x(i), i = 1, n)
Do i = 1, n
y(i) = x(i)
w(i) = x(i)
enddo
do i = 1, n-1
do j = i+1, n
If (y(i).gt.y(j)) Then
a = y(i)
b = y(j)
y(i) = b
y(j) = a
Endif
Enddo
Do I = 1, n
W(i) = y(n+1-i)
Enddo
Do I = 1, n
Z(i,1) = x(i)
Z(i,2)=y(i)
Z(i,3) = w(i)
Enddo
Do i = 1, n
Write(2,3) (z(i,j), j = 1, 3)
Enddo
3 Format (3(2x, F6.1))
Write(*,*) ‘Data have been save in output file output5.dat’
Stop
End
C -----------------------------------------------------
C output
C Give the term number
C 10
C output data has been saved in output5.dat
--------------------------------------------------------------
C input5.dat
C 5, 7, 3, 9, 2, 13, 17, 4, 8, 1, 15, 19, 11, 14, 6
C ------------------------------------------------------------
C output5.dat
C 5.0 1.0 17.0
C 7.0 2.0 13.0
C 3.0 3.0 9.0
C 9.0 4.0 8.0
C 2.0 5.0 7.0
C 13.0 7.0 5.0
C 17.0 8.0 4.0
C 4.0 9.0 3.0
C 8.0 13.0 2.0
C 1.0 17.0 1.0
---------------------------------------------------------------------------------