advertisement

13 arrays

60 %
40 %
advertisement
Information about 13 arrays
Education

Published on March 9, 2014

Author: fyjordan9

Source: slideshare.net

advertisement

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), …

Add a comment

Related presentations

Related pages

Galileo Computing :: C von A bis Z – 13 Arrays

Kapitel 13 Arrays. Bisher wurden die speicherorientierten Strukturelemente auf einfache Datentypen beschränkt. Bei den Aufgaben wurden lediglich ganze ...
Read more

13.9 Mehrdimensionale Arrays - C von A bis Z

13.9 Mehrdimensionale Arrays Arrays, wie sie bisher besprochen wurden, können Sie sich als einen Strang von hintereinander aufgereihten Zahlen ...
Read more

C-Programmierung: Arrays – Wikibooks, Sammlung freier Lehr ...

Eindimensionale Arrays . Nehmen Sie einmal rein fiktiv an, Sie wollten ein Programm für Ihre kleine Firma schreiben, das die Summe sowie den höchsten und ...
Read more

PHP: Arrays - Manual

Arrays. Ein Array in PHP ist tatsächlich eine geordnete Map. Eine Map ist ein Typ, der Werte zu Schlüsseln zuordnet. Dieser Typ ist für einige ...
Read more

PHP: array - Manual

Das folgende Beispiel zeigt wie man ein zweidimensionales Array erstellt, wie man Schlüssel für assoziative Arrays festlegt, ... 13 years ago. Chek this ...
Read more

Rheinwerk Computing :: Java ist auch eine Insel – 3.7 Arrays

Java ist auch eine Insel - Das umfassende Handbuch – 3.7 Arrays. Professionelle Bücher. ... 3.7.13 Klonen kann sich lohnen – Arrays vermehren *
Read more

#13 Java - Mehrdimensionale Arrays - YouTube

Eigene Programme schreiben? Kein Problem! Mit diesem Tutorial kannst du ganz leicht die einfache, plattformunabhängige Programmiersprache Java ...
Read more

Kapitel 13 Objekte und Arrays - openbook.rheinwerk-verlag.de

13.1.3 Array-Elemente mischen Als Anwendungsbeispiel für Arrays wird ein Kartenblatt verwaltet; jedes Array-Element ist eine Karte, die allerdings selbst ...
Read more

PHP Tutorial 13 - Arrays (PHP For Beginners) - YouTube

In this video we will cover two types of Arrays; a numeric array and associative array. Arrays can be used to store a list of items that are ...
Read more

Kapitel 8 Arrays - pst.ifi.lmu.de

Einführung in die Informatik: Programmierung und Software-Entwicklung, WS 12/13 Erweiterungen zur Behandlung von Arrays: Überblick Arrays 3
Read more