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.

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

Introduction to arrays
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
MATLAB ME1107 Y Yan Reference: MATLAB for Engineers by Holly Moore (Pearson Prentice Hall)
18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array.
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
Arrays.
Arrays. What is an Array? Similar to a Matrix. Collection of data that needs similar processing. Example: Transpose of a matrix.
“No one can be told what the matrix is…
Section 8.1 – Systems of Linear Equations
4.2 Adding and Subtracting Matrices 4.3 Matrix Multiplication
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
 Row and Reduced Row Echelon  Elementary Matrices.
Matrices King Saud University. If m and n are positive integers, then an m  n matrix is a rectangular array in which each entry a ij of the matrix is.
Multi-Dimensional Arrays
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 5 Systems and Matrices Copyright © 2013, 2009, 2005 Pearson Education, Inc.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Matrix Arithmetic. A matrix M is an array of cell entries (m row,column ) and it must have rectangular dimensions (Rows x Columns). Example: 3x x.
13.1 Matrices and Their Sums
If A and B are both m × n matrices then the sum of A and B, denoted A + B, is a matrix obtained by adding corresponding elements of A and B. add these.
Class Opener:. Identifying Matrices Student Check:
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
Notes 7.2 – Matrices I. Matrices A.) Def. – A rectangular array of numbers. An m x n matrix is a matrix consisting of m rows and n columns. The element.
Matrix Algebra Section 7.2. Review of order of matrices 2 rows, 3 columns Order is determined by: (# of rows) x (# of columns)
8.2 Operations With Matrices
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
Matrix Operations.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UNIT 10 Multidimensional Arrays.
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-
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
3.6 Multiplying Matrices Homework 3-17odd and odd.
3.5 Perform Basic Matrix Operations Add Matrices Subtract Matrices Solve Matric equations for x and y.
Arrays. Arrays are objects that help us organize large amounts of information.
Matrix Algebra Definitions Operations Matrix algebra is a means of making calculations upon arrays of numbers (or data). Most data sets are matrix-type.
The Algebra of Matrices Matrix: An array of numbers. Matrix Name (Capital Letter) Matrix Size (row x column) Columns Rows Elements: a rc a 22 = 4.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
Arrays. 1D Array Representation In C++ 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
Consultation Hours. Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wednesday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)
Ch. 12 Vocabulary 1.) matrix 2.) element 3.) scalar 4.) scalar multiplication.
A very brief introduction to Matrix (Section 2.7) Definitions Some properties Basic matrix operations Zero-One (Boolean) matrices.
13.4 Product of Two Matrices
Properties and Applications of Matrices
12-1 Organizing Data Using Matrices
Matrix Operations.
Matrix Multiplication
Matrix Operations Monday, August 06, 2018.
Matrix Operations.
Multiplying Matrices.
WarmUp 2-3 on your calculator or on paper..
Midterm Review Programming in Fortran
MATRICES MATRIX OPERATIONS.
Objectives Multiply two matrices.
Multiplying Matrices.
3.6 Multiply Matrices.
1.8 Matrices.
1.8 Matrices.
Multiplying Matrices.
Multiplying Matrices.
Multiplying Matrices.
Presentation transcript:

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 a common name. A specific element of an array can be accessed by using a subscript. For example: x(1), x(2), x(3)……x(10) What is the main difference? Subscript in an array is a number. It is easier to get access to the ith element of an array x(i) than to get access to xi in a program. When an array is declared, a block of memory is reserved for that array.

Declaration of Arrays Use same keywords as to declare variables: real x(10)! x(1)……x(10) real x(1:10)! x(1)……x(10) real x(0:10)! x(0)……x(10) integer M(10,10)! 100 elements integer N(3,3,3)! 27 elements Or use DIMENSION to declare: real x, y dimension x(10), y(10)

Arrays Input/Output real x(10), y(10) real pi do i = 1, 10 read(*,*) x(i)! Input 10 angles in degrees end do pi = 4.0*ATAN(1.0) do i = 1, 10 y(i) = x(i)*pi/180.0! Calculate radians end do do i = 1, 10 write(*,*) y(i)! Output 10 angles in radians end

