Solving FORTRAN 90 2D Arrays Program Pr2

  • Context: Fortran 
  • Thread starter Thread starter Milentije
  • Start date Start date
  • Tags Tags
    2d Arrays Fortran
Click For Summary

Discussion Overview

The discussion revolves around issues related to reading and writing 2D arrays in FORTRAN 90, specifically in the context of handling input files and formatting output. Participants are attempting to troubleshoot a program that is expected to read a 2D array from a file and display it correctly, but are encountering formatting and output issues.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares a FORTRAN program that reads a 2D array from a file but notes that the output does not display as expected, despite having the correct number of input values.
  • Another participant suggests modifying the write statements to match the read statements using implied do loops, proposing that this might resolve the output formatting issue.
  • A different participant questions whether the operating system (Windows vs. Linux) affects the output formatting, asserting that it should not make a difference.
  • One participant mentions ongoing issues with reading files and formatting output, providing a new program example that attempts to read a different 2D array with specific formatting but does not clarify the nature of the problems encountered.
  • Another participant inquires about the specific problems being faced and notes the absence of a write statement in the provided output example.

Areas of Agreement / Disagreement

Participants express differing opinions on the effectiveness of various approaches to reading and writing data in FORTRAN. There is no consensus on the best method to achieve the desired output formatting, and multiple competing views remain regarding the handling of 2D arrays and file input/output.

Contextual Notes

Some participants have noted issues related to output formatting and the control of write statements, but the discussion does not resolve these technical challenges or clarify the assumptions underlying the proposed solutions.

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 8 ·
Replies
8
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 10 ·
Replies
10
Views
26K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 19 ·
Replies
19
Views
4K