Fortran What could be causing the error in SAVEDATA!KEEP subroutine?

  • Thread starter Thread starter spelux11
  • Start date Start date
  • Tags Tags
    Error Fortran95
AI Thread Summary
The discussion revolves around a Fortran95 program that aims to generate random numbers using the RANMAR subroutine. The user encounters a run-time error indicating a reference to an undefined variable within the subroutine "keep." Key issues identified include the lack of declaration for the variable "archivo," which is necessary for file operations, and the improper handling of the allocatable array "V," which is not allocated within the subroutine. Additionally, the variable "N" is declared but not initialized, leading to potential garbage values. Suggestions emphasize passing "V" and "N" as parameters to the subroutine to resolve these issues and eliminate the run-time error.
spelux11
Messages
1
Reaction score
0
Hi,
I'm new in this space, I have a problem building a program with Fortran95 (I'm also new on this). The program it's really easy, I have to make a random generator using RANMAR subroutine here is the code:

program random
use aleatorio; use SAVEDATA
Implicit none
real, allocatable :: V(:)

integer N

write(*,*) "Choose the numbers"
read (*,*) N

allocate (V(N))

call RANMAR (V,N)

write(*,*) "Numbers:",V

call keep()
deallocate (V)
stop
end program random

module SAVEDATA


contains

subroutine keep()

real, allocatable :: V(:)
integer N

integer :: i
character (len=30)::fileb

write (*,*) 'what is the name of your file?'
read (*,*) archivo

open (10,file=fileb, access='append')
do i= 1,N
write (10, *) V(i)
enddo

end subroutine keep
end module SAVEDATA

I've got problems with this subroutine, the other one works! here is the error warning:

Run-time Error
*** Error 112, Reference to undefined variable, array element or function result (/UNDEF)

SAVEDATA!KEEP - in file salvar.f95 at line 24 [+019c]

main - in file main.f95 at line 29 [+031a]

If somebody could tell me something about it, I'd be so thankful.

SPELUX11
 
Technology news on Phys.org
spelux11 said:
Hi,
I'm new in this space, I have a problem building a program with Fortran95 (I'm also new on this). The program it's really easy, I have to make a random generator using RANMAR subroutine here is the code:

program random
use aleatorio; use SAVEDATA
Implicit none
real, allocatable :: V(:)

integer N

write(*,*) "Choose the numbers"
read (*,*) N

allocate (V(N))

call RANMAR (V,N)

write(*,*) "Numbers:",V

call keep()
deallocate (V)
stop
end program random

module SAVEDATA


contains

subroutine keep()

real, allocatable :: V(:)
integer N

integer :: i
character (len=30)::fileb

write (*,*) 'what is the name of your file?'
read (*,*) archivo

open (10,file=fileb, access='append')
do i= 1,N
write (10, *) V(i)
enddo

end subroutine keep
end module SAVEDATA

I've got problems with this subroutine, the other one works! here is the error warning:

Run-time Error
*** Error 112, Reference to undefined variable, array element or function result (/UNDEF)

SAVEDATA!KEEP - in file salvar.f95 at line 24 [+019c]

main - in file main.f95 at line 29 [+031a]

If somebody could tell me something about it, I'd be so thankful.

SPELUX11

The error refers to line 24 of the file salvar.f95, so that's where I would look first. I would also look at line 29 of the file main.f95. Since you haven't said which file we're looking at, I can't say where the problem lies.

However, I notice a few things about your keep() subroutine.
1. archivo is not declared, but should be.
2. The vector V in your subroutine is declared as allocatable, but you don't allocate any space for it. It would be better to pass V and N as parameters in your subroutine.
3. N is declared in your subroutine, but it is never initialized, so it will contain a garbage value.

Likely it is one or more of the above that is causing your run-time error.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top