Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assumed shape array 1

Status
Not open for further replies.

xrayspex1

Technical User
Jun 10, 2008
9
SI
hi all, i'm trying to write a very simple program, which prompts me for matrix dimensions (m,n) and then outputs a m x n matrix of ones (1's) on the display. the thing obviously :) doesn't work - if you can help me, i'd be very grateful!

my CODE:__________________________________________________

subroutine matrix(row,column,array)
implicit none

integer, intent(in) :: row,column
integer, dimension(row,column), intent(out) :: array
integer :: i,j

do i = 1,row
do j = 1,column
array(i,j) = 1
end do
end do
end subroutine matrix

!***

program mxn
implicit none

integer :: m,n
integer, dimension(m,n) :: matr


print *,"m,n:"
read *,m,n

call matrix(m,n,matr)

print *, matr

end program mxn



MY ERROR msg's:_______________________________________

Compiling Fortran...
C:\Documents and Settings\mlicer\My Documents\fortran\mxn_matrix.f90
C:\Documents and Settings\mlicer\My Documents\fortran\mxn_matrix.f90(23) : Error: A specification expression object must be a dummy argument, a COMMON block object, or an object accessible through host or use association [M]
integer :: m,n
-----------^
C:\Documents and Settings\mlicer\My Documents\fortran\mxn_matrix.f90(23) : Error: A specification expression object must be a dummy argument, a COMMON block object, or an object accessible through host or use association [N]
integer :: m,n
-------------^
C:\Documents and Settings\mlicer\My Documents\fortran\mxn_matrix.f90(24) : Error: An automatic object is invalid in a main program. [MATR]
integer, dimension(m,n) :: matr
___________________________________________________

does anyone know what i'm doing wrong? i can't figure it out...thanks a lot!
 
In your main program you need to declare maximal dimension for you matrix MATR as constant. Then in your subroutines you use only the submatrix of MATR. For the printing of the matrix, you can create a subroutine.
Example:
Code:
subroutine compute_matrix(row,column,array)
implicit none
integer :: i, j, row, column, array(row, column)

do i = 1,row
    do j = 1,column
        array(i,j) = 1
    end do
end do
end subroutine compute_matrix

subroutine print_matrix(row,column,array)
implicit none
integer :: row,column, i,j, array(row, column)

do i = 1,row
   print *, (array(i,j), j=1,column)
end do
end subroutine print_matrix

!***
program mxn
implicit none

integer :: m,n, matr(10,10)

write (*,*) 'Enter m,n:'
read  (*,*) m, n
write (*, *) 'Dimensions are: m = ', m, ', n = ', n

call compute_matrix(m,n,matr)
call print_matrix(m,n,matr)

!print *, matr
end program mxn

After compiling (I have g77 compiler with MSYS under windows) with
Code:
g77 matrix2.for -o matrix2 -ffree-form
the output is
Code:
$ matrix2
5 3
 Enter m,n:
 Dimensions are: m =  5, n =  3
 1 1 1
 1 1 1
 1 1 1
 1 1 1
 1 1 1
(I don't know why the output 'Enter m, n': cames first after reading of m and n)
 
mikrom, thanks a lot! i've tried it and it works nicely.
 
You may use allocatable array.
Code:
integer, allocatable:: array(:,:)
integer:: m, n, i, j
m = 4 ! or read m (and n) from console...
n = 4
allocate(array(m,n)) ! no comments
array = 1            ! set all elements at once
do i = 1, m
   print *, (array(i,j), j=1,n)
enddo
deallocate(array)    ! free memory
 
Which Fortran Standard (F90, F95) and which compiler are you using?
I only have g77 compiler, which supports Fortran 77 and some new features from F90 (or maybe F95).
 
hi mikrom, i'm using f90. (in Compaq Visual Fortran 200) i pressume the compiler must be Intel Visual Fortran Compiler. i don't know about the details - i just use the developer studio default compiler, sorry i can't be of more help :)
 
If you're using allocatable stuff, check if you have a F95 compiler. It makes programming a lot easier. F90 had some strange rules that didn't allow allocatable arrays to be created in one routine and used in another. All allocatables in F90 had to be created at the top level (i.e. in the program segment).
 
thanks a lot - i didn't know that and it sounds like a really useful advice for the future.
 
oh, and thanks ArkM! allocatables are what i needed..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top