How to Write a Fortran Program to Sum Numbers in Multiple Result Files?

In summary, the student's question involves summing numbers from different files using a fortran program. The solution involves using an array to store the values and looping through the files to add the matching values.
  • #1
kouhei
1
0

Homework Statement


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?
 
Physics news on Phys.org
  • #2
Homework Equations N/AThe Attempt at a SolutionThis question can be solved by using an array to store the values from both files. The program should read in the values from the sample.dat file and store them in an array. Then it should loop through the result files one by one and for each result file, it should go through each value in the array and check if the value from the result file matches the value from the sample file. If it does, then the program should add the result file value to the array value. After all the result files have been processed, the program should print out the new array.
 

1. What is a fortran program?

A fortran program is a high-level programming language used primarily for scientific and numerical computing. It was developed in the 1950s for use in scientific and engineering applications.

2. How is fortran different from other programming languages?

Fortran is specifically designed for scientific and numerical computing, making it more efficient and easier to use for these types of applications. It also has built-in features for handling complex mathematical operations and large datasets, making it a preferred language for scientific research.

3. What are the benefits of using fortran for scientific computing?

There are several benefits to using fortran for scientific computing, including its efficiency, reliability, and ability to handle complex mathematical operations. It also has a large community of users and a long history of use in scientific research, making it a well-established and trusted language for these types of applications.

4. Is fortran still relevant in modern scientific computing?

Yes, fortran is still widely used in modern scientific computing due to its efficiency and specialized features for handling complex mathematical operations and large datasets. Many scientific research institutions and organizations continue to use fortran for their computational needs.

5. Are there any limitations to using fortran for scientific computing?

While fortran is well-suited for scientific computing, it may not be the best choice for other types of programming tasks. It is primarily focused on numerical computing and may not have the same capabilities or flexibility as other general-purpose programming languages. Additionally, its syntax and structure may be more rigid and less user-friendly compared to newer programming languages.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
7K
  • Programming and Computer Science
Replies
4
Views
615
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top