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!

is it a problem with my computer,compiler or what ? 2

Status
Not open for further replies.

zmth

Technical User
Aug 5, 2015
57
PH
I need to have a mixed type array with short integer*1 values first and a real value last. I read all the examples and am
doing everything correct but it refuses to work !! This is only a simple trial example to try first. Later i will need to have much more than just
11 integer values and need a way to assign values in program loops. Using as .f95

integer*1 id(11)

type mixd;integer*1 id(11);real val;end type
type(mixd) mixr(9)
do i=1,9;do j=1,11;id(j)=i*j ;mixr(i)=(id,real(i)/real(j));enddo;enddo
end

As one can see above that is perfectly valid code but it refuses to accept ! and does not seem to matter whether i put the first "integer*1 id(11)"
or not.

As an alternative i even try manually as eg:

mixr(3)=mixd(1,2,3,4,5,6,7,8,9,10,11,0.2)
or try
mixr(3)=mixd((1,2,3,4,5,6,7,8,9,10,11),0.2)

Both of which are perfectly valid and even neither of these work !

NOw the following does work

type mixd1;integer*1 id1 ;integer*1 id2;real val1;end type
type (mixd1) mixr1(9)
mixr1(3)=mixd1(2,3,5.5)
print*,mixr1(3)
end


BUt i don't want to have to repeat integer*1 id1,..., integer*1 id99 written out that many times - that is ridiculous to have to force one to do it that way!
And then it is most unwiedly to have to assign values this way also - no way to do it in a loop.
 
zmth said:
As one can see above that is perfectly valid code but it refuses to accept !
I couldn't see that it's "perfectly valid". If it would be valid, then it would be compiled without error messages. Bout it's not compiled because it's invalid.

To access the elements of the data structure you proposed, you have to do something like this
Code:
program zmth

type mixd
  integer*1 id(11)
  real val
end type

type(mixd) mixr(9)

do i=1,9
  do j=1,11
    mixr(i)%id(j)=i*j 
  enddo
  mixr(i)%val=real(i)/real(j)
enddo

do i=1,9
  write(*,'(A,i2,A)') '*****  mixr(',i,')  ****'
  do j=1,11
    write(*,'(A,i2,A,i2)') 'array id(',j,')=', mixr(i)%id(j)
  enddo
  write(*,'(A,f5.2)') 'val=', mixr(i)%val
enddo
end program

Output:
Code:
$ gfortran zmth.f95 -o zmth

$ zmth
*****  mixr( 1)  ****
array id( 1)= 1
array id( 2)= 2
array id( 3)= 3
array id( 4)= 4
array id( 5)= 5
array id( 6)= 6
array id( 7)= 7
array id( 8)= 8
array id( 9)= 9
array id(10)=10
array id(11)=11
val= 0.08
*****  mixr( 2)  ****
array id( 1)= 2
array id( 2)= 4
array id( 3)= 6
array id( 4)= 8
array id( 5)=10
array id( 6)=12
array id( 7)=14
array id( 8)=16
array id( 9)=18
array id(10)=20
array id(11)=22
val= 0.17
*****  mixr( 3)  ****
...
...

I suggest you to read something about derived data types in fortran.
 
it is not the computer, not the compiler, not the what...it is the who!

In as much as you claim you are 'doing everything correctly', you are actually doing most everything wrong...so much for that.

In you first piece of code, you are forgetting to prefix the constructor with the type "mixr(i) = (id,...)" is not perfectly valid code, it should read "mixr(i) = mixd(id,...). Also, I am not sure it makes any sense to create mixr(i) over and over j times inside the inner loop...it looks to me as if you should create the temporary id() array first and THEN, outside the inner loop, you create mixr(i); otherwise, you are over-writing it over and over and only the last value sticks.

In the other pieces of code, you seem to think that arrays can be passed as "(1, 2, 3,...)", in fact, you missing the slashes: "(/1, 2, 3,.../)".

Etcetera, etcetera.

Here is a working example along the lines of what you are trying to do:

Code:
program what

type mixed
    integer*1 :: id(11)
    real :: val
end type mixed

type(mixed) :: mix(9)
integer*1 :: id(11)
integer :: i, j

do i = 1, 9
    do j = 1, 11
        id(j)=i*j
    end do
    mix(i) = mixed( id, i*1.0)
end do

write(*,'(11i3,2x,f5.2)') (mix(i),i=1,9)

end program what

Here is another, eliminating the explicit inner loop and using an implied, instead:

Code:
program what

type mixed
    integer*1 :: id(11)
    real :: val
end type mixed
type(mixed) :: mix(9)
integer :: i, j

do i = 1, 9
    mix(i) = mixed( (/ (i*j,j=1,11) /), i*1.0 )
end do
write(*,'(11i3,2x,f5.2)') (mix(i),i=1,9)

end program what
 
This is the output from the sample program above:
[pre]
1 2 3 4 5 6 7 8 9 10 11 1.00
2 4 6 8 10 12 14 16 18 20 22 2.00
3 6 9 12 15 18 21 24 27 30 33 3.00
4 8 12 16 20 24 28 32 36 40 44 4.00
5 10 15 20 25 30 35 40 45 50 55 5.00
6 12 18 24 30 36 42 48 54 60 66 6.00
7 14 21 28 35 42 49 56 63 70 77 7.00
8 16 24 32 40 48 56 64 72 80 88 8.00
9 18 27 36 45 54 63 72 81 90 99 9.00
[/pre]
 
ok thanks. I did read about the derived types. One example did NOT have to use the odd % symbol as in %id... and another did but neither had anything about how to put array information in derived type. What is most strange though and i don't know where you read about this is your use of A as in '(A,i2,A)'. I never read anything about that special symbol ' A ". Seems to come out of nowhere as nowhere did i have anything declared with the symbol A. In another place i see you had f5.2 which seems to have something to do with formatting perhaps floating point? But the A symbol is another matter as far as i know it has nothing to do with formatting though it appears the way you use it that it does.

Another matter now i see in the post by Salgerman that there the '%' symbol does not have to be used in assigning data to the derived bype.
 
Hi zmth,

You can read about the selector operator (%) for example here:

i don't know where you read about this is your use of A as in '(A,i2,A)'. I never read anything about that special symbol ' A ".
These are the format specifier using for formatting output. For example A means alpha numeric, I2 means integer with 2 digits and F5.2 means real number with overall length of 5 and 2 decimal places. Google for fortran format ot look here:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top