views:

1080

answers:

2

I have questions about Arrays and Derived Types. For my new project, I have to use an array instead of a scratch file to store information from users. To do this, I need to create derived types, too.

However, I haven't understood what an array is and what a derived type is, how to use them, what they can do, and some other basic ideas. Can anyone give me some information about array and derived types?

I wrote code for them, but I don't know it is written correctly. If anyone can check this for me, I would appreciate it.

Here are my array and derived types:

! derived type
TYPE Bank
  INTEGER :: acNumber, acChecks
  REAL :: acBlance, acRate
  CHARACTER :: acType*1, acLName*15, acFName*15
END TYPE

! array
INTEGER, PARAMETER :: MaxRow, MaxColum = 7
INTEGER, DIMENSION(MaxRow:MaxColum) :: AccountData

Please forgive me my English, this is my second language. Thank you.

Uka

+1  A: 

An array is an ordered list of variables, all of the same type, indexed by integers. See Array in Wikipedia Note that in Fortran array indexing is more flexible than most other low level languages, in that instead of a single index per dimension, you can have an index triplet consisting of lower bound, upper bound, and stride. In that case the lvalue of the expression is a subarray rather than a single element of the array type.

A derived type is a composite type defined by the users, which is made up of multiple components which can be of different types. In some other languages these are knows as structs, structure types, or record types. See Record in Wikipedia

You can also make an array of a derived type, or you can have a derived type where one or more components are themselves arrays, or for that matter, other derived types. It's up to you!

The easiest way to check your code is to try to compile it. Making it past the compiler is of course no guarantee that the program works as expected, but it certainly is a required step.

janneb
Thank you for your comment and also links!
Uka
+2  A: 

If you are a fortran programmer you have probably seen a subroutine accepting 10/15 arguments. If you think about it, it's insane (they are too many, you run the risk of swapping them) and you quickly realize that some arguments always travel together. It would make sense to pack them under a single entity that carries everything around as a whole, non as independent entities. This would reduce the number of arguments considerably, giving you only the burden to find proper association. This single entity is the type.

In your code, you say that a Bank is an aggregate of those informations. You can now declare a concrete variable of that type, which will represent and provide access to the single variables acNumber, acChecks and so on. In order to do so, you have to use the % symbol. so if your bank variable is called b, you can say for example

b%acNumber = 5

You can imagine b as a closet, containing different shelves. You move the closed, all the shelves and their content move together.

An array is a group of entities of the same type (say, integer, or Character(len=1024), or Bank) and they are one after another so you can access each of them with a numeric index. Remember that, unless specified differently, arrays indexes in fortran start at 1 (in all the other major languages, the first index is zero instead)

As for your code, I suggest you to:

  • write

     INTEGER, DIMENSION(MaxRow:MaxColum) :: AccountData
    

    as

     INTEGER :: AccountData(MaxRow,MaxColum)
    

    it is the same, but you write less. Please also note that there is a difference between using the : and the ,. If you want to define a matrix (your case), which is a two-dimension array, you have to use the comma. What you wrote is wrong.

  • for the strings, it's better if you write

     CHARACTER :: acType*1, acLName*15, acFName*15
    

    as

     CHARACTER(LEN=1) :: acType
     CHARACTER(LEN=15) :: acLName
     CHARACTER(LEN=15) :: acFName
    

    in this case, you write more, but your syntax is deprecated (I could be wrong, though) Also, remember that it's better if you write one member variable per line in the types. It's a matter of taste, but I prefer to see the full size of a type by having one line per member variable.

  • For MaxRows and MaxColumns, I would write them as MAX_ROWS and MAX_COLUMNS. Parameters and stuff that is highly constant by tradition is identified with an all capital, underscore separated name in any major language.


Edit: to answer your comment, here is an example of the use of an array

$ more foo.f90 
program test
    integer :: myarray(10)

    myarray = 0   ! equivalent to zeroing the single elements one by one
    myarray(2) = 5
    myarray(7) = 10

    print *, myarray

end program
$ g95 foo.f90 -o foo
$ ./foo
 0 5 0 0 0 0 10 0 0 0

an array is just like multiple variables with the same name, identified by an index. Very useful to express vectors, or matrices. You can of course do an array of an aggregated type you define, instead of a predefined type (eg. integer).

Stefano Borini
Thank you for your comment. I understood what array and derived type are, but I'm still confused about writing in array (store information in array). Is it like scratch file? Do I need to use WRITE statement to do this?
Uka
no. It's like multiple copies of the same variable type. you write in it by assignment. I'll add something more detailed to my answer later. Now I have to rush :)
Stefano Borini
Note that I corrected your writing MaxRow, MaxColumn. As you wrote, you are not defining a two-dimension array. That syntax means something else.
Stefano Borini