Download presentation
Presentation is loading. Please wait.
1
many subjects (DFS, C++, SQL)
Array Store marks of 1 subject (DFS) for many (5) students. Store marks of many subjects (DFS, C++, SQL) for many (5) students. Store marks of 1 subject for 1 student. Array DFS Array C++ Array SQL Array DFS 10 9 12 13 25 25 30 45 40 35 40 42 41 45 48 int 10 10 9 12 13 25 Array would be preferred. 1 integer variable is sufficient. Array Marks Array DFS 2D Array 10 9 12 13 25 25 30 45 40 35 40 42 41 45 48 10 9 12 13 25 Matrix
2
Array Array: To store marks of 1 subject for 1 student:
1 Integer variable is sufficient. To store marks of 1 subject for many students: Array would be preferred. 1 dimensional array is sufficient. To store marks of many subjects of many students: 2-dimensional array / Matrix is required.
3
2-D Array Index: A[ i ][ j ] C: int A[3][3] Position: APrPc
Fortran: int A[3][3] Pascal: int A[-1...1][1...3] Col (Pc) Position 1 2 3 C Col (j) Row (Pr) Index 1 2 Row (i) 1 10 12 9 A[0][0] A[0][2] A[0][1] A11 A13 A12 2 1 11 5 2 Rows A[1][0] A[1][1] A[1][2] A21 A22 A23 15 10 3 3 2 A[2][0] A[2][1] A[2][2] Size A31 A32 A33 3 x 3 m x n Array A Cols
4
2-D Array Array A[-2...1][-3...-1] W = 2 Questions: Pc Pos 1 2 3
What is the value of m, number of rows? j Pr Index -3 -2 -1 i 4 1 -2 A[-2][-3] A[-2][-2] A[-2][-1] What is the value of n, number of cols? A11 A12 A13 3 2 -1 A[-1][-3] A[-1][-2] A[-1][-1] How many elements are there in A? A21 A23 A22 m x n A[0][-3] A[0][-2] A[0][-1] 3 4 x 3 = 12 A31 A32 A33 What is the memory occupied by entire array? A[1][-3] A[1][-2] A[1][-1] 4 1 A41 12 x 2 = 24 A42 A43 4 x 3
5
2-D Array Array A[-23...17][-37...11] W = 4 Pc Pos 1 2 . . . j Pr
Index -37 -36 . . . Questions: i 1 -23 How many elements are there in A? m x n 2 -22 What is the memory occupied by entire array? 3 -21 . . Difficult because the array is large. Equations/Formulas are needed again.
6
2-D Array Array A [-23...17] [-37...11] Size = U – L + 1 Row Array
Column Array Index -37 -36 -35 -34 -23 -22 -21 -20 .
7
2-D Array Array A [-23...17] [-37...11] W = 2 Size = U – L + 1
Row Array Column Array Row Array Col Array Lr = -23 Lc = -37 Ur = 17 Uc = 11 Size(Ar) = Ur – Lr + 1 Size(Ac) = Uc – Lc + 1 Size(Ac) = 11 – (-37) + 1 Size(Ar) = 17 – (-23) + 1 Size(Ac) = Size(Ar) = Size(Ac) = 49 Size(Ar) = 41 n = 49 m = 41 Size(A) = m x n = 41 x 49 = 2009 Total Memory Occupied = Size x W = 2009 x 2 = 4018
8
2-D Array Array A [-100...99] [-5...-1] W = 4 Size = U – L + 1
Find out the size and total memory occupied by the array A[ ][ ] having word size of 4. Question: Array A [ ] [ ] W = 4 Row Array Column Array Lr = -100 Lc = -5 Ur = 99 Uc = -1 Size(Ar) = Ur – Lr + 1 Size(Ac) = Uc – Lc + 1 Size(Ac) = -1 – (-5) + 1 Size(Ar) = 99 – (-100) + 1 Size(Ac) = Size(Ar) = Size(Ac) = 5 Size(Ar) = 200 n = 5 m = 200 Size(A) = m x n = 200 x 5 = 1000 Total Memory Occupied = Size x W = 1000 x 4 = 4000
9
2-D Array Row by Row / Row-wise Column by Column / Column-wise 1
M = 1, W = 1 A11 A11 2 A12 A21 3 A13 A31 A11 A12 A13 A14 4 A14 A12 5 A21 A22 6 A21 A22 A23 A24 A22 A32 7 A23 A13 8 A24 A23 A31 A32 A33 A34 9 A31 A33 10 3 x 4 A32 A14 11 Will it be stored in memory? Yes A33 A24 12 On Continuous locations? Yes A34 A34 Row-major order Column-major order
10
2-D Array Memory representation of Matrix /
Storage mechanism of 2D array: 2 ways to store a 2D array / Matrix in memory: Row-major order: All elements are stored row by row / row-wise. Example: First row, Second row and so on. Column-major order: All elements are stored column by column / column-wise. First column, Second column and so on.
11
2-D Array Array A[-2...1][-1...1] M = 1000 W = 2 Row Major Order
Questions: A11 1000 What is the address location of A[0][0]? A[-2][-1] A11 A[-2][0] A12 A[-2][1] A13 A12 1002 1014 A13 1004 A21 1006 What is the address location of A33? A[-1][-1] A21 A[-1][0] A22 A[-1][1] A23 A22 1008 1016 A23 1010 A31 1012 What is the address location of A32? A[0][-1] A31 A[0][0] A32 A[0][1] A33 A32 1014 1014 A33 1016 A[1][-1] A41 A[1][0] A42 A[1][1] A43 A41 1018 A42 1020 4 x 3 A43 1022
12
2-D Array SPARSE MATRIX Sparse means ‘Not Dense’ / ‘Not Thick’ 10 - -
9 - - - - - - - - - - - 2 - - - - - 1 12 - - - 11 Majority of the elements are NULL in this matrix. Storing of NULL elements is a wastage of memory. We need a technique by which only non-null elements are stored.
13
Classification of Sparse Matrix
Based on the availability of NON-NULL elements. Sparse Matrix Asymmetric Matrix (Pattern) Symmetric Matrix Non-symmetric Matrix Band Triangular Tridiagonal Lower Upper Diagonal Lower- Left Lower- Right Upper- Left Upper- Right
14
Classification of Sparse Matrix
Lower-Left Lower-Right Upper-Left Upper-Right Null * * * * * * * * * * * * * * * * * * * * Non-Null * * * * * * * Diagonal * * * * * * * * * * * * * * * * * * * * * * * * * * * Tri-Diagonal * * * * * * * * * * * * * * * * * * * * * * * *
15
Application of 2D Array / Matrix
Representation of Polynomials using Array Polynomial in 2 variables Co-efficient 2x2y2 + 3x2y + 5xy2 – 10xy – 15x + 20y - 100 -10 5 -100 20 - 3 2 -15 -10 5 Dimension? - 3 2 3 x 3 Axiyj -> Store the co-efficient A in i th row and j th column. Not Working Axiyj -> Store the co-efficient A in (i+1) th row and (j+1) th column. Processing? Processing on Polynomials (Addition, Subtraction) can be easily done by manipulating Matrices.
16
Application of 2D Array / Matrix
Representation of Polynomials using Array Polynomial in 2 variables 10x4y3 + 5xy - 50 Axiyj -> Store the co-efficient A in (i+1) th row and (j+1) th column. Sparse Matrix -50 - - - Lot of memory wastage. - 5 - - - - - - - - - - - - - 10 5 x 4
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.