Solving FORTRAN 90 2D Arrays Program Pr2

In summary: See if formatting will help. Thanks.In summary, the problem is that the program is trying to read a file that has 1680 values but is only getting results in 5 columns.
  • #1
Milentije
48
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
  • #2
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
 
  • #3
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
 
  • #4
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.
 
  • #5
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.
 
  • #6
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
 
  • #7
What problem are you having? How did you get the output you showed? There is no write statement.
 

1. How do I create a 2D array in FORTRAN 90?

To create a 2D array in FORTRAN 90, you can use the DIMENSION statement followed by the name of the array and the desired dimensions. For example, to create a 2D array named "my_array" with dimensions 10x10, you would use the statement: DIMENSION my_array(10, 10)

2. How do I initialize a 2D array in FORTRAN 90?

You can initialize a 2D array in FORTRAN 90 by using the DATA statement after the DIMENSION statement. The DATA statement allows you to assign values to each element in the array. For example, if you want to initialize all elements of "my_array" to 0, you would use the statement: DATA my_array /10*10*0/

3. How do I access and modify elements in a 2D array in FORTRAN 90?

To access and modify elements in a 2D array in FORTRAN 90, you can use the index notation. For example, to access the element in the third row and fourth column of "my_array", you would use the notation my_array(3,4). You can also use this notation to assign values to specific elements in the array.

4. How do I perform operations on 2D arrays in FORTRAN 90?

In FORTRAN 90, you can use built-in functions and loops to perform operations on 2D arrays. Some common functions used for 2D arrays include SUM, MAXVAL, MINVAL, and MATMUL. You can also use DO loops to iterate through the elements of the array and perform operations on them.

5. How do I deallocate a 2D array in FORTRAN 90?

To deallocate a 2D array in FORTRAN 90, you can use the DEALLOCATE statement followed by the name of the array. This will free up the memory used by the array. It is important to deallocate arrays after they are no longer needed to avoid memory leaks.

Similar threads

  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
464
  • Programming and Computer Science
Replies
11
Views
2K
Replies
58
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
10
Views
25K
  • Programming and Computer Science
Replies
5
Views
12K
Back
Top