Programming For Nuclear Engineers Lecture 6 Arrays

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

How SAS implements structured programming constructs
Intermediate Code Generation
Introduction to arrays
Introduction to Programming in C++ John Galletly.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
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.
Arrays Array Terminology Declarations Visualisation of Arrays Array Conformance Array Element Ordering Array Syntax Whole Array Expressions Array Sections.
Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
FORTRAN Short Course Week 2 Kate Thayer-Calder February 23, 2009.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 2 Basic Elements of Fortan
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
CSC 111 Course orientation
Chapter 8 Arrays and Strings
VB .NET Programming Fundamentals
Fortran: Specification Statements Session Six ICoCSIS.
Introduction to FORTRAN
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Multi-Dimensional Arrays
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Arrays Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Unsur-unsur Algoritma
Test 2 Review Outline.
User-Written Functions
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 6: User-Defined Functions I
GC211Data Structure Lecture2 Sara Alhajjam.
September 8th.
Complex data types Complex data types: a data type made of a complex of smaller pieces. Pascal has four very commonly used complex data types: strings,
JavaScript Syntax and Semantics
Lecture 13 & 14.
Chapter 7 Arrays.
Functions, variables, operators, loops Modularity
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Scripts & Functions Scripts and functions are contained in .m-files
Array processing and Matrix manipulation
JavaScript: Functions.
Stacks Chapter 4.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Pointers and Arrays Chapter 12
Subroutine Comp 208 Yi Lin.
Computational Methods of Scientific Programming
Arrays, For loop While loop Do while loop
7 Arrays.
Midterm Review Programming in Fortran
Lecture 18 Arrays and Pointer Arithmetic
Pointers and Arrays Chapter 12
Starting Out with Programming Logic & Design
Chapter 6: User-Defined Functions I
7 Arrays.
mdul-cntns-sub-cmmnts2.f90
Revision of C++.
CMPE 152: Compiler Design March 7 Class Meeting
Lecture 14: Problems with Lots of Similar Data
C++ Array 1.
Array processing and Matrix manipulation
Arrays Introduction to Arrays Reading for this Lecture:
Presentation transcript:

Programming For Nuclear Engineers Lecture 6 Arrays

2- Using Array Elements in Fortran Statements Introduction 1- Declaring Arrays 2- Using Array Elements in Fortran Statements 2.1- Initialization of Array Elements 3- Input and Output 3.1- Input and Output of Array Elements 3.2 -The Implied DO Loop 3.3 -I/O of whole arrays and array sections

Introduction An array is a group of variables or constants, all of the same type, that are referred to by a single name. Arrays can be extremely powerful tools. They permit us to apply the same algorithm over and over again to many different data items with a simple DO loop. For example, suppose that we need to take the sine of 100 different real numbers. If the numbers are stored as elements of any array a consisting of 100 real values, then the code DO i = 1, 100 a(i) = sin(a(i)) END DO

1- Declaring Arrays The type and the number of elements that the array contains must be declared to the compiler in a type declaration statement. The size of the array being defined is declared by using the DIMENSION attribute. For example, a real array flux containing 20 elements could be declared as follows: REAL, DIMENSION(20) :: flux or REAL :: flux(20)

- The number of elements in a given dimension of an array is called the extent of the array in that dimension. - The number of subscripts declared for a given array is called the rank of the array. - The combination of the rank and the extent of the array, is called the shape of an array. - The size of an array is the total number of elements declared in that array. - An Array Constant, is an array consisting entirely of constants. It is defined by placing the constant values between special delimiters, called array constructors. For example, the expression shown below defines an array constant containing five integer elements: (/1,2,3,4,5/) Fortran 95 [1,2,3,4,5] Fortran 2003