Finding the maximum value real x(10) do i = 1, 10 read(*,*) x(i)! Temperatures of 10 consecutive days end do max_value = x(1) max_index = 1 Do i = 1, 10 If (x(i)>max_value max_value = x(i) max_index=i loop false true exit

Finding the maximum value real x(10) do i = 1, 10 read(*,*) x(i)! Temperatures of 10 consecutive days end do max_value = x(1) max_index = 1 do i = 1, 10 if (x(i) > max_value) then max_value = x(i) max_index = i end if end do print *,‘The maximum temperature of’, max_value, ‘is on day’, max_index Input values: 25, 26, 24, 29, 23.5, 20, 24.5, 28.5, 26, 25 What is the output? The maximum temperature of 29 is on day 4

Counting the number of days (>25˚C) real x(10), integer day(10) do i = 1, 10 read(*,*) x(i)! Temperatures of 10 consecutive days end do N = 0 Do i = 1,10 X(i) > 25 N = N +1 day(N)=i loop false trueexit

Counting IF….. real x(10), integer day(10) do i = 1, 10 read(*,*) x(i)! Temperatures of 10 consecutive days end do N = 0! Initializing the counter do i = 1, 10 if (x(i) > 25) then! Counting temperature >25 N = N + 1 day(N)=i end if end do print *,‘The number of days of temperature higher than 25 C is’, N print *, ‘ Temperature higher than 25 C on day(s):’ do i = 1, N print *, day(i) end do Input values: 25, 26, 24, 29, 23.5, 20, 24.5, 28.5, 26, 25 What is the output? The number of days of temperature higher than 25 C is 4 Temperature higher than 25 C on day(s):

Sorting Data (bubble sort) do i = 1, 10 read(*,*) x(i) end do II = 1: x(1) is compared with x(1), x(2) …. x(10) x(1) has the highest value of all II = 2: x(2) is compared with x(2), x(3)….x(10) x(2) has the highest value among x(2)….x(10) ……. II = 9 x(9) is compared with x(9) and x(10) x(9) has the higher value II = 10 x(10) is compared with x(10) nothing changes. Do ii = 1, 10 Do jj = ii, 10 If x(ii)>x(jj) ! swap temp=x(ii) x(ii)=x(jj) x(jj)=temp loop exit true false

Sorting Data integer N real x(10) N=10 do i = 1, N read(*,*) x(i) end do DO 110 II=1,N DO 110 JJ=II+1,N IF (X(II).GE.X(JJ)) GO TO 110 TEMP=X(II) X(II)=X(JJ) X(JJ)=TEMP 110CONTINUE do i = 1, N write(*,*) x(i) end II = 1: x(1) is compared with x(1), x(2) …. x(10) x(1) has the highest value of all II = 2: x(2) is compared with x(2), x(3)….x(10) x(2) has the highest value among x(2)….x(10) ……. II = 9 x(9) is compared with x(9) and x(10) x(9) has the higher value II = 10 x(10) is compared with x(10) nothing changes.

Matrix Operations To read a 5x5 matrix REAL x(5,5) !declare x as a 2D array Do 10 I=1,5 Do 10 J=1,5 Read(*,*) x(I,J) ! read 5x5=25 data from keyboard 10continue note: The program will wait for 25 numbers to be entered via keyboard. Q: Which element of the array will have the first number, the 11 th number and the 20 th number entered from the keyboard? Ans: x(1,1) – 1 st x(3,1) – 11 th x(4,5) – 20 th

Matrix Operations To display a 5x5 matrix with 5 rows and 5 columns REAL x(5,5) Do 10 I=1,5 Write(*, 1000) (x(I,J), J=1,5)! write 5 numbers on one row 10 continue 1000format(5f8.2) (1,1) (1,2)……(1,5) (2,1) (2,2)……(2,5) …… (5,1) (5,2)……(5,5)

Given two matrices A = (a ij ) and B = (b ij ) we can only find AB if the number of columns of A is the same as the number of rows of B. Suppose that A is m-by-n and B is n-by-p, then C = AB is an m-by-p matrix where the elements of C, c ij, are given by c ij = a i1 b 1j + a i2 b 2j a in b nj = i.e. To get the ij element of C, multiply the elements of row i from A with the corresponding elements of column j from B and add. Using Fortran arrays for A,B and C: Matrix Multiplication

Result of multiplication of two 5x5 matrices will be a 5x5 matrix. REAL a(5,5), b(5,5), c(5,5) ! z is multiplication of x and y Do 10 I=1,5 Do 10 J=1,5 c(I,J)=0 Do 20 K=1,5 c(I,J)=c(I,J)+a(I,K)*b(K,J) 20 continue 10 continue Matrix Multiplication i = 1,2,3,4,5 j = 1,2,3,4,5