1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Question of fortran program

  1. Feb 3, 2012 #1
    1. The problem statement, all variables and given/known data
    Hello.

    A student came to me by yesterday with a question like this:

    Here is a sample file (sample.dat),

    1 0
    2 0
    3 0
    4 0
    5 0
    6 0
    7 0
    8 0
    9 0
    10 0

    and also with many result files, such as result_1.dat:

    1 0.1
    2 0.2
    3 0.3

    or result_2.dat

    1 0
    5 10
    9 0.3
    10 20

    The student would like to write a fortran to sum numbers at the second column. (so if we take only two result files, the final 'result' will be:)

    1 0.1
    2 0.2
    3 0.3
    4 0
    5 10
    6 0
    7 0
    8 0
    9 0.3
    10 20

    I tried to solve this question but it doesn't work well, so far my program looks like this:

    program Main
    implicit none

    integer GetFileN,ios,nmax1,nmax2
    character(160) filename,tmp
    integer i,j,m,n
    integer x(1:20),a(1:20)
    real y,b
    integer summ

    filename='result_'
    print*, 'Please enter the number of files:'
    read*,n
    do m=1,n
    write(tmp,*)m

    open(1,file=trim(filename)//trim(adjustl(tmp))
    1//'.dat',status='old' )
    nmax1=GetFileN(1)
    do i = 1,nmax1
    read(1,*,iostat=ios) x(i),y
    if(ios /=0) then
    exit
    endif
    c write(6,*) x(i),y

    open(10,file='sample.dat',status='old')
    nmax2=GetFileN(10)
    do j=1,nmax2
    read(10,*,iostat=ios)a(j),b
    if(ios/=0) then
    exit
    endif
    c write(6,*)a(j),b

    summ=0
    if (any(x==a)) then
    summ=b+y
    else
    summ=b
    endif
    open(11,status='unknown',file='meanlifetime.dat')
    write(11,*)a(j),summ
    enddo
    enddo
    enddo
    close(1)
    close(10)
    close(11)
    end

    integer function GetFileN(iFileUnit)
    implicit none
    logical , parameter :: b = .True.
    integer , intent( IN ) :: iFileUnit
    character*(1) :: c
    GetFileN = 0
    rewind( iFileUnit )
    do while (b)
    read( iFileUnit , * ,end =999 ,Err = 999 )c
    GetFileN = GetFileN + 1
    end Do
    999 rewind( iFileUnit )
    return
    end function GetFileN

    Does anyone have an idea?
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?



Similar Discussions: Question of fortran program
  1. Mathcad programming (Replies: 0)

  2. Fortran Project (Replies: 0)

Loading...