18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array.

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

Introduction to 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.
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.
Matrix Multiplication To Multiply matrix A by matrix B: Multiply corresponding entries and then add the resulting products (1)(-1)+ (2)(3) Multiply each.
Maths for Computer Graphics
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays A variable: a named position of memory For example: x1, x2, x3……..x10 An array: a collection of variables of the same type that are referenced by.
Two Dimensional Arrays. One dimension Rank 1 Array INTEGER, DIMENSION (3) :: a Row 1 Row 2 Row 3.
Building Java Programs Chapter 7.5
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
LAB-12 2-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
21 April, 2000 CS1001 Lecture 27 Final Examination Review.
Wednesday, July 15, 2015 EQ: What are the similarities and differences between matrices and real numbers ? Warm Up Evaluate each expression for a = -5,
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
Multi-Dimensional Arrays
A First Book of ANSI C Fourth Edition
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
AIM: How do we perform basic matrix operations? DO NOW:  Describe the steps for solving a system of Inequalities  How do you know which region is shaded?
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
WEEK 6 Class Activities Lecturer’s slides.
Slide Copyright © 2009 Pearson Education, Inc. 7.3 Matrices.
How to Multiply Two Matrices. Steps for Matrix Multiplication 1.Determine whether the matrices are compatible. 2.Determine the dimensions of the product.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Matrix Operations.
Sec 4.1 Matrices.
Algebra Matrix Operations. Definition Matrix-A rectangular arrangement of numbers in rows and columns Dimensions- number of rows then columns Entries-
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Table of Contents Matrices - Definition and Notation A matrix is a rectangular array of numbers. Consider the following matrix: Matrix B has 3 rows and.
3.5 Perform Basic Matrix Operations Add Matrices Subtract Matrices Solve Matric equations for x and y.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.
Systems of Equations and Matrices Review of Matrix Properties Mitchell.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
Matrix – is a rectangular arrangement of numbers in rows and columns. Dimensions – Size – m is rows, n is columns. m x n ( row ∙ column) Elements – The.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Add and subtract matrices. Multiply by a matrix scalar.
2.3 MODELING REAL WORLD DATA WITH MATRICES By the end of the section students will be able to add, subtract, and multiply matrices of various sizes. Students.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
13.4 Product of Two Matrices
Multiplying Matrices.
Christmas Packets are due on Friday!!!
Matrix Operations.
Matrix Operations.
Matrix Multiplication
Matrix Operations Monday, August 06, 2018.
Matrix Operations.
CS1100 Computational Engineering
Multiplying Matrices.
WarmUp 2-3 on your calculator or on paper..
Matrices Elements, Adding and Subtracting
4.1 Matrices – Basic Operations
MATRICES MATRIX OPERATIONS.
Multiplying Matrices.
Lets Play with arrays Singh Tripty
3.5 Perform Basic Matrix Operations
What is the dimension of the matrix below?
Multiplying Matrices.
Multiplying Matrices.
MATRICES MATTER!.
Multiplying Matrices.
Presentation transcript:

18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array

18 April, 2000 Quiz 5 -- #1 DO I = 1,5 IF (MOD(I,2) == 0) THEN Number(I) = 2 * I ELSE Number(I) = 2 * I+1 END IF END DO First time in loop: I = 1 MOD(1,2) = 1.NE. 0 so - ELSE Number(1) = 2*1+1=3 Second time in loop: I = 2 MOD(2,2) = 0.EQ. 0 so - THEN Number(2) = 2*2 = 4 Third time in loop: I = 3 MOD(3,2) = 1.NE. 0 so - ELSE Number(1) = 2*3+1=7 Fourth time in loop: I = 4 MOD(4,2) = 0.EQ. 0 so - THEN Number(2) = 2*4 = 8 Fifth time in loop: I = 5 MOD(5,2) = 1.NE. 0 so - ELSE Number(1) = 2*5+1=11

18 April, 2000 Quiz 5 -- #2 I = 0 DO I = I + 1 Number (I) = MIN ( I,3) IF (I >= 5) EXIT END DO Third time in loop: I = = 3 Number (3) = MIN ( 3,3) = 3 (3 >= 5) is.FALSE. Do not EXIT Fourth time in loop: I = = 4 Number (4) = MIN ( 4,3) = 3 (4 >= 5) is.FALSE. Do not EXIT Fifth time in loop: I = = 5 Number (5) = MIN ( 5,3) = 3 (5 >= 5) is.TRUE. EXIT I = 0 First time in loop: I = = 1 Number (1) = MIN ( 1,3) = 1 (1 >= 5) is.FALSE. Do not EXIT Second time in loop: I = Number (2) = MIN ( 2,3) = 2 (2 >= 5) is.FALSE. Do not EXIT

18 April, 2000 Multi-Dimension Arrays Basically the same as one-dimensional –Declared as: REAL, DIMENSION(4, 3, 7) :: Temp or REAL, DIMENSION(1:4, 1:3, 1:7) :: Temp or REAL :: Temp(1:4, 1:3, 1:7) –An element is Temp(1, 3, 2) Multi-Dimensional Array Apps –Tables of data –Matrix processing

18 April, Dimensional Array Declare and dimension INTEGER,DIMENSION(1:9,1:3):: iVar1, iVar2 REAL, DIMENSION(0:25,1:5) :: rVar1, rVar2 Initialize whole array iVar1 = 0 rVar1 = 9.99 Reference elements using subscript iVar2(8,2) = 25 rVar2(19,5) = 17.49

