Simple reading function

I guess this is something stupid that I missed, but I asked my whole class and no one can figure it out. Making a simple program that calls in a subroutine and I'm having trouble reading the do loop in matrix entries.

program Householder_Program

use QR_Factorisation
use numeric_kinds

complex(dp), dimension(:,:), allocatable :: A, Q, R, V
integer :: i, j, n, m

print *, 'Enter how many rows in the matrix A'
read *, m

print *, 'Enter how many columns in the matrix A'
read *, n

allocate(A(m,n), Q(m,n), R(n,n), V(n,n))

do i = 1,m

    do j = 1,n

        Print *, 'Enter row', i, 'and column', j, 'of matrix A'
        read *, A(i,j)

    end do

end do

call Householder_Triangularization(A,V,R,n,m)

print *, R

end program

      

It will ask me for A (1,1), but when I dial the number, it will not ask me for A (1,2), it will leave a blank line. When I try to enter the second number it will fail and say:

 Enter row           1 and column           1 of matrix A
 1
 2
 At line 22 of file HouseholderProgram.f90 (unit = 5, file = 'stdin')
 Fortran runtime error: Bad repeat count in item 1 of list input

      

+3


source to share


3 answers


Your variable A

is (array) of type complex. This means that when you try to enter values ​​with a list of item values, you cannot just specify a single number. So, in your case, the problem is not with the program, but with the input.

From Fortran 2008 standard, 10.10.3



When the next effective element is of complex type, the input form consists of a left parenthesis followed by an ordered pair of numeric input fields, separated by commas (if decimal editing mode is POINT) or semicolons (if decimal rule mode is COMMA), followed by a right parenthesis ...

Input, then, should be something like (1., 12.)

.

+4


source


You are trying to read in tricky numbers ( A

is tricky)! Thus, you have to specify complex numbers for the code ... Since you only provide one integer, the program doesn't know what to do.



Providing (1,0)

both (2,0)

instead of 1

and 2

will do the trick.

+3


source


If the user input is always large and you want to read it into an array of complex types, you can do something like this:

    Print *, 'Enter row', i, 'and column', j, 'of matrix A'
    read *, dummy
    A(i,j)=dummy

      

where dummy

declared real

. This saves the user from having to insert the parentheses required for complex numbers. (Conversion to complex is done automatically)

+2


source







All Articles