Fortran How can I change the starting index of an array in Fortran?

  • Thread starter Thread starter Giammy85
  • Start date Start date
  • Tags Tags
    Fault Fortran90
AI Thread Summary
The discussion revolves around a Fortran program that encounters a segmentation fault due to improper array indexing. The user attempts to read files from subdirectories and create output files, but the issue arises when the program tries to access the first element of an array with a zero index. In Fortran, arrays are 1-based by default, and attempting to assign a value to `fileout(0)` leads to unexpected behavior. The solution involves either adjusting the array declaration to allow for a zero index or adhering to the 1-based indexing convention by starting the loop from 1. The conversation emphasizes the importance of understanding array indexing in Fortran to avoid such errors.
Giammy85
Messages
19
Reaction score
0
I'm try to read some files in subdirectories and create some other files in my present location:

program multiread
!
implicit none

!variables
integer :: n, k, i
integer :: j, m2
real(8) :: pratio, crap, rmsttv
character(len=42) :: filein(98)
character(len=10) :: fileout(14)
character (LEN=26) :: path
character(len=2) number, mass

j = 1
do i=0, 6
m2=5*i
if (m2==0) then
m2=1
end if
write(mass,998) m2
998 format(i2.2)
write(*,*) j
fileout(i)=mass//'_000.dat'
write(*,*) fileout(i),j
open(unit=i+10,file=fileout(i),status='replace',access="append")
! write(*,*) j
do n=0,6
pratio=1.5+0.25*n
! write(*,*) pratio
write(number,998) j
! write(*,*) j
! 999 format(i2.2)
write(*,*) number
path='/scratch/project3/set1/'//number//'/'
filein(j)=path//'periodrmsttv.dat'
write(*,*) filein(j)
open(unit=j+40,file=filein(j),status='old')

read (j+40,*) crap
read (j+40, '(f14.6)') rmsttv
write (i+10,'(f4.2,f14.6)') pratio, rmsttv
close (j+40)

j=j+2
end do
close (i+10)
end do

end program multiread

I receive a segmentation fault message: debugging I have found the value of j change from 1 to a random number just after having defined fileout(i)=mass//'_000.dat'
How to solve this problem?
Thanks
 
Technology news on Phys.org
When you post a code sample, put [ code ] and [ /code ] tags around it (omit the spaces I showed). This preserves your indentation and makes your code easier to read. I have done that below.
Giammy85 said:
I'm try to read some files in subdirectories and create some other files in my present location:
Code:
program multiread
!
implicit none 

!variables
integer :: n, k, i
integer :: j, m2
real(8) :: pratio, crap, rmsttv
character(len=42) :: filein(98)
character(len=10) :: fileout(14)
character (LEN=26) :: path
character(len=2) number, mass

j = 1
do i=0, 6
m2=5*i
if (m2==0) then
m2=1
end if
	   write(mass,998) m2
	998  format(i2.2)
      write(*,*) j
	   fileout(i)=mass//'_000.dat'
      write(*,*) fileout(i),j
	   open(unit=i+10,file=fileout(i),status='replace',access="append")
 !     write(*,*) j
	do n=0,6	
	pratio=1.5+0.25*n
!      write(*,*) pratio
	      write(number,998) j
!      write(*,*) j
!	 999  format(i2.2)
      write(*,*) number
		path='/scratch/project3/set1/'//number//'/'
	      filein(j)=path//'periodrmsttv.dat'
      write(*,*) filein(j)
	      open(unit=j+40,file=filein(j),status='old')

		read (j+40,*) crap
		read (j+40, '(f14.6)') rmsttv
		write (i+10,'(f4.2,f14.6)') pratio, rmsttv
		close (j+40)

	j=j+2
	end do
close (i+10)
end do

end program multiread
I receive a segmentation fault message: debugging I have found the value of j change from 1 to a random number just after having defined fileout(i)=mass//'_000.dat'
How to solve this problem?
Thanks

I believe this is your problem:
Code:
do i=0, 6
  m2=5*i
  if (m2==0) then
  m2=1
  end if
  write(mass,998) m2
  998 format(i2.2)
  write(*,*) j
  fileout(i)=mass//'_000.dat'
  .
  .
  .
end do
Arrays in Fortran are 1-based, by default[/color], which means that the smallest index is 1. In your code, i starts out at 0, and you attempt to store the value of mass in fileout(0).
 
Last edited:
Mark44 said:
Arrays in Fortran are 1-based, which means that the smallest index is 1. In your code, i starts out at 0, and you attempt to store the value of mass in fileout(0).

You can declare a Fortran array with any integer bounds if you want, for eaxmple
Code:
character(len=10) :: fileout(0:14)
makes the smallest index 0 not 1.

But if you are going to use Fortran a lot, you need to get used to working with arrays that start from index 1.
 
Thanks, solved!
 
AlephZero said:
You can declare a Fortran array with any integer bounds if you want, for eaxmple
Code:
character(len=10) :: fileout(0:14)
makes the smallest index 0 not 1.
I thought there would be a way to do it. What I meant to say was that by default, an array is 1-based.
AlephZero said:
But if you are going to use Fortran a lot, you need to get used to working with arrays that start from index 1.
 
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 had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
6
Views
2K
Replies
2
Views
1K
Replies
11
Views
3K
Replies
2
Views
2K
Replies
16
Views
4K
Replies
22
Views
5K
Replies
8
Views
4K
Back
Top