Structures or Derived Types. Real Life Objects Often structured –Students, Employees, Cars, Accounts, Cricket matches, flats etc contain heterogeneous.

Slides:



Advertisements
Similar presentations
Finding Complex Roots of Quadratics
Advertisements

You will learn about: Complex Numbers Operations with complex numbers Complex conjugates and division Complex solutions of quadratic equations Why: The.
Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Chapter 11 Additional Intrinsic Data Types Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software.
Lesson 1-6 Solving Quadratic Equations. Objective:
Complex Number A number consisting of a real and imaginary part. Usually written in the following form (where a and b are real numbers): Example: Solve.
4.8 Quadratic Formula and Discriminant
Structured Data Types. Date Types We have seen various data types –Integer, Real, Character, Logical All these types define data values of different kinds.
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
ReAl :: x OK CHARACTER :: name OK, a 1 character name! CHARACTER(LEN=10) :: name OK, string length 10 REAL :: var-1 cannot have -1 in a declaration var_1.
Chapter 1.4 Quadratic Equations.
Solving Quadratic Equations Section 1.3
Bell Ringer: Find the zeros of each function.
Introduction to FORTRAN-90 University of Liverpool course.
As you come in collect your Warm-Ups to be turned in
Introduction to FORTRAN
Fortran: Program Units and Procedures Session Four ICoCSIS.
Multi-Dimensional Arrays
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3.
Conditional Control Flow Constructs. Sequential Control Flow Execution order follows the textual order straight line flow Many simple problems can not.
CSC Programming I Lecture 5 August 30, 2002.
Week 8 Improving on building blocks Recursive procedures.
Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0.
5.6 Quadratic Equations and Complex Numbers
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
Some Advanced Features of Procedures. Recursion Recursive Calls –A procedure can call itself (Self Recursion) –A can call B, B calls C, etc, Z calls A.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
Pre-Calculus Section 1.5 Equations Objectives: To solve quadratics by factoring, completing the square, and using the quadratic formula. To use the discriminant.
3.8 Warm Up Write the function in vertex form (by completing the square) and identify the vertex. a. y = x² + 14x + 11 b. y = 2x² + 4x – 5 c. y = x² -
5.8 Quadratic Formula. For quadratic equations written in standard form, the roots can be found using the following formula: This is called the Quadratic.
5.9.1 – The Quadratic Formula and Discriminant. Recall, we have used the quadratic formula previously Gives the location of the roots (x-intercepts) of.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
Programming, an introduction to Pascal
Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.
The Quadratic Formula & Discriminant Essential question – How do you solve a quadratic equation using the Quadratic Formula?
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Functional Programming Lecture 5 - Tuples. Packaging several values together Sometimes you need to package up several values into a single object –A function.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
4.2 Quadratic Functions Objective: Solve quadratic equations. Use the discriminant to describe the roots of a quadratic equation.
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
Quadratic Equations. PROGRAM QuadraticEquations_1 IMPLICIT NONE REAL A, B, C, Discriminant, Root_1, Root_2 ! Get the coefficients PRINT *, "Enter the.
Fortran MATTHEW CARSON. History of FORTRAN FORTRAN ; First High Level Language FORTRAN I 1957; First Compiled Language FORTRAN II ; Independent.
Introduction to Programming Lecture 4. Key Words of C main main if if else else while while do do for for.
Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.
MM2A4. Students will solve quadratic equations and inequalities in one variable. b. Find real and complex solutions of equations by factoring, taking square.
4.2 – Quadratic Equations. “I can use the discriminant to describe the roots of quadratic equations.” DISCRIMINANT: b 2 – 4ac b 2 – 4ac > 0 2 distinct.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Warm Up  1.) Write 15x 2 + 6x = 14x in standard form. (ax 2 + bx + c = 0)  2.) Evaluate b 2 – 4ac when a = 3, b = -6, and c = 5.
Math 20-1 Chapter 4 Quadratic Equations
The Quadratic Formula & Discriminant
Fortran Tutorial Fortran spaces for code qqqqqqCODE
The Discriminant Given a quadratic equation, can youuse the
INC 161 , CPE 100 Computer Programming
Complex integers? Here a and b are integers.
Math 20-1 Chapter 4 Quadratic Equations
Worksheet Key 9 11/14/2018 8:58 PM Quadratic Formula.
5.6 The Quadratic Formula and the Discriminant
4.8 The Quadratic Formula and the Discriminant
Unit 7 Day 4 the Quadratic Formula.
Review.
Quadratic Formula & the Discriminant
3.4 – The Quadratic Formula
Using the Quadratic Formula to Solve Quadratic Equations
  Warm Up:.
Presentation transcript:

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 Weight (Real), Volume (Real), Make (String), Seating capacity(Integer), Year of make (integer)

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

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)

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

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

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++)

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

Structure inside structure Components can be intrinsic types or even other derived types Type:: team_pair character(len=10):: team1 character(len=10):: team2 end type team_pair Type:: match type(team_pair):: teams Integer:: date Character(len=10):: ground Character(len=10):: country end type match Type:: cricketer... type(match):: top_score_match... end type cricketer

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

Program structure Type:: cricketer Character(len=15):: name,country real:: average integer:: top_score end type type(cricketer), dimension(15):: natwest_series_team character(len=15):: player real:: average integer:: i read *, natwest_series_team read *, player do i = 1, 15 if (trim(natwest_series_team(i)%name) == player) then average = natwest_series_team(i)%average print *, average exit endif end do

Complex Data Types Complex data types can be defined as structured data types type :: Compx real:: re_part real:: im_part end type Compx type(Compx):: xyz But direct support is available in Fortran 90 Complex data type is an intrinsic type Complex:: a1 = (3.1410, ) declares a1 to be of type complex with given real and imaginary parts

Accessing components Complex numbers can be read or written component-wise complex:: a1 read*, a1 requires input to be (2.345, ) 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

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)

Example program quadratic implicit none real :: a, b, c, disc, x_r complex:: x_c1,x_c2 = (0.0, 0.0) real, parameter :: eps = 1.0e-6 read *, a, b, c if (a == 0.0) then ! a is 0, not a quadratic equation print *, "equation is not quadratic" else disc = b*b - 4.0*a*c x_r = -b/(2.0*a) if ( abs(disc) < eps ) then ! discriminant is nearly zero print *, "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) endif endif end program quadratic