Two Dimensional Arrays Rohit Khokher

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

One Dimensional 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
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Two Dimensional Arrays. One dimension Rank 1 Array INTEGER, DIMENSION (3) :: a Row 1 Row 2 Row 3.
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.
Wednesday, July 15, 2015 EQ: What are the similarities and differences between matrices and real numbers ? Warm Up Evaluate each expression for a = -5,
Module 6 Matrices & Applications Chapter 26 Matrices and Applications I.
Matrices The Basics Vocabulary and basic concepts.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
MATRICES MATRIX OPERATIONS. About Matrices  A matrix is a rectangular arrangement of numbers in rows and columns. Rows run horizontally and columns run.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Overview Definitions Basic matrix operations (+, -, x) Determinants and inverses.
Lecture 7 Matrices CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
13.1 Matrices and Their Sums
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Two Dimensional Arrays
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
When data from a table (or tables) needs to be manipulated, easier to deal with info in form of a matrix. Matrices FreshSophJunSen A0342 B0447 C2106 D1322.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
4.5 Inverse of a Square Matrix
Inverse of a Matrix Multiplicative Inverse of a Matrix For a square matrix A, the inverse is written A -1. When A is multiplied by A -1 the result is the.
How to Multiply Two Matrices. Steps for Matrix Multiplication 1.Determine whether the matrices are compatible. 2.Determine the dimensions of the product.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
1.3 Matrices and Matrix Operations. A matrix is a rectangular array of numbers. The numbers in the arry are called the Entries in the matrix. The size.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
MATRIX A set of numbers arranged in rows and columns enclosed in round or square brackets is called a matrix. The order of a matrix gives the number of.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
2.5 – Determinants and Multiplicative Inverses of Matrices.
CS 450: COMPUTER GRAPHICS TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE.
Arrays. Arrays are objects that help us organize large amounts of information.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Section 2.4. Section Summary  Sequences. o Examples: Geometric Progression, Arithmetic Progression  Recurrence Relations o Example: Fibonacci Sequence.
Review Multi-Dimensional Array Searching and Sorting in arrays Inheritance Graphics.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
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.
A very brief introduction to Matrix (Section 2.7) Definitions Some properties Basic matrix operations Zero-One (Boolean) matrices.
2.1 Matrix Operations 2. Matrix Algebra. j -th column i -th row Diagonal entries Diagonal matrix : a square matrix whose nondiagonal entries are zero.
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
Matrices and Matrix Operations
MATRICES.
12-1 Organizing Data Using Matrices
Matrices Rules & Operations.
Discrete Structures – CNS2300
1.5 Matricies.
Programming application CC213
Matrix Operations.
What we’re learning today:
Matrix Multiplication
Matrix Operations.
Dr. Ameria Eldosoky Discrete mathematics
Sequences and Summations
CS1100 Computational Engineering
WarmUp 2-3 on your calculator or on paper..
2. Matrix Algebra 2.1 Matrix Operations.
Section 2.4 Matrices.
Matrix Multiplication
© T Madas.
3.5 Perform Basic Matrix Operations
12.1 Addition of Matrices.
Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as ai,j and elements of B as.
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
Arrays and Matrices Prof. Abdul Hameed.
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

Two Dimensional Arrays Rohit Khokher

Two dimensional Arrays A vector can be represented as an one dimensional array A matrix can be represented as a two dimensional array A[8][10] Columns Rows A[5][6] Array element

The value of sales of three items by four sales person 1 2 3 A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2] 4 A[3][0] A[3][1] A[3][2] Sales Persons

2-D array 2-Day Array Declaration #define ROWS 2 #define COLS 3 1 float A[ROWS ][COLS]; Compile time Initialization int A[ROWS ][COLS]= {0,0,0,1,1,1}; or int A[ROWS ][COLS]= ({0,0,0},{1,1,1});