2- Using Array Elements in Fortran Statements Array elements may be included in arithmetic and logical expressions, and the results of an expression may be assigned to an array element. Each element of an array is a variable just like any other variable, and an array element may be used in any place where an ordinary variable of the same type may be used. For example, assume that arrays index and temp are declared as: INTEGER, DIMENSION(10)::index REAL, DIMENSION(3)::temp Then the following Fortran statements are valid: Index(1)=5 temp(3)= REAL(index(1))/4. WRITE(*,*)’index(1) = ‘ , index(1)

2.1 Initialization of Array Elements The values in an array must be initialized before use. If an array is not initialized, the contents of the array elements are undefined. The elements in an array may be initialized by one of three techniques: By using assignment statements. In type declaration statements at compilation time. By using READ statements. 1. Assignment statements the following examples will initialize the elements of array1: REAL, DIMENSION(10)::array1 DO i= 1,10 DO loop will initialize the elements of array1(i)=REAL(i) array array1 to 1.0,2.0,3.0,etc. END DO

using an array constructor will accomplish the same function all at once: REAL, DIMENSION(10)::array1 array1= (/1.,2.,3.,4.,5.,6.,7.,8.,9.,10./) 2.Type declaration statements To initialize an array in a type declaration statement, we use an array constructer to declare its initial value in that statement. INTEGER, DIMENSION(5)::array1=(/1,2,3,4,5/) To initialize larger arrays, we can use an implied DO loop, which have the general form: (arg1,arg2,…,index=istart,iend,incr) Where arg1, arg2,etc., are values evaluated each time the loop is executed. The variable index is set to the value istart. index is incremented by incr in each successive loop until its index*incr>iend*incr, at which time the loop terminates

3- Input and Output 3.1 Input and Output of Array Elements 3. READ statements Arrays may also be initialized with READ statements. The use of arrays in I/O statements will be described in the next section. 3- Input and Output 3.1 Input and Output of Array Elements READ and WRITE statements containing array elements are just like READ and WRITE statements for any other variables. 3.2 The Implied DO Loop It allows an argument list to be written many times as a function of an index variable. The general form of a WRITE or READ statement with an implied DO loop is: WRITE (unit,format)(arg1,arg2,…,index=istart,iend,incr) READ (unit,format) (arg1,arg2,…,index=istart,iend,incr)

For the following example, what will be the output? 1- WRITE(*,100) (i, 2*i, 3*i, i = 1,3) 1000 FORMAT (1X, 9I6) 2- WRITE (*,100) ((i, j, j = 1, 3),i = 1, 2) 100 FORMAT (1X, I5, 1X, I5) 3- WRITE (*,100) (a(i), i = 1, 5) 100 FORMAT (1X, ‘a = ‘ , 5F10.2) 4- n_factorial = 1 DO i = 1, n n_ factorial = n_factorial * i END DO

1- 1 2 3 2 4 6 3 6 9 2- 1 1 1 2 1 3 2 1 2 2 2 3 3- 1.00 2.00 3.00 4.00 5.00 4- depend on the value of n, if n is 10, the DO loop parameters will be istart = 1, iend = 10, and incr = 1, so the resulting value of n_factorial will be 1x2x3x4x5x6x7x8x9x10 = 3628800

3.3 I/O of whole arrays and array sections Entire arrays or array sections may also be read or written with READ and WRITE statements. The following example how we could use an array and two array sections in I/O statements. PROGRAM array_io … IMPLICIT NONE ! Data dictionary: declare variable types &definitions REAL, DIMENSIONS(5)::a=(/1.,2.,3.,20.,10./) INTEGER,DIMENSION(4)::vec=(/4,3,4,5/) !Output entire array. WRITE(*,100)a 100 FORMAT(2X,5F8.3)

Output array section selected by a triplet. WRITE (. ,100)a(2::2) !Output array section selected by a triplet. WRITE (*,100)a(2::2) !Output array section selected by a vector subscript. WRITE(*,100)a(vec) END PROGRAM array_io The output from this program is: 1.000 2.000 3.000 20.000 10.000 2.000 20.000 20.000 3.000 20.000 10.000 WHY?