ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.

Slides:



Advertisements
Similar presentations
Matrix.
Advertisements

2.3 Modeling Real World Data with Matrices
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 CS 162 Introduction to Computer Science Chapter 7 Matrix Manipulation Herbert G. Mayer, PSU Status 9/21/2014.
Arrays and Matrices CSE, POSTECH. 2 2 Introduction Data is often available in tabular form Tabular data is often represented in arrays Matrix is an example.
Maths for Computer Graphics
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
CS Data Structures Chapter 2 Arrays and Structures.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
ECON 1150 Matrix Operations Special Matrices
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
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.
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?
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.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Lesson 11-1 Matrix Basics and Augmented Matrices Objective: To learn to solve systems of linear equation using matrices.
Class Opener:. Identifying Matrices Student Check:
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
Working with Arrays in MATLAB
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 53 Generic Algorithms Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
1 Graphics CSCI 343, Fall 2015 Lecture 10 Coordinate Transformations.
8.2 Operations With Matrices
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.
Data Structures - Part I CS 215 Lecture 7. Motivation  Real programs use abstractions like lists, trees, stacks, and queues.  The data associated with.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Arrays.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
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.
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.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
Ch. 7 – Matrices and Systems of Equations 7.5 – Operations with Matrices.
13.4 Product of Two Matrices
Dynamic Array Multidimensional Array Matric Operation with Array
Matrix Multiplication
Basic notes on pointers in C
Two Dimensional Arrays
Multiplying Matrices.
Matrix Algebra.
CS111 Computer Programming
Multiplying Matrices.
ECE 103 Engineering Programming Chapter 19 Nested Loops
Matrix Algebra.
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
Multi-Dimensional Arrays
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
3.6 Multiply Matrices.
1.8 Matrices.
1.8 Matrices.
Multiplying Matrices.
Arrays and Matrices Prof. Abdul Hameed.
Multiplying Matrices.
Multiplying Matrices.
Presentation transcript:

ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

1 Arrays (Multi-dimensional) A multi-dimensional array can be thought of as an array whose elements are other arrays 2-D arrays are useful for storing a table of values (e.g., a matrix)  1 st dimension is “rows”, 2 nd dimension is “columns” 3-D arrays are often used to hold 3-D coordinates, such as in graphics applications Higher dimensions are useful for tracking multiple variables in an N-dimensional space

2 Declaration: datatype array_name[dim1][dim2][dim3]…[dimN]; The total number of elements allocated is equal to dim1  dim2  dim3  …  dimN. Example: /* 12 elements, indices 0:11 */ float x[12]; /* 12*4=48 elements, indices 0:11, 0:3 */ float y[12][4]; /* 12*4*5=240 elements, 0:11, 0:3, 0:4 */ float z[12][4][5]; // # elements? How many bytes?

3 Example: /* NUM_STUDENTS*NUM_HW elements */ /* indices 0:(NUM_STUDENTS-1), 0:(NUM_HW-1)*/ int HW_grades[NUM_STUDENTS][NUM_HW]; A multi-dimensional array can be declared and initialized at the same time: Example: int nums[2][3] = { {2, 4, 6},{-9,-7,-5} }; int A[3][3] = {{1, 0, 0}, {1, 1, 0}, {0, 0, 1} }; float data[5][8] = {{0.0}}; /* All zero */

4 Memory for an array is allocated as a consecutive, linear (1-D) block of cells In C, array elements are stored in “row major order” Internally, a mapping equation translates array indices to a linear offset  1-D: A[size] → A[i], offset = i  2-D: M[size1][size2] → M[i][j], offset = i*size2 + j

5 Example: 1-D integer array (32-bit) A[0] A[1] A[2] int A[3]; A[2] : 1-D offset is 2 If the array’s base address is Base, then A[2] is stored at address Base + 2*(4 bytes) = Base + 8

6 Example: 2-D integer array (32-bit) M[0][0]M[0][1] M[1][0]M[1][1] M[2][0]M[2][1] int M[3][2]; offset = row# * size2 + col# A[2][1] : 1-D offset is 2*2+1=5 If the array’s base address is Base, then A[2][1] is stored at address Base + 5*(4 bytes) = Base M[0][0] M[0][1] M[1][0] M[1][1] M[2][0] M[2][1]

7 Example: Calculate the matrix sum C = A+B

8 FOR each row x in C FOR each column y in C C xy = A xy + B xy END FOR 2×32×3 2×32×3 2×32×3

9 /* This code fragment adds two matrices */ #define NR 2 #define NC 3 int A[NR][NC] = { { 1, 3, 2 }, { 4, -2, 4} }; int B[NR][NC] = { { -3, 5, 2 }, { 0, 7, 6} }; int C[NR][NC]; // leave uninitialized int x, y; for( x = 0; x < NR; x++ ) { /* Add matrices */ for( y = 0; y < NC; y++ ) { C[x][y] = A[x][y] + B[x][y]; } //end for for( x = 0; x < NR; x++ ) { /* Display result matrix */ for( y = 0; y < NC; y++ ) { printf( "%2d ", C[x][y] ); } //end for printf( "\n” ); } //end for

10 Example: Calculate the matrix product C = AB.

11 Given: A is size m 1 ×n 1, B is size m 2 ×n 2, and n 1 =m 2. Let C = AB be size m 1 ×n 2 Element C xy stored at row x and column y in C is the dot product of row A x and column B y. Procedure: FOR each row A x FOR each column B y C xy = A x ·B y END FOR

12 2×32×3 3×23×2 2×22×2 1 st row of A, 1 st column of B 1 st row of A, 2 nd column of B 2 nd row of A, 1 st column of B 2 nd row of A, 2 nd column of B

13 /* This code fragment multiplies two matrices */ #define NR1 2 #define NC1 3 #define NR2 3 #define NC2 2 int A[NR1][NC1] = { {1,3,2},{4,-2,4}}; int B[NR2][NC2] = { {4,-1},{3,2},{5,3}}; int C[NR1][NC2]; int m1=NR1, n1=NC1, m2=NR2, n2=NC2; int x, y, k; for (x = 0; x < m1; x++) /* row */ for (y = 0; y < n2; y++) /* column */ { C[x][y] = 0; /* Initialize summation */ for (k = 0; k < n1; k++) /* Dot product */ C[x][y] += (A[x][k] * B[k][y]); }