Compile time initialization 1 int A[ROWS ][COLS]= {{0,0,0}, {1,1,1}}; int A[ ][COLS]= { {0,0,0}, {1,1,1} }; 1 2 int A[ROWS][COLS]= { {0,0}, {2} }; The missing values are initialized to zero automatically

Runtime Initialization for (i=0; i<ROWS; i++) for (j=0; i<COLS; j++) a[i][j]=0; Reading the elements of 2-D array Read data row-wise for (i=0; i<ROWS; i++) for (j=0; j<COLS; j++) scanf (“%d”, &a[i][j]); Priniting the elements of 2-D array for (i=0; i<ROWS; i++) { Print a row for (j=0; j<COLS; j++) printf (“%d”, &a[i][j]); Change line printf (“\n”); }

Matrix manipulation Read a matrix A (4,4) of real numbers and compute: sum of each row entries sum of each column entries sum of the main diagonal entries sum of the secondary diagonal entries

Compute row sum for (i=0; i<ROWS; i++) { R[i]=0; Initialize R[i] for (j=0; j<COLS; j++) R[i]=R[i]+A[i][j]; }

Compute Column sum for (j=0; j<COLS; j++) { C[j]=0; A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[1][3] A[2][0] A[2][1] A[2][2] A[2][3] A[3][0] A[3][1] A[3][2] A[3][3] for (j=0; j<COLS; j++) { C[j]=0; for (i=0; i<ROWS; i++) C[j]=C[j]+A[i][j]; } Initialize C[j] C[0]= A[0][0] + A[1][0] + A[2][0] + A[3][0] C[1]= A[0][1] + A[1][1] + A[2][1] + A[3][1] C[2]= A[0][2] + A[1][2] + A[2][2] + A[3][2] C[3]= A[0][3] + A[1][3] + A[2][3] + A[3][3]

Compute diagonal sum D =0; for (j=0; j<COLS; j++) D= D+ A[j][j]; OR for (i=0; i<ROWS; i++) D=D+A[i][i]; D= A[0][0] + A[1][1] + A[2][2] + A[3][3]

Compute second diagonal sum for (i=0; i<ROWS; i++) D= D+ A[i][COLS-i]; D= A[0][3] + A[1][2] + A[2][1] + A[3][0]

Practice question Write a C program to find the largest element of each row of a 2D array. Write a C program to find the row and column numbers of the smallest element of a 2D array and count the sum of its neighbors. In a 2D array an element A[i][j] may have up to eight neighbors defined as: i-1,j-1 i-1,j i-1, j+1 I,j-1 i,j i,j+1 i+1,j-1 i+1,j i+1,j+1

Add two matrices for (i=0; i<ROWS; i++) for (j=0; j<COLS; j++) C[i][j]=A[i][j]+B[i][j];

Multiplication of two matrices c11=a11 x b11 + a12 x b21 c12=a11 x b12 + a12 x b22 c13=a11 x b13 + a12 x b23 for (i=0; i<ROWSA; i++) c21=a21 x b11 + a22 x b21 for (j=0; j<COLSB; j++) c22=a21 x b12 + a22 x b22 c23=a21 x b13 + a22 x b23 { C[i][j]=0; c31=a31 x b11 + a32 x b21 for (k=0; j<COLSA; j++) C[i][j]= C[i][j]+A[i][k]+B[k][j]; } c22=a31 x b12 + a32 x b22 c33=a31 x b13 + a32 x b23

Practice problem To verify the correctness the matrix multiplication algorithm described before compute:

Practice problem A square 2D array is called a magic square if sums of each row, each column, and both the diagonals of the array are equal. A 3 x 3 magic square is shown below. Read more about magic square at http://en.wikipedia.org/wiki/Magic_square Write a program that reads a 2D array and determines whether the array is a magic square or not.

Project Read about the tic-tac-toe game at http://en.wikipedia.org/wiki/Tic-tac-toe . Find the simplest algorithm that can be implemented in C using 1D and 2D arrays.