Please help this fortran

program pair_difference

implicit none
integer :: i, x, y, n, m
integer, dimension(:), allocatable :: integer_pair
integer, dimension(:), allocatable :: result

print*, “Enter the integer pair you want to calculate.”
read*, n
print*, “Enter the integer pair.”
read*, integer_pair

m=n2
do i=1,m,2
result=diff_pair (x,y)
enddo
print
, (result(i), i=1,n)

contains

function diff_pair (a,b)
integer :: a,b,diff_pair
if (a>=b) diff_pair=a-b
if (a<b) diff_pair=b-a
end function diff_pair

end program pair_difference

When I run the code, it runs, but there is no answer because program received signal sigsegv segmentation fault - invalid memory reference is displayed. I don’t know what is the error. please Help.

You never ALLOCATEd the array integer_pair that you’re trying to use.

After that (obscured by the seg-fault), you’re trying to use variables x,y that have never been initialized.

Your (not-ALLOCATEd) array result is not compatible with being the value of an integer function.

That’s a start, at least…

Can you show me the code if it’s ok? I am a beginner in Fortran and I am not good at English, so it is difficult to understand. I’m sorry…

…and it also looks like you are a beginner to programming in general. This is not the appropriate place for your question; you need to learn how to program elsewhere first.

One “lightweight” introduction can be found at https://ourcodingclub.github.io/tutorials/fortran-intro/; for a thorough introduction, I’d recommend Chivers&Sleightholm’s book Introduction to Programming with Fortran, available among other places from Amazon, at https://www.amazon.com/Introduction-Programming-Fortran-Coverage-2003/dp/3319177001

3 Likes