Introduction to 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

Introduction to C Programming
Multidimensional Array
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
One Dimensional Arrays
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.
Chapter 3 Program Design And Branching Structures.
Programming Logic and Design Sixth Edition
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
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.
Maths for Computer Graphics
Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 8 ARRAYS. 2 Array subscript expressions  Each subscript in an array element designator is an expression of integer type. It need not be a constant.
Introduction to Programming with C++ Fourth Edition
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Arrays.
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Multi-Dimensional Arrays
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Arrays Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices.
Working with Arrays in MATLAB
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
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 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Computer Programming BCT 1113
Programming For Nuclear Engineers Lecture 6 Arrays
EGR 115 Introduction to Computing for Engineers
Numeric Arrays Numeric Arrays Chapter 4.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
CNG 140 C Programming (Lecture set 8)
EKT150 : Computer Programming
Starting Out with Programming Logic & Design
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Lets Play with arrays Singh Tripty
Introduction to Computer Programming IT-104
Presentation transcript:

Introduction to arrays Chapter 6 Introduction to arrays

What is an Array? Group of variables or constants, all of the same type, referred to by a single name. Array values occupy consecutive memory locations in the computer. Single value in an array called array element and referred to by the array name and a subscript (of type integer) that is pointing to its location in the group. Arrays allow a powerful computation that apply the same algorithm to many different items with a simple DO loop. Example: Find square root of 100 different real numbers. DO i= 1, 100 a(i) = SQRT(a(i)) END DO Rather than a1 = SQRT(a1) a2= SQRT(a2) … a100 = SQRT(a100) a ( 3 ) a ( 1 ) a ( 2 ) a ( 4 ) a ( 5 ) Computer Memory Array a

Arrays Arrays can be multi-dimensions. Number of subscripts called rank. Extent of array in one dimension is the number of values in that given dimension of the array. Shape of the array is the combination of its rank and the extent in each dimension. Size of array is the total number of elements in all dimensions.

Arrays One dimension array Vector(i) Rank: Extent: Shape: Rank, extent Size: Two dimension array Vector(i , j) Dimension - 2 Dimension - 1 Rank: Extent: Shape: Rank, extent Size: 1 15 2 5 , 4 20 Dimension - 1 Dimension - 1 Dimension - 2 Dimension - 3 Three dimension array Vector(i, j, k) Rank: Extent: Shape: Rank, extent Size: 3 5, 4, 3 60

Declaring Arrays Define type and size Array constant REAL, DIMENSION(16) :: voltage INTEGER, DIMENSION(5, 6):: matrix, values CHARACTER(len=20), DIMENSION(50) :: last_name Find rank, extent, shape and size of above arrays. Array constant (/ 3, 4, 5, 1, 3/)

Array example Declares an array that stores the student ID of all students in the class. (43 students) Make a WRITE statement that makes the program display the first student in the class. Make a WRITE statement that makes the program display the student number 25 in the list. INTEGER, DIMENSION(43) :: studentID WRITE(*,*) ‘ ID of first student: ’, studentID(1) WRITE(*,*) ‘ ID of 25th student: ’, studentID(25)

Array example Make two arrays that represent a building that consists of 7 floors. Each floor has 15 rooms. First array values indicates if the room is occupied or not. (True or False) The second array indicates how many persons in the room in case it is occupied. (Integer) Please, write to the customer if room number 9 in the 6th floor is occupied or not. Please, write how many persons in room 2 in the 7th floor. LOGICAL, DIMENSION (7,15) :: occupied INTEGER, DIMENSION (7,15) :: persons WRITE(*,*) ‘Is room 9 in floor 6 occupied ? ’, occupid(6,9) WRITE(*,*) ‘persons in room 2 at floor 7 are: ’, persons(7,2)

