2-D Array.

Slides:



Advertisements
Similar presentations
Maths for Computer Graphics
Advertisements

1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
Matrices. Outline What is a matrix? Size of matrices Addition of matrices Scalar multiplication Matrices multiplication.
Multiple-Subscripted Array
Exercise 5.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Matrices Jordi Cortadella Department of Computer Science.
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.
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.
Exposure C++ Chapter XXI C++ Data Structures, the 2D Array apmatrix Implementation.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CS 450: COMPUTER GRAPHICS TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
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.
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.
Matrix Algebra Definitions Operations Matrix algebra is a means of making calculations upon arrays of numbers (or data). Most data sets are matrix-type.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
Introduction to Programming
Lecture 18: Nested Loops and Two-Dimensional Arrays
13.4 Product of Two Matrices
Matrices and Matrix Operations
Properties and Applications of Matrices
Matrices Rules & Operations.
Chapter 7 Matrix Mathematics
1.5 Matricies.
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Two Dimensional Array Mr. Jacobs.
Dynamic Array Multidimensional Array Matric Operation with Array
Programming Fundamental
Matrix Multiplication
MULTI-DIMENSIONAL ARRAY
Chapter 7: Array.
Multi-dimensional Array
Computer Graphics Matrix
C++ Arrays.
Two Dimensional Arrays
CS1100 Computational Engineering
Matrix Algebra.
Engineering Problem Solving with C++, Etter/Ingber
Introduction to Programming
Chapter 13 Vector of Vectors (2D Arrays)
Counting Loops.
Section 2.4 Matrices.
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
CS100: Discrete structures
Lecture 13: Two-Dimensional Arrays
Arrays of Two-Dimensions
Matrix Algebra.
CHAPTER 2 Arrays and Vectors.
Lets Play with arrays Singh Tripty
Multi-Dimensional Arrays
CHAPTER 2 Arrays and Vectors.
3.6 Multiply Matrices.
Rayat Shikshan Sanstha’s S.M.Joshi College, Hadapsar -28
Arrays and Matrices.
Arrays and Matrices Prof. Abdul Hameed.
ICS103: Programming in C Searching, Sorting, 2D Arrays
Programming Fundamental
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

2-D Array

What is a matrix? A matrix is a collection of numbers represent in a tabular format (with rows and columns). Matrices have many uses including encryption, computer graphics, and computer animation

Examples of Matrices

Matrices (2D-array) . A matrix is defined using a type declaration statement. datatype array_name[row_size][column_size]; int matrix[3][4]; 4 1 2 -1 3 4 1 2 -1 3 Row 0 Row 1 Row 2 Column 3 Column 0 Column 1 Column 2 in memory

Accessing Array Elements int matrix[3][4]; matrix has 12 integer elements matrix[0][0] element in first row, first column matrix[2][3] element in last row, last column matrix is the address of the first element matrix[1] is the address of the Row 1 matrix[1] is a one dimensional array (Row 1)