18 April, 2000 Array Processing -- storage Rowwise: Table(1,1) Table(1,2) Table(1,3) Table(2,1) Table(2,2) etc. Columnwise Table(1,1) Table(2,1) Table(3,1) Table(1,2) Table(2,2) etc. Fortran convention is columnwise (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) ? :

18 April, 2000 Matrix Processing m x n matrix addition and subtraction is done element by corresponding element INTEGER :: M(2,3),N(2,3),Add(2,3),Sub(2,3) ! Remember this is done columnwise! READ *, M READ *, N ! Let the computer do the work and handle all ! of the overhead: Add = M + N Sub = M - N PRINT *, Add PRINT *, Sub ! The output isn’t pretty, but it’s right M(1,1) M(2,1) M(1,2) M(2,2) M(1,3) M(2,3) Add(1,1) Add(2,1) Add(1,2) Add(2,2) Add(1,3) Add(2,3)

18 April, 2000 Matrix Addition INTEGER :: M(2,3),N(2,3),Add(2,3),Sub(2,3), I,J ! READ elements of the 2 arrays! DO I = 1,2 DO J = 1,3 READ *, M(I,J) END DO DO I = 1,2 DO J = 1,3 READ *, N(I,J) END DO Input: M(1,1) M(1,2) M(1,3) M(2,1) M(2,2) M(2,3) N(1,1) N(1,2) N(1,3) N(2,1) N(2,2) N(2,3) ! Implied Loop DO I = 1,2 READ *, ((M(I,J), J=1,3) END DO Input: M(1,1) M(1,2) M(1,3) M(2,1) M(2,2) M(2,3)

18 April, 2000 Matrix Add/Subtract (Cont’d) ! Add M and N, Subtract N from M DO I = 1,2 DO J = 1,3 Add(I,J) = M(I,J)+N(I,J) END DO DO I = 1,2 DO J = 1,3 Add(I,J) = M(I,J)-N(I,J) END DO ! Print results using Implied Do-Loop DO I = 1,2 PRINT *, (Add(I,J), J=1,3) END DO DO I = 1,2 PRINT *, (Sub(I,J), J=1,3) END DO I = 1: J=1: Add(1,1) = M(1,1)+N(1,1) J=2: Add(1,2) = M(1,2)+N(1,2) J=3: Add(1,3) = M(1,3)+N(1,3) I = 2: J=1: Add(2,1) = M(2,1)+N(2,1) J=2: Add(2,2) = M(2,2)+N(2,2) J=3: Add(2,3) = M(2,3)+N(2,3) I = 1: Add(1,1) Add(1,2) Add(1,3) I = 2: Add(2,1) Add(2,2) Add(2,3)

18 April, 2000 Matrix Multiplication The number of columns of A must be equal to the number of rows of B to be able to multiply them 22 = 1*4 + 0*6 + 2*9 2 = 1*2 + 0*4 + 2*0 48 = 3*4 + 0*6 + 4*9 6 = 3*2 + 0*4 + 4* *=

18 April, 2000 Intrinsic Array-Processing Subprograms MATMUL(A,B) - returns the matrix product of matrices A and B. MAXVAL(A,D) - returns an array of one fewer dimension than A containing the max values in array A along dimension D. If D is omitted, the max value of the whole array is returned. For example: A= MAXVAL(A,2) = MAXVAL(A,1) = MAXVAL(A) = 9

18 April, 2000 PROGRAM Finding_Row_Maxima_1 ! ! Program to read an array of numbers and to find ! the maximum value in ! each row using the subroutine FindRowMaxima. ! Identifiers used are: ! IntArray : two-dimensional array of numbers ! NumRows : number of rows in IntArray (constant) ! NumColumns : number of columns in IntArray (constant) ! FindRowMaxima : subroutine to find the row maxima ! I, J : subscripts ! ! Input: NumRows, NumColumns, and IntArray ! Output: The maximum value in each row ! (using FindRowMaxima) !

18 April, 2000 IMPLICIT NONE INTEGER, PARAMETER :: NumRows = 4, NumColumns = 5 INTEGER, DIMENSION(NumRows, NumColumns) :: IntArray INTEGER :: I, J PRINT *, "Enter the", NumRows, "x", NumColumns, & "array of integers in a rowwise manner:" READ *, ((IntArray(I,J), J = 1, NumColumns), & I = 1, NumRows) CALL FindRowMaxima(IntArray) CONTAINS Enter the 4 x 5 array of integers in a rowwise manner:

18 April, 2000 !-FindRowMaxima ! Subroutine to find and display the maximum value ! in each row of an array Table of integers. ! Local identifiers used are: ! Rows : number of rows in array Table ! (named constant) ! Cols : number of columns in array Table ! (named constant) ! RowMax : one-dimensional array of row maxima ! I : subscript ! ! Accepts: Array Table ! Output: The maximum value in each row of Table !

18 April, 2000 SUBROUTINE FindRowMaxima(Table) INTEGER, PARAMETER :: Rows = 4, Cols = 5 INTEGER, DIMENSION(Rows, Cols), INTENT(IN) :: Table INTEGER, DIMENSION(Rows) :: RowMax INTEGER :: I ! Find max values along dimension 2 RowMax = MAXVAL(Table, 2) DO I = 1, Rows PRINT 130, I, RowMax(I) 130 FORMAT (1X,"Maximum entry in row", I2, "is", I2) END DO END SUBROUTINE FindRowMaxima END PROGRAM Finding_Row_Maxima_1 Maximum entry in row 1 is 5 Maximum entry in row 2 is 6 Maximum entry in row 3 is 4 Maximum entry in row 4 is 9