Using array elements in FORTRAN Each element in the array is treated as any regular FORTRAN variable. FORTRAN statements can access its values, assign values and make operations on them. Back to student ID example: INTEGER, DIMENSION(43) :: studentID studentID(5)=999999 IF (studentID(5)==999999) studentID(4)=888888 studentID(6) = MAX(studentID(4), studentID(5)) WRITE(*,*) ‘ ID of sixth student: ’, studentID(6)

Initialization of array elements Un-initialized array INTEGER, DIMENSION(10) :: j WRITE (*,*) ‘ j(1) = ’, j(1) Initialization with assignment statement REAL, DIMENSION(10) :: array1 DO I = 1, 10 array1(i) = REAL(i) END DO Initialization with assignment using array constructor array1 = (/1., 2., 3., 4., 5., 6., 7., 8., 9., 10./) Possible to assign one value to all array elements. array1 = 0.

Example-1 PROGRAM squares IMPLICIT NONE INTEGER :: i INTEGER, DIMENSION(10) :: number, square DO i=1, 10 number(i)=i square(i)=number(i)**2 END DO WRITE(*,*) number(i), ' ', square(i) END PROGRAM

Initialization of array elements Named constants in array declaration INTEGER, PARAMETER :: max_size=1000 INTEGER, DIMENSION(max_size) :: array1 REAL, DIMENSION(2*max_size) :: array2 Do i = 1, max_size array2(i) = array1(i) * 2.0 / 3.0 END DO

Initialization of array elements Initializing with READ statement INTEGER, DIMENSION(5) :: array1 Do i=1,5 READ(*,*) array1(i) END DO Initializing arrays in declaration (OK for small arrays) INTEGER, DIMENSION(5) :: array1 =(/1,2,3,4,5/) Number of values must be equal to the array size Declaring arrays using implied loops INTEGER, DIMENSION(100) :: array1 = ( / ( i , i=1,100) / ) General form of implied loop (arg1, arg2, …, index = istart, iend, incr)

Example-2 PROGRAM square_roots IMPLICIT NONE INTEGER :: i INTEGER, DIMENSION(10) :: value = (/ (i, i = 1, 10) /) INTEGER, DIMENSION(10) :: s_root DO i=1, 10 s_root(i) = SQRT(value(i)) END DO WRITE(*,*) values(i), ' ', s_root(i) END PROGRAM

