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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with arrays 1

Status
Not open for further replies.

manoslev

Technical User
Dec 7, 2004
12
GR
I want to know if it is possible to assign 2 values in one array element.
For example I want the element a(1,1) to store the values
123 and 456.
I would appreciate if anyone could help me!
 
Try using COMPLEX numbers. You can assign one value to the real part and the other to the imaginary part.
 
Thanks for your answer but the main problem is when I am trying to run fortran in UNIX. You see UNIX doesn't seems to recognize double precision numbers and the 2 values I want to assign have 5 digits each.
So I can't have a number with 10 digits !
That's why I ask if I can assign two values separately to one element.
 
Are you using F77 or F9x?
On F9x, you can create structures (types) and assign accordingly. On F77, structures do not exist but, as mentioned before, you can use complex numbers eg
Code:
COMPLEX a(10,10)
a(1,1) = CMPLX(123,456)
print *,'left  part=',REAL(a(1,1))
print *,'right part=',AIMAG(a(1,1))
On some implementations, REAL is called AREAL.
 
I am using F90.
I will try out your solution and I will let you know.
Thanks!
 
If you are using F90, you could try this. It basically declares a structure (type) called Pair and one method to set the values and two methods to retrieve the values.
Code:
module ModPair
   implicit none
   ! Pair of values
   type::Pair
      integer::mVal1
      integer::mVal2
   end type Pair

   ! Limits of array
   integer, parameter:: &
      ROWMAX = 100, &
      COLMAX = 100

   ! Declare an array of Pair
   type (Pair), dimension(ROWMAX,COLMAX)::a
      
contains

   ! Set values
   subroutine PairSet (r, c, v1, v2)
      integer, intent(in):: r, c, v1, v2
      a(r,c)%mVal1 = v1
      a(r,c)%mVal2 = v2
      return
   end subroutine PairSet

   ! Get Value
   function PairGetV1 (r,c)
      integer::PairGetV1
      integer, intent(in):: r, c
      PairGetV1 = a(r,c)%mVal1
      return
   end function PairGetV1

   ! Get Value
   function PairGetV2 (r,c)
      integer::PairGetV2
      integer, intent(in)::r, c
      PairGetV2 = a(r,c)%mVal2
      return
   end function PairGetV2

end module ModPair

program PrgPair
   use ModPair
   implicit none

   ! Set some values
   call PairSet (1, 1, 123, 456)
   call PairSet (1, 2, 789, 124)

   ! Retrieve the values
   write(*,*) PairGetV1(1,1)
   write(*,*) PairGetV2(1,2)

   stop
end program PrgPair
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top