- #1
volcano5683
- 12
- 0
Hi every one. I had try to make a simple program but it doesn't work because i am an amateur
The program as following:
PROGRAM: Sum_row_n_col
!
! PURPOSE: Read the 2-rank array from input disk file and calculate sums of all of the data in each row and each column in the array.
! The size of array will read from the first row of input disk file.
!
!****************************************************************************
program Sum_row_n_col
implicit none
! Data dictionary: Declare variable types and definitions
Character (len=20) :: filename ! Name of input disk data file
integer :: row ! The number of row in input array
integer :: col ! The number of column in input array
Real, allocatable, dimension (:,:) :: a ! Data array to be evaluated
integer :: i, j ! Loop index
integer :: status ! I/O status: 0 for success
real, allocatable, dimension (:) :: sum_row ! Sums of data in each row in the array
real, allocatable, dimension (:) :: sum_col ! Sums of data in each column in the array
! Initialize sums to zero
sum_row = 0.
sum_col = 0.
! Body of Sum_row_n_col
! Get the file name of the file containing the input data array
Write (*,1000)
1000 format ('Enter the file name containing input data: ')
read (*,'(A20)') filename
! Open input data file. Status is OLD because the input data file must already exist.
Open (Unit = 9, file = filename, status = 'old', action = 'read', iostat = status)
! Was opend was successful?
fileopen: if (status == 0) then
! File was opened successfully, so read the data to process.
! Read the first row of input file data as the size of array
Read (9,*, iostat = status) row, col
! Write out the size of input array
Write (*,1010) row, col
1010 format ('The size of array is: ', I2.0, 1x,'x', I2.0)
! Allocatable memory.
Write (*,*) 'Allocating a: size = ', row, col
Allocate (a(row,col), stat = status) ! Allocate memory of input array
Write (*,*) 'Allcocating sum_row: size = ', row
Allocate (sum_row(row), stat = status) ! Allocate memory of sums of each row
Write (*,*) 'Allocating sum_col: size = ', col
Allocate (sum_col(col), stat = status) ! Allocate memory of sums of each column
! Was allocation successful? If so, rewind file, read in data, and process it
allocate_ok: if (status == 0) then
Rewind (unit = 9) ! Rewind the file
!Now read in the data. We know that was enough value to fill array.
read (9,*) ((a(i,j), i = 1, row), j = 1, col) ! Get the value
! 1012 format (4(1x, F3.2))
Write (*,1011) a
1011 format (4(1x, F30.2))
! Calculate the sums of data in each row of array.
row1: do i = 1, row
row2: do j = 1, col
sum_row (i) = a (i,j) + sum_row (i)
end do row2
end do row1
! Calculate the sums of data in each column of array
col1: do j = 1, col
col2: do i = 1, row
sum_col (j) = a (i,j) + sum_col (j)
end do col2
end do col1
! Tell the user results
Write (*,*)
out1: do i = 1, row
Write (*,1020) i, sum_row (i)
1020 format ('Sum of row',I2, 1x, '=', F20.4)
end do out1
Write (*,*)
out2: do j = 1, col
Write (*,1030) j, sum_col (j)
1030 format ('Sum of column', I2, 1x, '=', F20.4)
end do out2
! Deallocate the array now that we are done
Deallocate (a, stat = status)
Deallocate (sum_row, stat = status)
Deallocate (sum_col, stat = status)
end if allocate_ok
else fileopen
! Else file open failed. Tell user
write (*,1080) status
1080 format (1X,'File open failed -- status = ', I6)
end if fileopen
end program Sum_row_n_col
____________________________________________________
Hope someones check it for me.
Thanks
The program as following:
PROGRAM: Sum_row_n_col
!
! PURPOSE: Read the 2-rank array from input disk file and calculate sums of all of the data in each row and each column in the array.
! The size of array will read from the first row of input disk file.
!
!****************************************************************************
program Sum_row_n_col
implicit none
! Data dictionary: Declare variable types and definitions
Character (len=20) :: filename ! Name of input disk data file
integer :: row ! The number of row in input array
integer :: col ! The number of column in input array
Real, allocatable, dimension (:,:) :: a ! Data array to be evaluated
integer :: i, j ! Loop index
integer :: status ! I/O status: 0 for success
real, allocatable, dimension (:) :: sum_row ! Sums of data in each row in the array
real, allocatable, dimension (:) :: sum_col ! Sums of data in each column in the array
! Initialize sums to zero
sum_row = 0.
sum_col = 0.
! Body of Sum_row_n_col
! Get the file name of the file containing the input data array
Write (*,1000)
1000 format ('Enter the file name containing input data: ')
read (*,'(A20)') filename
! Open input data file. Status is OLD because the input data file must already exist.
Open (Unit = 9, file = filename, status = 'old', action = 'read', iostat = status)
! Was opend was successful?
fileopen: if (status == 0) then
! File was opened successfully, so read the data to process.
! Read the first row of input file data as the size of array
Read (9,*, iostat = status) row, col
! Write out the size of input array
Write (*,1010) row, col
1010 format ('The size of array is: ', I2.0, 1x,'x', I2.0)
! Allocatable memory.
Write (*,*) 'Allocating a: size = ', row, col
Allocate (a(row,col), stat = status) ! Allocate memory of input array
Write (*,*) 'Allcocating sum_row: size = ', row
Allocate (sum_row(row), stat = status) ! Allocate memory of sums of each row
Write (*,*) 'Allocating sum_col: size = ', col
Allocate (sum_col(col), stat = status) ! Allocate memory of sums of each column
! Was allocation successful? If so, rewind file, read in data, and process it
allocate_ok: if (status == 0) then
Rewind (unit = 9) ! Rewind the file
!Now read in the data. We know that was enough value to fill array.
read (9,*) ((a(i,j), i = 1, row), j = 1, col) ! Get the value
! 1012 format (4(1x, F3.2))
Write (*,1011) a
1011 format (4(1x, F30.2))
! Calculate the sums of data in each row of array.
row1: do i = 1, row
row2: do j = 1, col
sum_row (i) = a (i,j) + sum_row (i)
end do row2
end do row1
! Calculate the sums of data in each column of array
col1: do j = 1, col
col2: do i = 1, row
sum_col (j) = a (i,j) + sum_col (j)
end do col2
end do col1
! Tell the user results
Write (*,*)
out1: do i = 1, row
Write (*,1020) i, sum_row (i)
1020 format ('Sum of row',I2, 1x, '=', F20.4)
end do out1
Write (*,*)
out2: do j = 1, col
Write (*,1030) j, sum_col (j)
1030 format ('Sum of column', I2, 1x, '=', F20.4)
end do out2
! Deallocate the array now that we are done
Deallocate (a, stat = status)
Deallocate (sum_row, stat = status)
Deallocate (sum_col, stat = status)
end if allocate_ok
else fileopen
! Else file open failed. Tell user
write (*,1080) status
1080 format (1X,'File open failed -- status = ', I6)
end if fileopen
end program Sum_row_n_col
____________________________________________________
Hope someones check it for me.
Thanks