Fortran Solving FORTRAN 90 2D Arrays Program Pr2

AI Thread Summary
The discussion revolves around issues with reading and writing data in Fortran programs, specifically when trying to populate a 2D array from a file. The initial code attempts to read a file containing 1680 values into a 21x80 array but results in unexpected output formatting. Suggestions include using implied do loops for both reading and writing data to ensure proper formatting. Despite attempts to implement these suggestions, the user continues to experience output in five columns instead of the desired format. The conversation also touches on the importance of using formatted WRITE statements to control output format, with references to external resources for formatting guidance. Additionally, there are mentions of a separate program that reads a different data file, but confusion arises regarding the absence of a write statement for displaying output. Overall, the thread highlights common challenges in file I/O operations in Fortran and the need for proper formatting techniques.
Milentije
Messages
47
Reaction score
0
program pr2
implicit none
integer:: i,j
real, dimension(21,80) :: istat
open(7,file='sens.dat',status='old',action='read')
read(7,*)((istat(i,j),i=1,21),j=1,80)
write(*,*)istat
end

Where is the problem?Input file has 1680 values.But I get this:

1.3598419E-08 4.9547498E-19 -2.3852840E-09 1.1903219E-04 4.6582014E-07
-8.6184544E-18 -9.3819693E-08 1.3887561E-02 -8.2534843E-06 -1.7656002E-16
-3.4295390E-08 8.9485317E-02 -1.4944331E-03 -4.6679033E-15 5.9263188E-05
0.4140495 -5.5463272E-03 3.7684741E-15 1.6822672E-04 -0.2041877

I want to get 21 times 80 table.
 
Technology news on Phys.org
I have made some changes but no use of it.
program pr2

implicit none
integer:: i,j
real, dimension(21,80) :: istat
open(7,file='sens.dat',status='old',action='read')
do i=1,21
read(7,*)(istat(i,j),j=1,80)
write(*,*)istat
end do
end
 
I would do the writes just like you did the reads, in implied do loops. That might make the difference.
Code:
read(7,*) (istat(i,j), i=1,21, j=1,80)
write(*,*) (istat (istat(i,j), i=1,21, j=1,80)

As an alternative, here's a variant of your second technique.
Code:
do i=1,21
  read(7,*) (istat(i,j), j=1,80)
  write(*,*) (istat(i,j), j=1,80)
end do
 
No,I have tried but still I get output in 5 columns.My professor told me the same as you but he is working on windows and I am on Linux.
 
Windows vs. Linux wouldn't make any difference, I don't believe. Are the values you're getting the right ones?

If you want to control the format of the output, you need to use a formatted WRITE statement. Your textbook should have some examples of how to do this. If not, here is a link showing how it can be done - http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html.
 
Thanks.But I still have problems with reading files,even with format.
program pr

c creat 2d array appres and phase
implicit none
integer::a,b
real,dimension(20,4)::model,data
do a=1,20
open(20,file='apandph.dat',status='unknown')
read(20,175)(model(a,b),b=1,4)
175 format(f10.6,2x,f11.8,2x,f10.6,2x,f11.8)
end do
end
3.645672 -0.47391191 3.634044 -0.53884584
3.645537 -0.47391942 3.643924 -0.53889292
3.632416 -0.47286296 3.723857 -0.58097768
9.571163 -0.33350858 6.509775 -0.65866327
127.110130 -0.97106200 205.496704 -0.77715313
107.565323 -0.98180515 191.546555 -0.99031007
54.473228 -0.72339439 53.588963 -1.01995885
35.943069 -0.46582329 8.643129 -0.84699863
85.260635 -0.80550200 590.542480 -0.60874861
79.657425 -0.80408365 80.931747 -0.69449586
56.826580 -0.70932525 9.529596 -0.87045997
53.008141 -0.81282926 10.899014 -1.45818961
11.361421 -0.39589506 3.772177 -1.23566055
72.485718 -1.03836429 204.431763 -1.09789932
100.328796 -1.20606327 154.542145 -1.13996935
32.644672 -1.13054383 37.369968 -1.19683146
43.448734 -1.23686874 55.834637 -1.16218436
108.154144 -0.69512361 103.532623 -0.66260660
108.352539 -0.64010102 110.725822 -0.63119751
108.218521 -0.63990360 110.622787 -0.63112277
 
What problem are you having? How did you get the output you showed? There is no write statement.
 

Similar threads

Replies
3
Views
4K
Replies
11
Views
3K
Replies
13
Views
4K
Replies
10
Views
25K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
19
Views
3K
Back
Top