Initialization int x[4][4] = { {2, 3, 7, 2}, {7, 4, 5, 9}, {5, 1, 6, -3}, {2, 5, -1, 3}}; int x[][4] = { {2, 3, 7, 2},

Initialization int i, j, matrix[3][4]; for (i=0; i<3; i++) for (j=0; j<4; j++) matrix[i][j] = i; matrix[i][j] = j; j 0 1 2 3 1 2 i j 0 1 2 3 1 2 i 1 2 1 2 3

Exercise Write the nested loop to initialize a 2D array as follow int i, j, x[4][3]; for(i=0; i<4; i++) for(j=0; j<3; j++) x[i][j] = i+j; 1 2 3 4 5

void input(int mat[][10], int n) { for(int r=0; r<n; r++) //Square matrix have equal no of rows and column where as Non-Square Matrix have rows and column are not equal //Input Element in 2D Array (matrix) //Square matrix void input(int mat[][10], int n) { for(int r=0; r<n; r++) for(int c=0; c<n; c++) cout<<"Input values ? "; cin>>mat[r][c]; } //Non-square matrix void input(int mat[][10], int n) { for(int r=0; r<n; r++) for(int c=0; c<n; c++) cout<<"Input values ? "; cin>>mat[r][c]; }

void display(int mat[][10], int n) { for(int r=0; r<n; r++) // Display Element in 2D Array (matrix) void display(int mat[][10], int n) { for(int r=0; r<n; r++) for(int c=0; c<n; c++) printf("%5i", mat[r][c]); //Or cout<<setw(5)<<mat[r][c]; Or cout<<'\t'<<mat[r][c]; cout<<endl; } void display(int mat[][10], int nor, int noc) { for(int r=0; r<nor; r++) for(int c=0; c<noc; c++) printf("%5i", mat[r][c]); //Or cout<<setw(5)<<mat[r][c]; Or cout<<'\t'<<mat[r][c]; cout<<endl; }

Max in 2D Find the maximum of int matrix[3][4] int max = matrix[0][0]; 0 1 2 3 int max = matrix[0][0]; for (i=0; i<3; i++) for (j=0; j<4; j++) if (matrix[i][j] > max) max = matrix[i][j]; 1 2 -1 2 4 3 1 -1 3 1 2

Find a value in 2D int count = 0; Find the number of times x appears in int matrix[3][4] int count = 0; for (i=0; i<3; i++) for (j=0; j<4; j++) if (matrix[i][j] == x) count = count + 1; 0 1 2 3 1 2 -1 2 4 3 1 -1 3 1 2

Adding Matrices Rule #1 : You can only add matrices that are the same size Rule #2: Add corresponding locations in the two matrices to create a new matrix with the same size

Matrix sum Compute the addition of two matrices + = 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 1 2 3 -1 3 1 3 3 3 + -1 2 4 3 1 4 2 = 6 6 3 1 1 1 -1 3 1 2 1 1 3 2 4 4 2 2 2

void add(int mata[][10], int matb[][10], int matc[][10], int n) { // Adding Elements of 2 Matrix in 3rd Array (matrix) void add(int mata[][10], int matb[][10], int matc[][10], int n) { for(int r=0; r<n; r++) for(int c=0; c<n; c++) matc[r][c]=mata[r][c]+matb[r][c]; } void add(int mata[][10], int matb[][10], int matc[][10], int nor, int noc) { for(int r=0; r<nor; r++) for(int c=0; c<noc; c++) matc[r][c]=mata[r][c]+matb[r][c]; }

The multiplication of matrices is easier shown than put into words The multiplication of matrices is easier shown than put into words. You multiply the rows of the first matrix with the columns of the second adding products Find AB First we multiply across the first row and down the first column adding products. We put the answer in the first row, first column of the answer.

Find AB Notice the sizes of A and B and the size of the product AB. Now we multiply across the second row and down the second column and we’ll put the answer in the second row, second column. Now we multiply across the first row and down the second column and we’ll put the answer in the first row, second column. Now we multiply across the second row and down the first column and we’ll put the answer in the second row, first column. We multiplied across first row and down first column so we put the answer in the first row, first column.

Matrix multiplication double a[3][2], b[2][4], c[3][4]; Find c = a * b; 3 4 5 2 1 6 22 29 45 35 18 40 47 21 26 33 43 49 2 3 7 1 4 5 6 8 = x 3*2 + 4*4=22 3*3 + 4*5=29 3*7 + 4*6=45 3*1 + 4*8=35 5*2 + 2*4=18 5*3 + 2*5=40 5*7 + 2*6=47 5*1 + 2*8=21 1*2 + 6*4=26 1*3 + 6*5=33 1*7 + 6*6=43 1*1 + 6*8=49

To multiply matrices A and B look at their dimensions MUST BE SAME SIZE OF PRODUCT If the number of columns of A does not equal the number of rows of B then the product AB is undefined.

void add(int mata[][10], int matb[][10], int matc[][10], int n) { // Multiplying Elements of 2 Matrix in 3rd Array (matrix) void add(int mata[][10], int matb[][10], int matc[][10], int n) { for(int r=0; r<n; r++) for(int c=0; c<n; c++) matc[r][c]=mata[r][c]*matb[r][c]; } void add(int mata[][10], int matb[][10], int matc[][10], int nor, int noc) { for(int r=0; r<nor; r++) for(int c=0; c<noc; c++) matc[r][c]+=mata[r][c]*matb[r][c]; }

int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; void main() { int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; cout << "Enter rows and columns for first matrix: "; cin >> r1 >> c1; cout << "Enter rows and columns for second matrix: "; cin >> r2 >> c2; /* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */ while (c1!=r2) { cout << "Error! column of first matrix not equal to row of second."; cout << "Enter rows and columns for first matrix: "; cin >> r1 >> c1; cout << "Enter rows and columns for second matrix: "; cin >> r2 >> c2; }

* STORING elements of first matrix. */ cout << endl << "Enter elements of matrix 1:" << endl; for(i=0; i<r1; ++i) for(j=0; j<c1; ++j) { cout << "Enter element a" << i+1 << j+1 << " : "; cin >> a[i][j]; } /* STORING elements of second matrix. */ cout << endl << "Enter elements of matrix 2:" << endl; for(i=0; i<r2; ++i) for(j=0; j<c2; ++j) { cout << "Enter element b" << i+1 << j+1 << " : "; cin >> b[i][j]; } /* Initializing elements of matrix mult to 0.*/ for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) { mult[i][j]=0; }

/* Multiplying matrix a and b and STORING in array mult. */ for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) for(k=0; k<c1; ++k) { mult[i][j]+= a[i][k]*b[k][j]; } /* Displaying the multiplication of two matrix. */ cout << endl << "Output Matrix: " << endl; for(i=0; i<r1; ++i){ for(j=0; j<c2; ++j) { cout << mult[i][j] << " " ;} cout << endl; } getch(); } C++ Code for Matrix Multiplication

Transpose B A 1 4 5 2 3 6 1 5 3 4 2 6

void transpose(int mata[][10], int matb[][10], int n) { Transpose of Matrix void transpose(int mata[][10], int matb[][10], int n) { for(int r=0; r<n; r++) for(int c=0; c<n; c++) matb[r][c]=mata[c][r]; } void transpose(int mata[][10], int matb[][10], int nor, int noc) { for(int r=0; r<nor; r++) for(int c=0; c<noc; c++) matb[r][c]=mata[c][r]; }

void rowsum(int mat[][10], int n) { for(int r=0; r<n; r++) Row wise sum of Matrix Column wise sum of Matrix void rowsum(int mat[][10], int n) { for(int r=0; r<n; r++) int sum=0; for(int c=0; c<n; c++) sum+=mat[r][c]; cout<<"Sum of "<<(r+1)<<" row = "<<sum<<endl; } void colsum(int mat[][10], int n) { for(int c=0; c<n; c++) int sum=0; for(int r=0; r<n; r++) sum+=mat[r][c]; cout<<"Sum of "<<(c+1)<< " column = "<<sum<<endl; }

void diagonalsum(int mat[][10], int n) { int sum1=0, sum2=0; Displaying Diagonal of Matrix Sum of Diagonal of Matrix Values Of 2D Array [ Matrix ] : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 Values Of 2D Array [ Matrix ] : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 void diagonalsum(int mat[][10], int n) { int sum1=0, sum2=0; for(int k=0; k<n; k++) sum1+=mat[k][k]; sum2+=mat[k][n-k-1]; } cout<<"Sum of the main diagonal = "<<sum1<<endl; cout<<"Sum of the other diagonal = "<<sum2<<endl; void diagonal(int mat[][10], int n) { for(int k=0; k<n; k++) cout<<mat[k][k]; cout<<mat[k][n-k-1]; }

cout<<"\nUpper Right Triangle Of Matrix "; for(i=0; i<r; i++) Values Of 2D Array [ Matrix ] : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 if(r==c) { cout<<"\nUpper Right Triangle Of Matrix "; for(i=0; i<r; i++) for(j=0; j<c; j++) if(j>=i) cout<<a[i][j]<<"\t"; else cout<<"\t"; } cout<<endl; Upper Right Triangle Of Matrix 1 2 3 4 6 7 8 2 3 7

cout<<"\nUpper Right Triangle Of Matrix "; for(i=0; i<r; i++) Lower Right Triangle Of Matrix Values Of 2D Array [ Matrix ] : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 if(r==c) { cout<<"\nUpper Right Triangle Of Matrix "; for(i=0; i<r; i++) for(j=0; j<c; j++) if(j<=i) cout<<a[i][j]<<"\t"; else cout<<"\t"; } cout<<endl; Lower Left Triangle Of Matrix 1 5 6 9 1 2 4 5 6 7

//Swap the first row and last row of a matrix Values Of 2D Array [ Matrix ] : 4 5 6 7 5 6 7 8 9 1 2 3 1 2 3 4 Values Of 2D Array [ Matrix ] : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 //Swap the first row and last row of a matrix   for(int i=0; i<c; i++) { int t=a[0][i]; //mat[0][c] - first row element a[0][i]=a[r-1][i]; //mat[r-1][c] - last row element a[r-1][i]=t; }  

cout<<"\n enter the row to interchange"; Values Of 2D Array [ Matrix ] : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 Values Of 2D Array [ Matrix ] : 9 1 2 3 5 6 7 8 2 3 4 4 5 6 7 int row1,row2; cout<<"\n enter the row to interchange"; cin>>row1>>row2; for(int i=0; i<c; i++) { int t=a[row1-1][i]; a[row1-1][i]=a[row2-1][i]; a[row2-1][i]=t; }