Lesson 9 – Matrices By John B. Owen All rights reserved ©2011.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to Programming
Chapter 7: Arrays In this chapter, you will learn about
Lesson 6D – Loops and String Arrays – split process By John B. Owen All rights reserved ©2011.
Lesson 8 – Passing Parameters By John B. Owen All rights reserved ©2011.
OWEN COMPUTER SCIENCE LESSONS
Arrays.
Chapter 10 Introduction to Arrays
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
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.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Manipulating 2D arrays in Java
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Java Unit 9: Arrays Declaring and Processing Arrays.
Exercise problems for students taking the Programming Parallel Computers course. Janusz Kowalik Piotr Arlukowicz Tadeusz Puzniakowski Informatics Institute.
Lists in Python.
Lesson 6C – Loops 3 – Advanced File Processing By John B. Owen All rights reserved ©2011.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
259 Lecture 13 Spring 2013 Advanced Excel Topics – Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
The goal is to give an introduction to the mathematical operations with matrices. A matrix is a 2-dimensional arrangement of (real valued) data. The data.
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.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
 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)
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
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.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Linear Algebra Review.
EGR 2261 Unit 10 Two-dimensional Arrays
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
2-D Lists Taken from notes by Dr. Neil Moore
JavaScript: Functions.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Lecture 13: Two-Dimensional Arrays
Building Java Programs
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.
2-D Lists Taken from notes by Dr. Neil Moore
Presentation transcript:

Lesson 9 – Matrices By John B. Owen All rights reserved ©2011

Objectives Matrix definition Matrix example Nested loop output Arrays of other data types Creating new matrices Filling matrices Loading new arrays Matrix processing examples Matrix processing examples Lesson Summary / Labs Acknowledgement of use Table of Contents Lesson 9 – Matrices2

In several previous lessons, you learned about arrays and how they can handle multiple instances of the same data type. The arrays you learned about were one- dimensional, and could represent a single row of elements. In this lesson you will learn about matrices (plural for matrix) which can represent several rows of data in a 2-dimensional fashion. Objectives Lesson 9 – Matrices3

A matrix is an array of arrays, a one- dimensional array of one-dimensional arrays. A matrix has rows and columns, like a spreadsheet. Often a matrix is referred to as a 2D array (two-dimensional array) It is even possible to have 3D, 4D, or 5D arrays…whatever you need! What is a matrix? Lesson 9 – Matrices4

Matrix example Lesson 9 – Matrices5 Here you see a very simple example of a 3X3 matrix – 3 rows, and 3 columns in each row.

Matrix example Lesson 9 – Matrices6 First notice how the array is created. The data type is followed by TWO pairs of square brackets, then the identifier, then the elements.

Matrix example Lesson 9 – Matrices7 The elements are arranged inside nested brackets. The outside pair of brackets encompasses the entire grid.

Matrix example Lesson 9 – Matrices8 The inside pairs of brackets contain each row of the grid, with commas separating each row.

Matrix example Lesson 9 – Matrices9 The values inside the inner sets of brackets are the column elements of each row, also separated by commas.

Matrix example Lesson 9 – Matrices10 Finally, the contents of the grid are output using TWO index values… The first index represents the row The second index represents the column.

Output using nested loops Lesson 9 – Matrices11 Just as arrays use loops to process, matrices often use nested loops, as seen below. The outside loop is the row loop, and the inside one the column loop

grid.length vs grid[r].length Lesson 9 – Matrices12 The length method measures the length of an array, pure and simple. The grid.length command below measures the length of the grid array, which in reality counts how many rows are in the matrix, in this case 3.

grid.length vs grid[r].length Lesson 9 – Matrices13 The grid[r].length command measures the length of the each row in the grid array, or the number of column elements in each row, again 3 in each case here.

Output with for each nested loops Lesson 9 – Matrices14 You can use the for each loops in a nested fashion to output this matrix. Check it out.

Output with for each nested loops Lesson 9 – Matrices15 Notice carefully that the data type for the first loop is an array, and for the second loop the data type itself.

double matrix Lesson 9 – Matrices16 Here is an example of a matrix with type double. Notice carefully that the number of columns in each row are different, which is possible for any data type, although most of the time the rows are all the same length.

char matrix Lesson 9 – Matrices17 Here is a char matrix. Notice the only changes involve the data type. The matrix construction and output processes are identical.

String matrix Lesson 9 – Matrices18 Here is a String matrix.

boolean matrix Lesson 9 – Matrices19 You can even have a boolean matrix if the situation requires it.

Creating new matrices Lesson 9 – Matrices20 The new process you learned with arrays also works with matrices, as you can see below. Default values are automatically loaded into each new array

Filling matrices Lesson 9 – Matrices21 Although there is no direct way to fill matrices as there was with arrays, a loop can be used to fill each row, which IS an array, using the Arrays.fill method.

Filling matrices Lesson 9 – Matrices22 You could also fill each row with a different value, like the row number itself.

Loading new arrays, example #1 Lesson 9 – Matrices23 To load an array with outside data, a keyboard input process can be used, as shown below.

Loading new arrays, example #2 Lesson 9 – Matrices24 Here is an example of data file input. The first two values indicate the number of rows and columns, followed by the matrix elements.

