SlideShare a Scribd company logo
1 of 19
Basic Scientific Programming
Arrays
Introduction








So far we have covered the predefined
Fortran data types; Integer, Real, Character,
and Logical.
These data types are called simple types
because a data value of one of these types
consists of a single value.
It might be necessary for some application to
process a collection of values, e.g. a list of
scores.
To do that we use Arrays.
Ex:


Suppose we want to calculate the
average of the grades of 40 students in
a class, then display all grades greater
than the average. The grades are
stored in a file.


Program Avg
implicit none
integer:: openstatus, Inputstatus
real:: grade1, grade2,… , grade40, avg
.
.

Open(unit=10,file=“grade.avg”,status=“old” , &
IOSTAT= openstatus)

if (openstatus>0) stop “ **error**”


Read(10,*,IOSTATUS= inputstatus) grade1,&
grade2,……… , grade40
if (inputstatus>0) stop “**Input Error**”
if (inputstatus<0) stop “not enough data”
.
.
Avg= (grade1+grade2+…+grade40)/40




If (grade1>avg) print*, grade1
If (grade2>avg) print*, grade2
.
.
If (grade40>avg) print*, grade40
End Program Avg
Is this a practical approach??
What if we have 15432 students?


Another way is to rewind the file
Program Avg2
implicit none
integer:: openstatus, Inputstatus
integer:: No_of_grades, I
real:: grade, sum, avg
.
.
Open(unit=10,file=“grade.avg”,status=“old” , &
IOSTAT= openstatus)

if (openstatus>0) stop “ **error**”


No_of_grades = 0
Do
Read(10,*,IOSTATUS= inputstatus) grade
if (inputstatus>0) stop “**Input Error**”
if (inputstatus<0) stop “not enough data”
No_of_grades = No_of_grades + 1
End do


Rewind (unit=10)
sum = 0.0
Do I = 1, No_of_grades
Read(10,*) grade
sum= sum + grade
End do
Avg = sum/ No_of_grades


Rewind (unit=10)
Do I = 1, No_of_grades
Read(10,*) grade
if (grade>avg) print*, grade
End do
End program Avg2
this approach is slow because files are stored
in the hard drive which is slower than the
memory.
Arrays








We need a data structure to store and
organize the entire collection of grades.
This structure should be stored in the main
memory to reduce retrieval time.
Instructions to access this structure should be
simple.
Direct access to data  it should take the
same time to access grade40 as it takes to
access grade1
Arrays Declaration


Real, Dimension(40):: grades
instructs the compiler to establish an
array with the name grades consisting
of 40 memory locations in which values
of type real can be stored.


grades(1) refers to the first element of the
array.
Grades(40) refers to the 40th element of the
array.
Each subscripted variable grades(1),
grades(2), …, grades(40) maps an individual
memory location and can be used the same
way as a simple variable.
Ex:




Grades(5) =41.6
stores 41.6 in the fifth location of
grades.

Print*, grades(20)
displays the value stored in the 20th
location.


Subscripts attached to an array may be an
integer variable or expression
Read*, grades(n)
where n holds an integer value.
To read the 40 grades,
do I =1, 40
read(10,*) grades(I)
end do
Note


Each value in the input file must be on a
separate line, because each execution of the
read inquires a new line.
Equivalent to :
Read(10,*) grades(1)
Read(10,*) grades(2)
.
.
Read(10,*) grades(40)


An alternative method of reading or
displaying an array is to use input or output
statements containing the array name.
Read(10,*) grades
Read(10,*) grades(1),grades(2),…, grades(40)



here all values need not be read from
separate lines.
Implied Do Loops


Using an implied do loop provides
another way for Input/Output.

Form

(list of vars, control_var = init_value,limit,step)


It has exactly the same effect as the do
loop.
Ex:


Read(10,*) ( grades(I), I = 1, 40)
Read(10,*) grades(1), grades(2), …

