Presentation is loading. Please wait.

Presentation is loading. Please wait.

2-D Array.

Similar presentations


Presentation on theme: "2-D Array."— Presentation transcript:

1 2-D Array

2 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

3 Examples of Matrices

4 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

5 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)

6 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},

7 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 1 2 i j 1 2 i 1 2 1 2 3

8 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

9 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]; }

10 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; }

11 Max in 2D Find the maximum of int matrix[3][4] int max = matrix[0][0];
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

12 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; 1 2 -1 2 4 3 1 -1 3 1 2

13 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

14 Matrix sum Compute the addition of two matrices + = 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

15 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]; }

16 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.

17 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.

18 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

19 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.

20 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]; }

21 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; }

22 * 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; }

23 /* 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

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

25 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]; }

26 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; }

27 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 ] : Values Of 2D Array [ Matrix ] : 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]; }

28 cout<<"\nUpper Right Triangle Of Matrix "; for(i=0; i<r; i++)
Values Of 2D Array [ Matrix ] : 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 2 3 7

29 cout<<"\nUpper Right Triangle Of Matrix "; for(i=0; i<r; i++)
Lower Right Triangle Of Matrix Values Of 2D Array [ Matrix ] : 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

30 //Swap the first row and last row of a matrix
Values Of 2D Array [ Matrix ] : Values Of 2D Array [ Matrix ] : //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; }

31 cout<<"\n enter the row to interchange";
Values Of 2D Array [ Matrix ] : Values Of 2D Array [ Matrix ] : 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; }

32


Download ppt "2-D Array."

Similar presentations


Ads by Google