Here is my simple program for 2 by 2 matrix
program matrix
INTEGER :: A(2,2)
integer::i,j
do i = 1,2
write(*,*) A(i,1),A(i,2)
end do
end program
and when I run and compile I get the output of
15866218 1869135244
11 -2
I want to know why this four no. came and how...I was expecting output like
A(1,2) A(1,2)
A(2,1) A(2,2)
But it has value instead. Any help?
You never initialized the values in your matrix. You have to tell the compiler to put some kind of value into the matrix, or you just get whatever number was in those memory addresses before your program ran.
This question already has answers here:
How to initialize two-dimensional arrays in Fortran
(3 answers)
Closed 7 years ago.
This is my code:
program test
integer, dimension(3,3) :: a =(/1,2,3,4,5,6,7,8,9/)
do i=1,3
write(*,*) (a(i,j),j=1,3)
enddo
end program
I get the following error:
Incompatible ranks 2 and 1 in assignment at (1)
Is the initialization method wrong?
Currently you are trying to assign a 1D array (of length 9) to a 2D (3x3) array. You need to reshape the array before the assignment:
program test
integer, dimension(3,3) :: a = reshape( (/1,2,3,4,5,6,7,8,9/), (/ 3,3/))
do i=1,3
write(*,*) (a(i,j),j=1,3)
enddo
end program
I am writing a program in Fortran to find the velocity of a parachuting person in relation to time. I keep getting an error that I can't fix. I am very new to programming and any help is greatly appreciated.
The error is
v(i+1)=v(i)+[32-((c*v(i)*v(i))/m)]*(h)
1
Error: Incompatible ranks 0 and 1 in assignment at (1)
and my program is
PROGRAM para
INTEGER :: i
REAL :: v(11) !velocity
REAL :: q !initial velocity
REAL :: h !time step
REAL :: c !drag coefficient
REAL :: m !mass
! gravity is equal to 32 ft/s^2
WRITE (*,*)'enter time step'
READ(*,*)h
Write(*,*)'enter initial velocity'
READ(*,*)q
WRITE(*,*)'enter drag coefficient'
READ(*,*)c
WRITE(*,*)'enter mass'
READ(*,*)m
DO i=1,10 ! from 1 to 10, with 1 being the interval.
END DO
v(i+1)=v(i)+[32-((c*v(i)*v(i))/m)]*(h)
q=v(1)
END PROGRAM
You cannot use [] as a normal parenthesis in expressions. It is an array constructor, [ items ] means an array with items as elements. Also the end do should be after this line.
Well, you span up a vector using [ ... ] (this is equivalent to (/ ... /)). So, using regular brackets instead of the square ones solves your problem.
BTW: I'm a little confused about your loop... Shouldn't the v(i+1) = ... line be inside the loop?
This is my program to solve a problem of three dimensional cylindrical fin. But when i run this program in Fortran 6.2 this error shows up:
Error: The shapes of the array expressions do not conform. [T]"
Now i don't understand why this is happening. I need quick assistance. Anyone please help me.
PROGRAM CYLINDRICAL FIN
DIMENSION T(500,500,500), OLDT(500,500,500),ERR(500,500,500)
DIMENSION R(500),Y(500),Z(500)
REAL CC,H,DR,DY,DZ,A,D,RY,YZ,ZR,B,E,TA,RL,YL,ZL
+P,AC,QF,MF,Q,EF,EFF,QMAX,AS,LC,QT,QF1,QF2
INTEGER I,J,K,M,N,L,M1,N1,L1,M2,M4,M34,N2,N4,N34,L2,L4,L34
RL=0.5
YL=6.283
ZL=0.04
M=100
N=40
L=20
M2=((M/2)+1)
M4=((M/4)+1)
M34=((3*M/4)+1)
N2=((N/2)+1)
N4=((N/4)+1)
N34=((3*N/4)+1)
L2=((L/2)+1)
L4=((L/4)+1)
L34=((3*L/4)+1)
DR=RL/M
DY=YL/N
DZ=ZL/L
CC=400.0
H=10.0
TA=25
M1=M-1
N1=N-1
L1=L-1
************VARIABLES************
A=DR*DY*DZ
D=DR+DY+DZ
RY=DR*DY
YZ=DY*DZ
ZR=DZ*DR
E=RY+YZ+ZR
************VARIABLES FOR EFFICIENCY AND EFFECTIVENESS (CROSS-SECTION AREA,PERIMETER,M,SURFACE AREA OF FIN)************
AC=3.1416*DR**2
P=2*(3.1416*DR+DZ)
MF=((H*P)/(CC*AC))**(0.5)
AS=2*3.1416*DR*DZ+3.1416*DR**2
************************************** distance discritization ******************
R(1)=0.0
Y(1)=0.0
Z(1)=0.0
R(M+1)=RL
Y(N+1)=YL
Z(L+1)=ZL
DO I=2,M
R(I)=R(I-1)+DR
END DO
DO J=2,N
Y(J)=Y(J-1)+DY
END DO
DO K=2,L
Z(K)=Z(K-1)+DZ
END DO
DO I=1,M
DO J=1,N
DO K=1,L
T(I,J,K)=0.0
END DO
END DO
END DO
DO I=1,M
DO J=1,N
T(I,J,1)=400
END DO
END DO
ITER=0.0
READ(*,*) LAST
31 CONTINUE
ITER=ITER+1
*************************************** FORMULAS**********************************
DO I=2,M1
DO J=2,N1
DO K=2,L1
T(I,J,K)=((R*DR*T(I+1,J,K)*(YZ)**2.0)-((T(I+1,J,K)+T(I-1,J,K))*
+(R*YZ)**2.0)-((T(I,J+1,K)+T(I,J-1,K))*(ZR**2.0))-((T(I,J,K+1)+
+T(I,J,K-1))*(R*RY)**2.0))/((R*DR*(YZ)**2.0)-(2.0*(R*YZ)**2.0)-
+(2.0*(ZR)**2.0)-(2.0*(R*RY)**2.0))
END DO
END DO
END DO
*************************************** END OF ITERATIONG FORMULAS****************
DO I=1,M
DO J=1,N
DO K=1,L
OLDT(I,J,K)=T(I,J,K)
END DO
END DO
END DO
DO I=1,M
DO J=1,N
DO K=1,L
ERR(I,J,K)=T(I,J,K)-OLDT(I,J,K)
END DO
END DO
END DO
EMAX=0.0
EMAX=MAX(EMAX,ERR(I,J,K))
WRITE(*,*) ITER, EMAX
IF (ITER.LT.LAST) GOTO 31
WRITE(*,*) DR,A,B,E
END PROGRAM CYLINDRICAL FIN
In your long formula you have R without index. But R is a vector so that's why you get the error.
DO I=1,M
DO J=1,N
T(I,J,1)=400
END DO
END DO
can be written more swiftly as
T(1:M,1:N,1) = 400
also 400 is an integer so you better use 400.
I quickly converted the program to free-format and compiled it with gfortran. The problem line is:
T(I,J,K)=((R*DR*T(I+1,J,K)*(YZ)**2.0)-((T(I+1,J,K)+T(I-1,J,K))* &
(R*YZ)**2.0)-((T(I,J+1,K)+T(I,J-1,K))*(ZR**2.0))-((T(I,J,K+1)+ &
T(I,J,K-1))*(R*RY)**2.0))/((R*DR*(YZ)**2.0)-(2.0*(R*YZ)**2.0)- &
(2.0*(ZR)**2.0)-(2.0*(R*RY)**2.0))
The gfortran error message is:
"Error: Incompatible ranks 0 and 1 in assignment at (1)"
with the (1) designation the "T" on the LHS. Obviously the T(I,J,K) is a scaler of rank 0. Somewhere on the RHS you have an array of rank 1 -- identified by Azrael3000 as "R".
P.S. I recommend using "implicit none" and typing all of your variables. Implicit typing is pernicious, e.g., it allows typos in variable names to be accepted by the compiler.
Your problem comes from the array operation you are trying to perform.
If you simplify the line:
T(I,J,K)=((R*DR*T(I+1,J,K)*(YZ)**2.0)-((T(I+1,J,K)+T(I-1,J,K))*
+(R*YZ)**2.0)-((T(I,J+1,K)+T(I,J-1,K))*(ZR**2.0))-((T(I,J,K+1)+
+T(I,J,K-1))*(R*RY)**2.0))/((R*DR*(YZ)**2.0)-(2.0*(R*YZ)**2.0)-
+(2.0*(ZR)**2.0)-(2.0*(R*RY)**2.0))
You can end up with this other line that also shows the same error:
T(I,J,K)= T(I,J,K-1)*R
The shapes of the array expressions do not conform. [T]
So you can see that your problem comes from trying to assign an array R to a scalar T(I,J,K).
I've just stumbled upon the fact that compiler lets me use integer arrays as indices to other arrays. For example:
implicit none
real*8 :: a(3), b(2)
integer :: idx(2)
a=1.d0
idx=(/1,2/)
b = a(idx)
print*,shape(b)
print*,b
print*
end
Given the fact that this seems to work with both gfortan and a PGI compiler, I'm wondering if this a language feature rather than something compiler just lets me out with. I would appreciate if somebody more knowledgeable than me can comment if this is really a language feature.
And if it is, than I'd appreciate if somebody would spell out the exact language rules of how such constructions are interpreted in multidimensional case, like here:
implicit none
real*8 :: aa(3,3), bb(2,2)
integer :: idx(2)
do i=1,3 ; do j=1,3
aa(i,j) = 1.d0*(i+j)
enddo; enddo
bb=aa(idx,idx)
print*,shape(bb)
print*,bb
end
Yes, it is.
The final draft of the Fortran 2008 standard, ISO/IEC JTC 1/SC 22/WG 5/N1830, ftp://ftp.nag.co.uk/sc22wg5/N1801-N1850/N1830.pdf says on page 84
4.8 Construction of array values
...
6 If an ac-value is an array expression, the values of the elements of the expression, in array element order (6.5.3.2), specify the corresponding sequence of elements of the array constructor.
Example
real, dimension(20) :: b
...
k = (/3, 1, 4/)
b(k) = 0.0 ! section b(k) is a rank-one array with shape (3) and
! size 3. (0.0 is assigned to b(1), b(3), and b(4).)
The rules you can see directly from your code
implicit none
real*8 :: aa(3,3), bb(2,2)
integer :: idx(2),i,j,k
idx=(/3, 2/)
k=0
do i=1,3 ; do j=1,3
k=k+1
aa(i,j) = aa(i,j)+1.d0*k
enddo; enddo
write(*,*),shape(aa)
write(*,'(3G24.6,2X)') aa
bb=aa(idx,idx)
print*,shape(bb)
write(*,'(2G24.6,2X)'),bb
end
Output:
3 3
1.00000 4.00000 7.00000
2.00000 5.00000 8.00000
3.00000 6.00000 9.00000
2 2
9.00000 6.00000
8.00000 5.00000