VKSLEARNING HUB 2 D Array VKSLEARNING HUB What
VKS-LEARNING HUB 2 -D Array
VKS-LEARNING HUB 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
VKS-LEARNING HUB Examples of Matrices
VKS-LEARNING HUB Matrices (2 D-array) • . A matrix is defined using a type declaration statement. – datatype array_name[row_size][column_size]; – int matrix[3][4]; 4 1 0 2 Row 0 Row 1 Row 2 4 1 0 2 -1 -1 2 4 3 4 0 -1 3 1 2 3 0 -1 3 Column 0 Column 1 Column 2 Column 3 1 in memory 4
VKS-LEARNING HUB 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) 5
VKS-LEARNING HUB Initialization int x[4][4] = { int x[][4] = { {2, {7, {5, {2, 3, 4, 1, 5, 7, 2}, 5, 9}, 6, -3}, -1, 3}}; 6
VKS-LEARNING HUB 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 0 i 1 2 0 1 2 1 0 1 2 j 2 0 1 2 3 0 1 2 0 0 i 1 2 0 0 0 1 1 2 2 3 3 7
VKS-LEARNING HUB Exercise • Write the nested loop to initialize a 2 D array as follow 0 1 1 2 2 3 4 3 4 5 int i, j, x[4][3]; for(i=0; i<4; i++) for(j=0; j<3; j++) x[i][j] = i+j; 8
VKS-LEARNING HUB //Square matrix have equal no of rows and column where as Non-Square Matrix have rows and column are not equal //Input Element in 2 D 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]; } }
VKS-LEARNING HUB // Display Element in 2 D Array (matrix) void display(int mat[][10], int n) { for(int r=0; r<n; r++) { for(int c=0; c<n; c++) printf("%5 i", 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("%5 i", mat[r][c]); //Or cout<<setw(5)<<mat[r][c]; Or cout<<'t'<<mat[r][c]; cout<<endl; } }
Max in 2 D • Find the maximum of int matrix[3][4] 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]; 0 0 0 1 1 2 0 3 2 1 -1 2 4 3 2 0 -1 3 1 11
Find a value in 2 D • 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 0 0 1 1 2 0 3 2 1 -1 2 4 3 2 0 -1 3 1 12
VKS-LEARNING HUB 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
VKS-LEARNING HUB Matrix sum • Compute the addition of two matrices 0 1 2 3 0 0 1 0 2 1 -1 2 4 3 2 0 -1 3 1 + 0 1 2 3 0 3 -1 3 1 1 1 4 2 0 2 2 1 1 3 0 = 0 3 1 0 2 3 3 3 1 0 6 6 3 2 2 0 4 4 14
VKS-LEARNING HUB // Adding Elements of 2 Matrix in 3 rd 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]; }
VKS-LEARNING HUB 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.
VKS-LEARNING HUB 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 Now we multiply across the first row and down the second column and we’ll put Now we multiply across the second row and down the first column and we’ll put We multiplied across first row and down first column so we put the answer in the first row, second column. the answer in the second row, first column. put the answer in the second row, second column. answer in the first row, first column.
VKS-LEARNING HUB Matrix multiplication double a[3][2], b[2][4], c[3][4]; • Find c = a * b; 3 5 1 4 2 6 x 2 3 7 1 4 5 6 8 = 22 29 45 35 18 40 47 21 26 33 43 49 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 18
VKS-LEARNING HUB 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.
VKS-LEARNING HUB // Multiplying Elements of 2 Matrix in 3 rd 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]; }
VKS-LEARNING HUB void main() { int a[10], b[10], mult[10], r 1, c 1, r 2, c 2, i, j, k; cout << "Enter rows and columns for first matrix: "; cin >> r 1 >> c 1; cout << "Enter rows and columns for second matrix: "; cin >> r 2 >> c 2; /* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */ while (c 1!=r 2) { cout << "Error! column of first matrix not equal to row of second. "; cout << "Enter rows and columns for first matrix: "; cin >> r 1 >> c 1; cout << "Enter rows and columns for second matrix: "; cin >> r 2 >> c 2; }
VKS-LEARNING HUB * STORING elements of first matrix. */ cout << endl << "Enter elements of matrix 1: " << endl; for(i=0; i<r 1; ++i) for(j=0; j<c 1; ++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<r 2; ++i) for(j=0; j<c 2; ++j) { cout << "Enter element b" << i+1 << j+1 << " : "; cin >> b[i][j]; } /* Initializing elements of matrix mult to 0. */ for(i=0; i<r 1; ++i) for(j=0; j<c 2; ++j) { mult[i][j]=0; }
VKS-LEARNING HUB /* Multiplying matrix a and b and STORING in array mult. */ for(i=0; i<r 1; ++i) for(j=0; j<c 2; ++j) for(k=0; k<c 1; ++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<r 1; ++i){ for(j=0; j<c 2; ++j) { cout << mult[i][j] << " " ; } cout << endl; } getch(); } C++ Code for Matrix Multiplication
VKS-LEARNING HUB Transpose B A 1 4 5 2 3 6 6 24
VKS-LEARNING HUB 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]; }
VKS-LEARNING HUB Row 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; } } Column wise sum of Matrix 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; } }
VKS-LEARNING HUB Displaying Diagonal of Matrix Values Of 2 D Array [ Matrix ] : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 void diagonal(int mat[][10], int n) { for(int k=0; k<n; k++) cout<<mat[k][k]; for(int k=0; k<n; k++) cout<<mat[k][n-k-1]; } Sum of Diagonal of Matrix void diagonalsum(int mat[][10], int n) { int sum 1=0, sum 2=0; for(int k=0; k<n; k++) { sum 1+=mat[k][k]; sum 2+=mat[k][n-k-1]; } cout<<"Sum of the main diagonal = "<<sum 1<<endl; cout<<"Sum of the other diagonal = "<<sum 2<<endl; }
VKS-LEARNING HUB Upper Right Triangle Of Matrix Values Of 2 D Array [ Matrix ] : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 if(r==c) { cout<<"n. Upper Right Triangle Of Matrix "; for(i=0; i<r; i++) { for(j=0; j<c; j++) Upper Right Triangle Of Matrix { if(j>=i) 1 2 3 4 cout<<a[i][j]<<"t"; 6 7 8 else 2 3 cout<<"t"; 7 } cout<<endl; } }
VKS-LEARNING HUB Lower Right Triangle Of Matrix Values Of 2 D Array [ Matrix ] : 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 if(r==c) { cout<<"n. Upper Right Triangle Of Matrix "; for(i=0; i<r; i++) { for(j=0; j<c; j++) Lower Left Triangle Of Matrix { 1 if(j<=i) 5 6 cout<<a[i][j]<<"t"; 9 1 2 else 4 5 6 7 cout<<"t"; } cout<<endl; } }
VKS-LEARNING HUB Values Of 2 D Array [ Matrix ] : 4 5 6 7 1 2 3 4 5 6 7 8 9 1 2 3 4 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; }
VKS-LEARNING HUB Values Of 2 D Array [ Matrix ] : 1 2 3 4 9 1 2 3 5 6 7 8 9 1 2 3 4 4 5 6 7 int row 1, row 2; cout<<"n enter the row to interchange"; cin>>row 1>>row 2; for(int i=0; i<c; i++) { int t=a[row 1 -1][i]; a[row 1 -1][i]=a[row 2 -1][i]; a[row 2 -1][i]=t; }
VKS-LEARNING HUB
- Slides: 32