or
print (10,*) (grades(I), I = 1,40)
print (10,*) grades(1), grades(2), …

More Related Content

What's hot

What's hot (20)

Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
Presentation of array
Presentation of arrayPresentation of array
Presentation of array
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Arrays
ArraysArrays
Arrays
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
 
Ii pu cs practical viva voce questions
Ii pu cs  practical viva voce questionsIi pu cs  practical viva voce questions
Ii pu cs practical viva voce questions
 
Introduction to Data Structure
Introduction to Data Structure Introduction to Data Structure
Introduction to Data Structure
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Introduction to Data Structure : Pointer
Introduction to Data Structure : PointerIntroduction to Data Structure : Pointer
Introduction to Data Structure : Pointer
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Introduction java programming
Introduction java programmingIntroduction java programming
Introduction java programming
 
Array 2 hina
Array 2 hina Array 2 hina
Array 2 hina
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Introduction on Data Structures
Introduction on Data StructuresIntroduction on Data Structures
Introduction on Data Structures
 

Viewers also liked

Viewers also liked (7)

Metamateria lpp (1)
Metamateria lpp (1)Metamateria lpp (1)
Metamateria lpp (1)
 
Fortran cơ sở
Fortran cơ sởFortran cơ sở
Fortran cơ sở
 
Intr fortran90
Intr fortran90Intr fortran90
Intr fortran90
 
Fortran 95
Fortran 95Fortran 95
Fortran 95
 
Fortran introduction
Fortran introductionFortran introduction
Fortran introduction
 
Fortran 90 Basics
Fortran 90 BasicsFortran 90 Basics
Fortran 90 Basics
 
Uni texus austin
Uni texus austinUni texus austin
Uni texus austin
 

Similar to 13 arrays

Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptxSajalFayyaz
 
20130215 Reading data into R
20130215 Reading data into R20130215 Reading data into R
20130215 Reading data into RKazuki Yoshida
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence RelationNilaNila16
 
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptxAbhimanyuChaure
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 
The Ring programming language version 1.6 book - Part 33 of 189
The Ring programming language version 1.6 book - Part 33 of 189The Ring programming language version 1.6 book - Part 33 of 189
The Ring programming language version 1.6 book - Part 33 of 189Mahmoud Samir Fayed
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysMaulen Bale
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshopBAINIDA
 

Similar to 13 arrays (20)

Arrays In C
Arrays In CArrays In C
Arrays In C
 
14 arrays
14 arrays14 arrays
14 arrays
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
20130215 Reading data into R
20130215 Reading data into R20130215 Reading data into R
20130215 Reading data into R
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence Relation
 
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
The Ring programming language version 1.6 book - Part 33 of 189
The Ring programming language version 1.6 book - Part 33 of 189The Ring programming language version 1.6 book - Part 33 of 189
The Ring programming language version 1.6 book - Part 33 of 189
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
Arrays
ArraysArrays
Arrays
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Arrays
ArraysArrays
Arrays
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 

More from fyjordan9

More from fyjordan9 (16)

17recursion
17recursion17recursion
17recursion
 
16 subroutine
16 subroutine16 subroutine
16 subroutine
 
15 functions
15 functions15 functions
15 functions
 
12 doloops
12 doloops12 doloops
12 doloops
 
11 doloops
11 doloops11 doloops
11 doloops
 
10 examples for if statement
10 examples for if statement10 examples for if statement
10 examples for if statement
 
9 case
9 case9 case
9 case
 
8 if
8 if8 if
8 if
 
7 files
7 files7 files
7 files
 
6 read write
6 read write6 read write
6 read write
 
5 format
5 format5 format
5 format
 
4 design
4 design4 design
4 design
 
3 in out
3 in out3 in out
3 in out
 
2 int real
2 int real2 int real
2 int real
 
1 arithmetic
1 arithmetic1 arithmetic
1 arithmetic
 
PHYS303
PHYS303PHYS303
PHYS303
 

Recently uploaded

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 

