From 7e18ae9e162d20a2878c2a5241ccb562f0353aed Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Mon, 6 Mar 2023 01:45:53 -0600 Subject: [PATCH] fixed issue with matrix reader function --- PA003/gaussian_elimination.f90 | 12 ++++++++++-- PA003/mymodule.f90 | 31 ++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/PA003/gaussian_elimination.f90 b/PA003/gaussian_elimination.f90 index bbe2f5a..ab66bd7 100644 --- a/PA003/gaussian_elimination.f90 +++ b/PA003/gaussian_elimination.f90 @@ -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 diff --git a/PA003/mymodule.f90 b/PA003/mymodule.f90 index 648edef..619ff85 100644 --- a/PA003/mymodule.f90 +++ b/PA003/mymodule.f90 @@ -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