SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Basic Scientific Programming
Arrays
Arrays Declaration




type, dimension (l:u):: array_name
or
type:: array_name
l,u: are integer constants.
Ex:
integer, dimension(40)::grades
integer:: grades(40)
integer:: grades(0:39), avg(1:40)
integer:: final(21:60)
Ex:




To read 6 number from the keyboard.
Real, dimension(6):: example
integer:: j
do j =1,6
print*, ‘Enter a number’
read*, example(j)
end do
Or print*, ‘please enter 6 numbers’
read*, example
Compile Time & Run Time Arrays








The memory for grades is allocated at
compile time.
This means that the size of such compile-time
arrays is fixed before execution begins.
If the data to be stored in the array is too
large, it cannot be stored in the array and
processed correctly.
To deal with this problem, we use run-time
arrays for which memory is allocated during
execution.
Allocatable Arrays


Integer :: N
print*, ‘ how many elements?’
read*, N
Real:: Grades(N)

THIS IS WRONG !!
Declaration of Allocatable Arrays


Type, dimension, Allocatable:: Array_name
the actual bounds of an allocatable array can
be specified in an Allocate statement.

Form
Allocate(array_name)
Allocate(array_name,Stat= StatusVariable)
Ex:






Print*, ‘Enter size of arrays A and B:’
read*, N
Allocate(A(N),B(0:N+1), stat=allostatus)
if(allostatus /= 0) stop “not enough memory”
If memory is available, A can store N values
and be can store N+2 values
Once memory has been allocated, these
arrays may be used in the same ways as
compile-time arrays.
Deallocate


Memory that is not needed anymore
should be cleared so that we can use it
for allocating other arrays.

Form deallocate(grades)
or

deallocate(grades,stat=StatusVar)

Please read the example in page 530.
Array Constants


An array constant may be constructed
as a list of values enclosed between (/
and /)
(/ Value1, Value2, … , Value n /)
Ex: integer:: A(5)
A= (/ 2,4,6,8,10/)
A= ( /(2*I, I = 1,5)/ )
Array Expressions




Operators and functions normally
applied to simple expressions may also
be applied to arrays having the same
number of elements.
Operations applied to an array are
carried out element wise.




Integer,dimension(4):: A,B,C
…
A= B + C
Do I = 1,4
A(I)= B(I)+ C(I)
end do




A=2*C
or Do I = 1,4
A(I)= 2 * C(I)
End do
C = 0 assigns zeros to all elements of
C
Array Sections and Subarrays


In some cases, it is desired to have access to
only a part of the array. We can either use Do
loops or subarrays.

Form array_name(lower:upper:stride)
integer:: A(10)
A=(/11,22,33,44,55,66,77,88,99,110/)
print*, A(2:10:2)
prints A(2), A(4), A(6), A(8), A(10)
The WHERE Construct


May be used to assign values to arrays
depending on the value of a logical array
expression.

Form where (logical_array_expr)
array_var1= array_expr1
Elsewhere
array_var2= array_expr2
End where
Ex:


Integer, dimension(5):: A=(/0,2,5,0,10/)
Real:: B(5)
Where (A>0)
B= 1.0/ Real(A)
elsewhere
B= -1.0
end where
B will store –1.0, 0.5, 0.2, -1.0, 0.1


Same as
Do I=1,5
if (A(I) > 0) then
B(I) = 1.0/ Real(A(I))
else
B(I) = -1.0
end if
end do
Intrinsic Array-Processing Subprograms


Fortran 90 provides several intrinsic functions
whose arguments are arrays, some of the
useful ones are:
Dot_product(A,B)
Maxval(A)
Maxloc(A)
Minval(A)
Minloc(A)
refer to page 548 and Appendix D.
Programmer-defined Array-Processing
Subprograms


The actual array argument must be declared
in the calling program unit, and the
corresponding formal argument must be
declared in the subprogram.

…
Actual Array Arg.

…
Formal Array Arg.
Array Valued Functions


A function may return an array as the
return argument.
2-Dimensional Arrays








2-D arrays have two indices to indicate rows
and columns.(matrix)
The first index corresponds to rows, the
second to columns.
Real:: Matrix(5,3)
declares a matrix with 5 rows and 3
columns.
Integer, dimension(22:28,1:24):: hours
declares a matrix of 7 rows and 24 columns


2D arrays follow all the rules 1D arrays
have.
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
(4,1) (4,2) (4,3)
(5,1) (5,2) (5,3)
Do I=1,5
Do J = 1,5
matrix(I,J) = I * J
end do
1 2 3
End do
2
3
4
5

4
4 6 8
6 9 12
8 12 16
10 15 20

5
10
15
20
25

Weitere ähnliche Inhalte

Was ist angesagt?

C programming session 05
C programming session 05C programming session 05
C programming session 05
Vivek Singh
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Kumar Boro
 
Csc1100 lecture09 ch07_pt2
Csc1100 lecture09 ch07_pt2Csc1100 lecture09 ch07_pt2
Csc1100 lecture09 ch07_pt2
IIUM
 

Was ist angesagt? (20)

Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Array and string
Array and stringArray and string
Array and string
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
 
Csc1100 lecture09 ch07_pt2
Csc1100 lecture09 ch07_pt2Csc1100 lecture09 ch07_pt2
Csc1100 lecture09 ch07_pt2
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Functions class11 cbse_notes
Functions class11 cbse_notesFunctions class11 cbse_notes
Functions class11 cbse_notes
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Strings and pointers
Strings and pointersStrings and pointers
Strings and pointers
 
Array
ArrayArray
Array
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 

Andere mochten auch (9)

