fixed issue with matrix reader function

This commit is contained in:
RustyRaptor committed 2023-03-06 01:45:53 -06:00
1 parent 3627c187f5
commit 7e18ae9e16
2 files changed
+32 -11

No files matched your search

+10 -2
View File
@@ -1,5 +1,6 @@
program read_matrix_from_args
use mymodule
use iso_fortran_env
implicit none
integer :: i, j, n, m, index_of_input, s
! real :: next_argument_real
@@ -10,6 +11,7 @@ program read_matrix_from_args
real, dimension(:,:), allocatable :: matrix
real, dimension(:), allocatable :: input_list
REAL,DIMENSION(:), allocatable:: result_vector
real(kind=8) :: elapsed_time, start_time, end_time
file_unit = 10
! Get the number of rows and columns from the command line
@@ -47,7 +49,13 @@ program read_matrix_from_args
allocate(input_list(n*m))
allocate(result_vector(n))
call cpu_time(start_time)
print *, "BEGIN"
input_list = string_to_real_array(input_array, n*m)
call cpu_time(end_time)
elapsed_time = end_time - start_time
print *, "Elapsed time:", elapsed_time, "seconds"
! write (*, '(f20.5)') input_list(1)
@@ -58,8 +66,8 @@ program read_matrix_from_args
do i = 1, n
do j = 1, m
matrix(i,j) = input_list(index_of_input)
! write ( * , '(f20.5)', advance="no") matrix(i,j)
! write ( * , '(f20.5)') input_list(index_of_input)
!write ( * , '(f0.20)', advance="no") matrix(i,j)
!write ( * , '(f0.20)') input_list(index_of_input)
index_of_input = index_of_input + 1
end do
end do
+22 -9
View File
@@ -7,23 +7,36 @@ pure function string_to_real_array(str, n) result(arr)
implicit none
character(len=*), intent(in) :: str
character(len=:), allocatable :: str_new
character(len=10) :: index_str
character(len=10000) :: num_str
character(len=10000) :: index_str
integer, intent(in) :: n
real, dimension(:), allocatable :: arr
integer :: i, status, index_int
integer :: i, status, index_int, j, k
str_new = str
! Allocate memory for the real array
allocate(arr(n))
! Read each real number from the string and store it in the array
i = 1
do while (len_trim(str_new) > 0)
read(str_new, '(f2.20)', iostat=status) arr(i)
! i = 1
! do while (len_trim(str_new) > 0)
! read(str_new, '(f2.20)', iostat=status) arr(i)
i = i + 1
index_int = index(str_new, ' ')
str_new = adjustl(str_new(index_int+1:))
end do
! i = i + 1
! index_int = index(str_new, ' ')
! str_new = adjustl(str_new(index_int+1:))
! end do
! Read each real number from the string and store it in the array
i = 1
j = 1
do while (j <= n)
k = index(str_new(j:), ' ')
if (k == 0) k = len(str_new(j:)) + 1
num_str = str_new(j:j+k-2)
read(num_str, '(f20.5)', iostat=status) arr(i)
i = i + 1
j = j + k
end do
end function string_to_real_array