Recently uploaded (20)

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 

13 arrays

  • 2. Introduction     So far we have covered the predefined Fortran data types; Integer, Real, Character, and Logical. These data types are called simple types because a data value of one of these types consists of a single value. It might be necessary for some application to process a collection of values, e.g. a list of scores. To do that we use Arrays.
  • 3. Ex:  Suppose we want to calculate the average of the grades of 40 students in a class, then display all grades greater than the average. The grades are stored in a file.
  • 4.  Program Avg implicit none integer:: openstatus, Inputstatus real:: grade1, grade2,… , grade40, avg . . Open(unit=10,file=“grade.avg”,status=“old” , & IOSTAT= openstatus) if (openstatus>0) stop “ **error**”
  • 5.  Read(10,*,IOSTATUS= inputstatus) grade1,& grade2,……… , grade40 if (inputstatus>0) stop “**Input Error**” if (inputstatus<0) stop “not enough data” . . Avg= (grade1+grade2+…+grade40)/40
  • 6.   If (grade1>avg) print*, grade1 If (grade2>avg) print*, grade2 . . If (grade40>avg) print*, grade40 End Program Avg Is this a practical approach?? What if we have 15432 students?
  • 7.  Another way is to rewind the file Program Avg2 implicit none integer:: openstatus, Inputstatus integer:: No_of_grades, I real:: grade, sum, avg . . Open(unit=10,file=“grade.avg”,status=“old” , & IOSTAT= openstatus) if (openstatus>0) stop “ **error**”
  • 8.  No_of_grades = 0 Do Read(10,*,IOSTATUS= inputstatus) grade if (inputstatus>0) stop “**Input Error**” if (inputstatus<0) stop “not enough data” No_of_grades = No_of_grades + 1 End do
  • 9.  Rewind (unit=10) sum = 0.0 Do I = 1, No_of_grades Read(10,*) grade sum= sum + grade End do Avg = sum/ No_of_grades
  • 10.  Rewind (unit=10) Do I = 1, No_of_grades Read(10,*) grade if (grade>avg) print*, grade End do End program Avg2 this approach is slow because files are stored in the hard drive which is slower than the memory.
  • 11. Arrays     We need a data structure to store and organize the entire collection of grades. This structure should be stored in the main memory to reduce retrieval time. Instructions to access this structure should be simple. Direct access to data  it should take the same time to access grade40 as it takes to access grade1
  • 12. Arrays Declaration  Real, Dimension(40):: grades instructs the compiler to establish an array with the name grades consisting of 40 memory locations in which values of type real can be stored.
  • 13.  grades(1) refers to the first element of the array. Grades(40) refers to the 40th element of the array. Each subscripted variable grades(1), grades(2), …, grades(40) maps an individual memory location and can be used the same way as a simple variable.
  • 14. Ex:   Grades(5) =41.6 stores 41.6 in the fifth location of grades. Print*, grades(20) displays the value stored in the 20th location.
  • 15.  Subscripts attached to an array may be an integer variable or expression Read*, grades(n) where n holds an integer value. To read the 40 grades, do I =1, 40 read(10,*) grades(I) end do
  • 16. Note  Each value in the input file must be on a separate line, because each execution of the read inquires a new line. Equivalent to : Read(10,*) grades(1) Read(10,*) grades(2) . . Read(10,*) grades(40)
  • 17.  An alternative method of reading or displaying an array is to use input or output statements containing the array name. Read(10,*) grades Read(10,*) grades(1),grades(2),…, grades(40)  here all values need not be read from separate lines.
  • 18. Implied Do Loops  Using an implied do loop provides another way for Input/Output. Form (list of vars, control_var = init_value,limit,step)  It has exactly the same effect as the do loop.
  • 19. Ex:  Read(10,*) ( grades(I), I = 1, 40) Read(10,*) grades(1), grades(2), … or print (10,*) (grades(I), I = 1,40) print (10,*) grades(1), grades(2), …