13 arrays
13 arrays13 arrays
13 arrays
 
11 doloops
11 doloops11 doloops
11 doloops
 
9 case
9 case9 case
9 case
 
10 examples for if statement
10 examples for if statement10 examples for if statement
10 examples for if statement
 
12 doloops
12 doloops12 doloops
12 doloops
 
17recursion
17recursion17recursion
17recursion
 
16 subroutine
16 subroutine16 subroutine
16 subroutine
 
15 functions
15 functions15 functions
15 functions
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 

Ähnlich wie 14 arrays

Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1
IIUM
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 

Ähnlich wie 14 arrays (20)

Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Array&string
Array&stringArray&string
Array&string
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
 
Arrays
ArraysArrays
Arrays
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Unit 2
Unit 2Unit 2
Unit 2
 
Array
ArrayArray
Array
 
Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Array in C
Array in CArray in C
Array in C
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 

Mehr von fyjordan9

Mehr von fyjordan9 (9)

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
 

Kürzlich hochgeladen

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Kürzlich hochgeladen (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

14 arrays

  • 2. Arrays Declaration   type, dimension (l:u):: array_name or type:: array_name l,u: are integer constants. Ex: integer, dimension(40)::grades integer:: grades(40) integer:: grades(0:39), avg(1:40) integer:: final(21:60)
  • 3. Ex:   To read 6 number from the keyboard. Real, dimension(6):: example integer:: j do j =1,6 print*, ‘Enter a number’ read*, example(j) end do Or print*, ‘please enter 6 numbers’ read*, example
  • 4. Compile Time & Run Time Arrays     The memory for grades is allocated at compile time. This means that the size of such compile-time arrays is fixed before execution begins. If the data to be stored in the array is too large, it cannot be stored in the array and processed correctly. To deal with this problem, we use run-time arrays for which memory is allocated during execution.
  • 5. Allocatable Arrays  Integer :: N print*, ‘ how many elements?’ read*, N Real:: Grades(N) THIS IS WRONG !!
  • 6. Declaration of Allocatable Arrays  Type, dimension, Allocatable:: Array_name the actual bounds of an allocatable array can be specified in an Allocate statement. Form Allocate(array_name) Allocate(array_name,Stat= StatusVariable)
  • 7. Ex:    Print*, ‘Enter size of arrays A and B:’ read*, N Allocate(A(N),B(0:N+1), stat=allostatus) if(allostatus /= 0) stop “not enough memory” If memory is available, A can store N values and be can store N+2 values Once memory has been allocated, these arrays may be used in the same ways as compile-time arrays.
  • 8. Deallocate  Memory that is not needed anymore should be cleared so that we can use it for allocating other arrays. Form deallocate(grades) or deallocate(grades,stat=StatusVar) Please read the example in page 530.
  • 9. Array Constants  An array constant may be constructed as a list of values enclosed between (/ and /) (/ Value1, Value2, … , Value n /) Ex: integer:: A(5) A= (/ 2,4,6,8,10/) A= ( /(2*I, I = 1,5)/ )
  • 10. Array Expressions   Operators and functions normally applied to simple expressions may also be applied to arrays having the same number of elements. Operations applied to an array are carried out element wise.
  • 11.   Integer,dimension(4):: A,B,C … A= B + C Do I = 1,4 A(I)= B(I)+ C(I) end do
  • 12.   A=2*C or Do I = 1,4 A(I)= 2 * C(I) End do C = 0 assigns zeros to all elements of C
  • 13. Array Sections and Subarrays  In some cases, it is desired to have access to only a part of the array. We can either use Do loops or subarrays. Form array_name(lower:upper:stride) integer:: A(10) A=(/11,22,33,44,55,66,77,88,99,110/) print*, A(2:10:2) prints A(2), A(4), A(6), A(8), A(10)
  • 14. The WHERE Construct  May be used to assign values to arrays depending on the value of a logical array expression. Form where (logical_array_expr) array_var1= array_expr1 Elsewhere array_var2= array_expr2 End where
  • 15. Ex:  Integer, dimension(5):: A=(/0,2,5,0,10/) Real:: B(5) Where (A>0) B= 1.0/ Real(A) elsewhere B= -1.0 end where B will store –1.0, 0.5, 0.2, -1.0, 0.1
  • 16.  Same as Do I=1,5 if (A(I) > 0) then B(I) = 1.0/ Real(A(I)) else B(I) = -1.0 end if end do
  • 17. Intrinsic Array-Processing Subprograms  Fortran 90 provides several intrinsic functions whose arguments are arrays, some of the useful ones are: Dot_product(A,B) Maxval(A) Maxloc(A) Minval(A) Minloc(A) refer to page 548 and Appendix D.
  • 18. Programmer-defined Array-Processing Subprograms  The actual array argument must be declared in the calling program unit, and the corresponding formal argument must be declared in the subprogram. … Actual Array Arg. … Formal Array Arg.
  • 19. Array Valued Functions  A function may return an array as the return argument.
  • 20. 2-Dimensional Arrays     2-D arrays have two indices to indicate rows and columns.(matrix) The first index corresponds to rows, the second to columns. Real:: Matrix(5,3) declares a matrix with 5 rows and 3 columns. Integer, dimension(22:28,1:24):: hours declares a matrix of 7 rows and 24 columns
  • 21.  2D arrays follow all the rules 1D arrays have. (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3) (4,1) (4,2) (4,3) (5,1) (5,2) (5,3)
  • 22. Do I=1,5 Do J = 1,5 matrix(I,J) = I * J end do 1 2 3 End do 2 3 4 5 4 4 6 8 6 9 12 8 12 16 10 15 20 5 10 15 20 25