ARRAYS COMPUTER PROGRAMMING. By Zerihun Alemayehu

Similar documents
Goals for This Lecture:

Chapter 4. Fortran Arrays

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

FORTRAN - ARRAYS. For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,

INTRODUCTION TO FORTRAN PART II

Review More Arrays Modules Final Review

Bits, Bytes, and Precision

Chapter 6. A Brief Introduction to Fortran David A. Padua

Declaration and Initialization

A Brief Introduction to Fortran of 15

Allocating Storage for 1-Dimensional Arrays

2 3. Syllabus Time Event 9:00{10:00 morning lecture 10:00{10:30 morning break 10:30{12:30 morning practical session 12:30{1:30 lunch break 1:30{2:00 a

SHAPE Returns the number of elements in each direction in an integer vector.

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Introduction to Programming with Fortran 90

6.1 Expression Evaluation. 1 of 21

Practical Exercise 1 Question 1: The Hello World Program Write a Fortran 95 program to write out Hello World on the screen.

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

AMath 483/583 Lecture 8

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

Introduction to Arrays. Midterm Comments. Midterm Results. Midterm Comments II. Function Basics (Problem 2) Introduction to Arrays April 11, 2017

How to declare an array in C?

MATLAB NOTES. Matlab designed for numerical computing. Strongly oriented towards use of arrays, one and two dimensional.

Chapter 7: Programming in MATLAB

More Coarray Features. SC10 Tutorial, November 15 th 2010 Parallel Programming with Coarray Fortran

Size, Alignment, and Value Ranges of Data Types. Type Synonym Size Alignment Value Range. BYTE INTEGER*1 8 bits Byte

Array Accessing and Strings ENGR 1181 MATLAB 3

Matrix Multiplication

Module 25.2: nag correl Correlation Analysis. Contents

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Matrix Multiplication

Chapter 3. Fortran Statements

Module 7.2: nag sym fft Symmetric Discrete Fourier Transforms. Contents

CSI33 Data Structures

Arrays. CSE 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures. Arrays

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CS 1313 Spring 2000 Lecture Outline

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

(8-1) Arrays I H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (October 8, 2018) Washington State University

Array Processing { Part II. Multi-Dimensional Arrays. 1. What is a multi-dimensional array?

Arrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures.

Introduction to Computer Science Midterm 3 Fall, Points

Module 28.3: nag mv rotation Rotations. Contents

Lecture V: Introduction to parallel programming with Fortran coarrays

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis

function [s p] = sumprod (f, g)

Introduction to Fortran95 Programming Part II. By Deniz Savas, CiCS, Shef. Univ., 2018

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018)

CSE 262 Spring Scott B. Baden. Lecture 4 Data parallel programming

Introduction to Programming for Engineers Spring Final Examination. May 10, Questions, 170 minutes

Personal SE. Functions & Arrays

Module 6: Array in C

Lecture 2: Variables, Vectors and Matrices in MATLAB

Appendix D. Fortran quick reference

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CHAPTER 5 SYSTEMS OF EQUATIONS. x y

Chapter 9 Introduction to Arrays. Fundamentals of Java

The G3 F2PY for connecting Python to Fortran 90 programs

PROGRAMS. EXCELLENT ACADEMY OF ENGINEERING. Telephone: / NORMAL PROGRAM


MIDTERM EXAM (Solutions)

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

CS 2505 Computer Organization I

Introduction to MatLab. Introduction to MatLab K. Craig 1

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Module 19.1: nag ip Integer Programming. Contents

Pointers and Arrays 1

Arrays. Arizona State University 1

Fortran 2003 Part 1. École normale supérieure L3 geosciences 2018/2019. Lionel GUEZ Laboratoire de météorologie dynamique Office E324

1 Lexical Considerations

CSC Advanced Scientific Computing, Fall Numpy

NAGWare f95 Recent and Future Developments

Module 25.1: nag lin reg Regression Analysis. Contents

Dr Richard Greenaway

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

HPF commands specify which processor gets which part of the data. Concurrency is defined by HPF commands based on Fortran90

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

FORM 2 (Please put your name and form # on the scantron!!!!)

Fortran 90 - A thumbnail sketch

Programming for Engineers Arrays

Activity 7: Arrays. Content Learning Objectives. Process Skill Goals

Introduction to Matrix Operations in Matlab

CS , Fall 2001 Exam 2

Chapter 7 Multidimensional Arrays

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

BITG 1113: Array (Part 1) LECTURE 8

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Page 1 of 7. Date: 1998/05/31 To: WG5 From: J3/interop Subject: Interoperability syntax (Part 1) References: J3/98-132r1, J3/98-139

NAGWare f95 and reliable, portable programming.

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

UNIVERSITY OF CALIFORNIA, COLLEGE OF ENGINEERING E77N: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS

Array programming languages

Class Notes, 3/21/07, Operating Systems

Transcription:

ARRAYS COMPUTER PROGRAMMING By Zerihun Alemayehu AAiT.CED Rm. E119B

BASIC RULES AND NOTATION An array is a group of variables or constants, all of the same type, that are referred to by a single name. Array elements are indexed or subscripted, just like x 1, x 2,..., x n in mathematics. Array Elements Array subscript

BASIC RULES AND NOTATION Array Declaration REAL, DlMENSION (10):: X Declares X to be an array (or list) with 10 real elements, denoted by X(1), X(2),...,X(10). The number of elements in an array is called its size (10 in this case). Each element of an array is a scalar (singlevalued).

BASIC RULES AND NOTATION REAL, DIMENSION (0:100): : A declares A to have 101 elements, from A(0) to A(100). The upper bound must be specified; if the lower bound is missing it defaults to 1. REAL, DIMENSION (2, 3) : : A A is a two-dimensional array. The number of elements along a dimension is called the extent in that dimension. A has an extent of 2 in the first dimension, and an extent of 3 in the second dimension(and a size of 6).

ONE AND TWO DIMENSIONAL ARRAY Array1 (row) Array2 (row, col)

BASIC RULES AND NOTATION Fortran allows up to seven dimensions The number of dimensions of an array is its rank, and the sequence of extents is its shape. The shape of A is (2, 3), or (2 x 3) in matrix notation. A scalar is regarded as having a rank of zero. An array with a rank of one is called a vector An array with a rank of 2 or greater is called a matrix

BASIC RULES AND NOTATION REAL, DIMENSION (5): : A, B(2,3), C(10)! Only A is (1:5) INTEGER :: I (10), K (4, 4), L (5)! All different DIMNESION key word can be omitted Array constants (/ 1, 2, 5, 6, 9 /)! In FORTRAN 95 [ 1, 2, 5, 6, 9 ]! In FORTRAN 2003

BASIC RULES AND NOTATION An array subscript can be used as the control variable in a DO loop to generate array elements. DO I = 1, 5 X(I) = 2 * I END DO Integer, dimension(5), parameter:: i=6! Assigns a constant value of 6 to all the five elements of i READ*, X! An entire array may be read X = 1! An entire array assigned a scalar value

BASIC RULES AND NOTATION a = i = 100 y i i= 1 This expression represents the sum of all the elements stored in the subscripted variable y and the statement stores the result in a. REAL,DIMENSION(100):: Y READ*, Y A=0.0 DO I = 1,100 A = A + Y(I) END DO

ARRAY STORAGE The array element ordering: conceptualise a linear sequence of the array elements with the first index changing first. REAL, DIMENSION(3, 5) :: a

ARRAY ASSIGNMENT Whole array assignment REAL, DIMENSION(100) :: a, b, c REAL :: d(10,10) = 0.0 b = 2*a+4 a = c = b*a c = d! Illegal, the two arrays are not conformable

ARRAY ASSIGNMENT Array section assignment alpha beta alpha REAL, DIMENSION(10) :: alpha, beta alpha = beta = alpha + alpha(1:10:2) = beta(1:5) * 1 2 3 4 5 6 4.0 4.0 4.0 4.0 4.0 4.0 8.0 8.0 8.0 7 8 9 10 4.0 4.0 4.0 4.0 8.0 8.0

INITIALIZING ARRAY Constructors array = (/ list /) INTEGER :: a(6)=(/ 1, 2, 3, 6, 7, 8 /) variable expression(s) REAL :: b(2)=(/ SIN(x), COS(x) /) array expression(s) INTEGER :: c(5)=(/ 0, a(1:3), 4 /)

INITIALIZING ARRAY Implied DO loops (value list, variable = start, end [, step]) REAL :: d(100)=(/ REAL(i), i=1,100 /)!1 up to 100 INTEGER :: A (6) = (/ (I, I = 1, 2), J = 1, 3) /) the same as (/ 1, 2, 1, 2, 1, 2 /)

INITIALIZING ARRAY RESHAPE used for the initialization or assignment of multidimensional arrays RESHAPE (list, shape [,PAD] [,ORDER]) INTEGER, DIMENSION(2,3) :: a a=reshape((/i,i=0,5/),(/3,2/)) RESHAPE (SOURCE = (/1, 2, 3, 4, 5, 6/), SHAPE = (/2, 3/)) -- -- forms the (2 x 3) matrix 1 3 5 2 4 6

INITIALIZING ARRAY DATA statement DATA variable / list /... INTEGER :: a(4), b(2,2), c(3,3) DATA a/4,3,2,1/ DATA a/4*0/ DATA b(1,:)/0,0/ DATA b(2,:)/1,1/ DATA ((C(I,J), J= 1, 3), I=1, 3) /3*0, 3*1, 3*2/

WHERE STATEMENT used when the value of an element depends on the outcome of some condition. The WHERE statement allows a single array assignment only if a logical condition is true. WHERE (condition) statement INTEGER :: a(2,3,4) WHERE(a< 0) a=0 WHERE(a*3>10) a=999

WHERE STATEMENT The WHERE construct allows array assignment(s) only if a logical condition is true, and alternative array assignement(s) if false. WHERE (condition) block1 [ELSEWHERE block2] ENDWHERE INTEGER :: b(8,8) READ*, b WHERE (b<=0) b=0 ELSEWHERE b=1/b ENDWHERE

WHERE EXAMPLE Write a WHERE statement which replicates every nonzero element of an array beta by its reciprocal and every zero element by 1. INTEGER :: beta(8,8) READ*, b WHERE (beta == 0) beta=1 ELSEWHERE beta=1/beta ENDWHERE

ARRAYS: EXPERIMENTAL WORK Write a program that calculates dot product of two vectors a and b which is defined by. z = a b i = = = i Give values of a and b at the beginning of the program. And the result onto screen. 3 1 a i b i

ARRAYS: EXPERIMENTAL WORK Real, Dimension (3) :: a,b Real:: z=0.0 Read*, a Read*, b Do I =1,3 z=z+a(i)*(b(i) End do Print*, The dot product =, z

ALLOCATABLE (DYNAMIC) ARRAYS REAL, DIMENSION(:), ALLOCATABLE: : X Although its rank is specified, it has no size until it appears in an ALLOCATE statement, such as: ALLOCATE(X (N)) DEALLOCATE(X)!freeing up the memory used.

ARRAY EXAMPLE Write a program which first reads the number of people sitting an exam. It should then read their marks (or scores) in to an array and print the highest and lowest marks, followed by the average mark for the class.

SOLUTION Declare a dynamic array Read the no. of students, n Allocate the size of the array n Read all the marks Use do loop to find the max and min Calculate the sum Calculate the average Print the result

SOLUTION REAL, DIMENSION (:) :: mark INTEGER :: n PRINT*, Give me the no. of students READ*, n ALLOCATE (mark (n)) PRINT*, Give me the marks READ*, mark max_grade = MAXVAL (mark) min_grade = MINVAL (mark) avg = SUM(mark)/n DEALLOCATE (mark) PRINT *, Maximum =, max, Minimum =, min, average=, avg