Matrix processing – Example #1 Lesson 9 – Matrices25 Now that we know how to construct and load data into matrices, lets look at some classic processing techniques. This one totals all the values in the matrix, using a method with a traditional nested for loop process.

Matrix processing – Example #2 Lesson 9 – Matrices26 This example also shows a totaling method using the nested for each loop process

Matrix processing – Example #3 Lesson 9 – Matrices27 Here is an unusual example in that the structure shown is a 1D array of Strings. However, since a String is in a sense an array of characters, this actually a 2D array of characters. The first two outputs are a review of the two types of loops that can be used to output an array. The next slide shows the 2D nature of the structure.

Matrix processing – Example #3 Lesson 9 – Matrices28 The two- dimensional character array becomes evident in this example, with each word treated as a row of characters, separated in each output… …with an interesting twist at the end…check it out!

In this lesson, you learned about constructing, loading, and processing 2-dimensional arrays. You used the nested loop processes learned earlier. Now it is time to practice with several examples. Lesson Summary Lesson 9 – Matrices29

MyMatrices will be the name of this series of labs. As you did before, create a separate folder and file called MyMatrices and do your work there. Labs Lesson 9 – Matrices30

WAP to manage two grids. Read 12 numbers from a file ( lab9A.in ) into a 4 X 3 TWO DIMENSIONAL array, or matrix (4 rows, 3 columns). Call this gridA. Create another same size matrix gridB, divide each element in gridA by five and store the result in the gridB. Output both grids as shown. A suggested solution is shown. Lab 9A Lesson 9 – Matrices31

Lab 9B Lesson 9 – Matrices32 WAP to load your three initials into three different matrixes of characters, each measuring 5 rows by 8 columns. You must use loops at every possible opportunity. The basic code for the letter J might look like this. The required final output is shown on the next slide.

Lab 9B(cont) Lesson 9 – Matrices33 The final output shows all three letters centered with a metric line above the first initial. The output for the initials JBO is shown.

Lab 9C Lesson 9 – Matrices34 Adapt the work you did for Lab 9B to output the three initials in horizontal fashion, as shown below, again with the metric line to verify centering.

Lab 9D Lesson 9 – Matrices35 WAP to read four numbers (A, B, C, D) from a data file ( lab9d.in ). Use the numbers to create an A X B matrix stored in a two-dimensional array such that each element is the sum of its index values. For the numbers , the matrix will be a 5 X 8 grid. In the matrix, each element will be the sum of its index values, so the value in row 0, column 0 will be 0+0, or 0. Row 4, column 3 will contain the number 7 (4+3). Once the array is constructed and loaded, generate the four outputs shown below. The values of C and D ( 4 and 5 in the sample) are used for reports 2 and 3. (continued on next slide)

Lab 9D(cont) Lesson 9 – Matrices36 Output 1: print the A by B matrix contents Output 2: print the elements in row C Output 3: print the elements in col D Output 4: calculate and print the total of all the elements in the matrix

Lab 9E Lesson 9 – Matrices37 WAP to create a 4 X 2 matrix of Strings. Read from a data file (lab9E.in)into the array the first and last name of several famous people and produce each of the following outputs. Output 1: print the list of names (first name first) Ouput 2: print the list of names (last name first) Output 3: print the first initial and last name of each person Output 4: see the output to figure it out…hint: triple-nested loop!

Lab 9F Lesson 9 – Matrices38 WAP to input several Strings from a data file ( lab9F.in ) (use the while hasNext file input technique), create an X-diagonal formation in a character matrix for each String, and output as shown here.

Lab 9G Lesson 9 – Matrices39 WAP to load an ASCII picture (max size 25 rows, 40 columns) into a character matrix, and output the picture 4 different ways: normal, inverted, reversed, both inverted and reversed.

Lab 9H Lesson 9 – Matrices40 And now for the classic matrix algorithm…multiplying matrices! This is a process usually first learned in Algebra II, involving a very specific set of rules. It is easily accomplished on the graphing calculator because someone wrote a program to do that. You will now write that program! If you dont know the process, ask your algebra teacher, or look it up! Your job is to write a method that receives two integer matrices as parameters, and returns a new integer matrix containing the product of the two parameter matrices. It will be guaranteed in the precondition to the method that the two matrices passed as parameters will be compatible as far as the size requirements (columns of the first must match rows of the second).

Lab 9H (cont) Lesson 9 – Matrices41 Due to the fairly difficult nature of this algorithm, most of the solution is shown below and on the next slide. All that is left for you to do is to figure out the triple nested loop process required to actually do the math! Good luck!

Lab 9H (cont) Lesson 9 – Matrices42 Here is most of the main method.

You now know how to work with 2D arrays. Lesson 10 will begin the OOP adventure, the journey into the world of Object Oriented Programming. CONGRATULATIONS! Lesson 9 – Matrices43

Please copy, paste, and customize the brief acknowledgement shown below in an to indicating that you used this lesson. Feel free to include any comments you wish to offer, positive or negative. I, (your name), from (educational institution) in (city, state, country) used Lesson 9 on (date), understand, respect and honor all of the rights claimed by the author, Mr. John Owen, and offer the following comments..._______________. THANK YOU! Acknowledgement of use Lesson 9 – Matrices44