More Implied loops (arg1, arg2, …, index = istart, iend, incr) INTEGER, DIMENSION(25) :: array = (\ ((0, i=1, 4), 5*j, j=1,5) \) (arg1, arg2, …, index = istart, iend, incr) array = 0, 0, 0, 0, 5, 0, 0, 0, 0, 10, 0, 0, 0, 0, 15, 0, 0, 0, 0, 20, 0, 0, 0, 0, 25 INTEGER, DIMENSION(25) :: array = (\ ((j, 2*j, 3*j, j=1,3) \) array = 1, 2, 3, 2, 4, 6, 3, 6, 9

Whole array operations REAL, DIMENSION(4):: a = (/ 1, 2, 3, 4 /) REAL, DIMENSION(4):: b = (/ 2, 2, 1, 0 /) REAL, DIMENSION(4):: c DO i = 1, 4 c(i) = a(i) + b(i) END DO WRITE(*,*) c(i) 2 1 3 4 + = a b c c = a + b WRITE(*,*) ‘ c = ’, c Output : c = 3.0 4.0 4.0 4.0

Out of bound subscripts REAL, DIMENSION(5) :: array array should have 5 values numbered 1 to 5 Any other subscript out of these bounds (1, 5) will result in an out of bounds error either detected by compiler (if compiler bounds check is turned on) or at run time.

Example-3 PROGRAM summation IMPLICIT NONE INTEGER :: i REAL, DIMENSION(6) :: x=(/ 1., 2., 3., 4., 5., 6./) REAL, DIMENSION(6) :: a=(/ .1, .1, .1, .2, .2, .2/) REAL :: total=0 DO i=1, 6 total = total + (2*x(i)+a(i)) END DO WRITE (*,*) 'Total = ', total END PROGRAM _____________________________________________ What does this program do?

Changing the Subscript Range of an Array REAL, DIMENSION(5):: arr Elements arr(1), arr(2), arr(3), arr(4), arr(5) Change range REAL, DIMESION(-2:2) Elements arr(-2), arr(-1), arr(0), arr(1), arr(2) What is he sixze of the following array? REAL, DIMENSION(-1:19) Size = upper – lower +1 = 19 - -1 +1 = 21

Out of bounds INTEGER, DIMESION(5):: arr Do i=1,5 WRITE(*,*) arr(i) END DO INTEGER, DIMESION(-2:2):: arr Do i=-2,2

arr(subscript1 : subscript2: increment) Array subset arr(subscript1 : subscript2: increment) INTEGER, DIMENSION(10):: a=(/1., -2., 3., -4., 5., -6., 7., -8., 9., -10./) Find the following subsets: arr(:) arr(3:7) arr(3:7:2) arr(3:7:7) arr(:6) arr(5:) arr(::2)

Example INTEGER, DIMENSION(3):: x=(/2, 5, 8/) INTEGER, DIMENSION(5):: y=(/1, 3, 2, 4, 7/) INTEGER, DIMENSION(2):: z z = x(1:2) + y(3:4) WRITE(*,*) z z = 4, 9

Vector Subscript The subset in previous slides used subscript triplets array(a:b:c) Vector subscript is using a vector or array as index values of another array. INTEGER, DIMENSION(5):: vec=(/1,6,4,1,9/) REAL, DIMENSION(10):: a=(/1, -2, 3, -4, 5, -6, 7, -8, 9, -10/) WRITE(*,*) a(vec) Output: a(1), a(6), a(4), a(1), a(9) 1, -6, -4, 1, 9

Subset Assignment INTEGER, DIMENSION(5):: vec=(/1,6,4,1,9/) REAL, DIMENSION(10):: a=(/1, -2, 3, -4, 5, -6, 7, -8, 9, -10/) REAL, DIMENSION(10):: b=(/1, -2, 3, -4, 5, -6, 7, -8, 9, -10/) a(2:8:2)=(/1, -1, 1, -1/) b(vec)=(/1,-1,1, -1, 1/) WRITE(*,*) ‘a = ’, a WRITE (*,*) ‘b = ’, b Output : a = 1, 1, 3, -1, 5, 1, 7, -1, 9, -10 b = -1, -2, 3, 1, 5, -1, 7, -8, 1, -10

Simple Output Format INTEGER :: a=10 REAL :: b=5.1 WRITE(*,*) a, b 100 FORMAT(I2, F10.8) Output: 105.10000000 100 FORMAT(I2, 1X, F10.8) 100 FORMAT(I2, 3X, F3.1) Output: 10 5.1 100 FORMAT(' a= ',I2, 3X, 'b= ', F3.1) Output: a= 10 b= 5.1

Input/output of array elements REAL, DIMENSION(5) :: a= (/0.5,-1,0.1,2., 10.4/) WRITE(*,*) (a(i), i=1, 5) Output: 0.500000000 -1.00000000 0.100000001 2.00000000 10.40000000 Do i=1,5 WRITE(*,*) a(i) END DO Output: 0.500000000 -1.00000000 0.100000000 2.00000000 10.40000000 WRITE(*,100) a(1), a(2), a(3), a(4), a(5) 100 FORMAT (1X, 'a =', 5F7.2) Output: a = 0.50 -1.00 0.10 2.00 10.40 WRITE(*,100) (a(i), i=1, 5) 100 FORMAT (F10.2) 0.50 -1.00 0.10 2.00 10.40 WRITE(*,100) (a(i), i=1, 5) 100 FORMAT (5F10.2) Output: 0.50 -1.00 0.10 2.00 10.40

Input/Output with implied loops WRITE(unit, format)(arg1, ar2,.., index=istart, iend, incr) READ(unit, format)(arg1, ar2,.., index=istart, iend, incr) WRITE(*, *) (i, 2*I, 3*I, i=1,3) Output: 1 2 3 2 4 6 3 6 9 WRITE(*,100) (a(i), i=1, 5) 100 (1X, ‘a= ’, 5F7.2) WRITE(*,200) ((i,j, j=1,3), i=1, 2) 200 FORMAT(1X, I5, 1X, I5) Output: 1 1 1 2 1 3 2 1 2 2 2 3

Two-Dimensional Array Rank-2 array require 2 subscripts as an address to retrieve one value. array(a,b) a is the index in the first dimension b is the index in the second dimension Declaring 2-D array REAL, DIMENSION(3,6) :: arr1 REAL, DIMENSION(-1:9, 0:19) :: arr2 INTEGER, DIMENSION(10, 0:19) :: arr3

Initializing 2-D arrays Using DO loops INTEGER, DIMENSION(4,3):: arr DO i=1, 4 DO j=1, 3 arr(i, j)= j END DO Using Constructor arr=(/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /) Memory arrangement in computer (/ arr(1,1), arr(2,1), arr(3,1), arr(4,1), arr(1,2), arr(2,2), arr(3,2), arr(4,2), arr(1,3), …, arr(4,3) /) Is array shape correct?? NO that is 1-D array arr=RESHAPE( (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /), (/4, 3/)) 2 3 1 2 3

Initializing 2-D arrays Initializing in Declaration INTEGER, DIMENSION(4,3):: arr=RESHAPE ( (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /), (/4,3/)) Initializing using READ READ(*,*) arr READ(*,*) ((arr(i,j), j=1,3), i=1,4)

2-D array subsets arr(:, 1) arr(1, :) arr(1:3, 1:4:2) arr(1:4:2, :) list = (/ 1, 2, 4/) arr(:, list) How to access the third column? How to access the fourth row? How to access the second and fourth columns together? 2 3 4 5 6 7 8 1 2 2 3 3 4 4 arr(:,3) arr(4,:) arr(:,2:4:2)

2-D array output 2 3 1 2 3 WRITE (*,*) arr 1 1 1 1 2 2 2 2 3 3 3 3 2 3 1 2 3 WRITE (*,*) arr 1 1 1 1 2 2 2 2 3 3 3 3 DO j=1, 3 DO i=1, 4 WRITE (*,*) arr(i, j) END DO WRITE (*,*) ((arr(i,j), i=1,4), j=1,3) WRITE (*,*) (arr(i, j), j=1,3) 1 2 3 1 2 3 1 2 3

2-D array example-1 Write a program that initializes a 3X4 matrix as shown below. The program should make search for minimum and maximum values in the 2-D array. Repeat the above program but let the user now enter the values of the matrix via the keyboard. 1.5 2. -3.1 4.0 5. 9.6 7.7 -8. 1.1 0.1 2.1 -2.

2-D array example-2 Write a program that initializes a 3X4 matrix as shown below. The program should count how many positive numbers, negative numbers and zeros in the matrix. Repeat the above program but let the user now enter the values of the matrix via the keyboard. 1.5 2. -3.1 4.0 5. 9.6 7.7 -8. 1.1 0.1 2.1 -2.

Matrix Operations Write a program that accepts two matrices from the user and store them in two 2-D arrays. Then, the program prompt the user to select the operation he is seeking by entering 1 for matrix addition, 2 for matrix subtraction, and 3 for matrix multiplication. Addition and subtraction are done element by element for two matrices that are having the same size. The multiplication is done for two matrixes where the number of columns in the first matrix is equal to the number of rows in the second one. C = A + B (MXN) = (MXN) + (MXN) C = A - B (MXN) = (MXN) - (MXN) C = A X B (MXN) = (MXL) + (LXN) Multiplication can be done as follows