1 / 15

Structures or Derived Types

Structures or Derived Types. Real Life Objects. Often structured Students , Employees , Cars , Accounts , Cricket matches , flats etc contain heterogeneous sub-objects Students Name ( string ), Age ( integer in [15, 65], ) Height ( real ), CPI ( real number in [1,10] ) Car

britain
Télécharger la présentation

Structures or Derived Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structures or Derived Types

  2. Real Life Objects • Often structured • Students, Employees, Cars, Accounts, Cricket matches, flats etc • contain heterogeneous sub-objects • Students • Name (string), • Age (integer in [15, 65],) • Height (real), • CPI (real number in [1,10]) • Car • Weight (Real), Volume (Real), • Make (String), Seating capacity(Integer) , • Year of make (integer)

  3. Arrays • Two Structured Data Types • Arrays and Character strings • Both are homogeneous: • components are of identical type • Accessing a component through indices • not very natural • More natural data types desirable

  4. Derived Data Types • Fortran's solution to representing general structured data • Heterogeneous collection of data values • Components identified directly by names rather than indices • Generalized random access • Derived data type objects called structures (in C ) and Records (Pascal)

  5. Example Type :: Cricketer Character(len=25) :: Name Integer :: Age Real :: Height Character(len=10) :: Country logical :: in_action Real :: ave_score logical :: bowler end type Cricketer

  6. Structure variables • Variables declared to be of a derived type • Similar syntax used, eg. • Type (Cricketer)::indian_top_scorer_01, england_captain_99, aussie_keeper_74 • Type (Cricketer),Dimension(15)::Indian_Team_02 • The latter is an array of structures or records

  7. Initialization Indian_top_scorer_01 = Cricketer("Tendulkar", 29, 5.2, "India", .true. ... ) England_captain_02 = Cricketer("Hussain", 24, 5.6, "England", .true. , ... ) • Structure Constructor operation(inspired by C++)

  8. Accessing the Components • The components of a structure (a variable of derived type) can be accessed by the names top_scorer%height captain%ave_score Indian_Team_02(i)%ave_score • Each of these is like a variable of appropriate type • can appear wherever such variables can occur • top_scorer%height = 5. • if (captain%ave_score > 25) then • top_scorer%ave_score = … • Int(captain%ave_score) + 26 • Indian_Team_02(i)%ave_score < Eng_Team_02(i)%ave_score

  9. Structure inside structure • Components can be intrinsic types or even other derived types Type:: team_pair character(len=10):: team1 character(len=10):: team2end typeteam_pairType:: matchtype(team_pair):: teamsInteger:: dateCharacter(len=10):: groundCharacter(len=10):: countryend type matchType:: cricketer ...type(match):: top_score_match ...end type cricketer

  10. Component Selection • Any component can be selected using series of component selectors Eg. eng_captain%topscore_match%ground = “Lords” • Recursive Structures • One of the component can be of the parent type itself! • More on this later

  11. Program structure Type:: cricketerCharacter(len=15):: name,countryreal:: averageinteger:: top_scoreend type type(cricketer), dimension(15):: natwest_series_teamcharacter(len=15):: playerreal:: averageinteger:: iread *, natwest_series_teamread *, playerdo i = 1, 15 if (trim(natwest_series_team(i)%name) == player) then average = natwest_series_team(i)%averageprint *, averageexitendifend do

  12. Complex Data Types • Complex data types can be defined as structured data typestype :: Compxreal:: re_partreal:: im_partend type Compx type(Compx):: xyz • But direct support is available in Fortran 90 • Complex data type is an intrinsic type Complex:: a1 = (3.1410, - 2.3456) • declares a1 to be of type complex with given real and imaginary parts

  13. Accessing components • Complex numbers can be read or written component-wisecomplex:: a1 read*, a1 • requires input to be (2.345, 6.7890) • Print command also outputs in the same way • To assign a variable, use a1 = complex(a,b) • a1 will get the value (a,b) • Kind parameter is an optional parameter

  14. Intrinsic Functions • Real(),Int() - throws the imaginary part and converts the real part to real or integer data types • Aimag() - converts the imaginary part to a real number • Cabs(c) - absolute value of c = sqrt(a^2 + b^2)

  15. Example program quadraticimplicit nonereal :: a, b, c, disc, x_rcomplex:: x_c1,x_c2 = (0.0, 0.0)real, parameter :: eps = 1.0e-6read *, a, b, c if (a == 0.0) then! a is 0, not a quadratic equationprint *, "equation is not quadratic"elsedisc = b*b - 4.0*a*c x_r = -b/(2.0*a) if ( abs(disc) < eps ) then! discriminant is nearly zeroprint *, "double real root", x_r else x_c1 = (x_r + Sqrt( Cmplx(disc,0.0))/(2.0*a)) x_c2 = (x_r - Sqrt( Cmplx(disc,0.0))/(2.0*a))print *, Real(x_c1),"+ i", Aimag(x_c1) print *, Real(x_c2),"- i", Aimag(x_c2)endifendifend program quadratic

More Related