Continue from Array: Part-two
A multi-dimension array or N-Dimension store data element in N-dimensions. Simplest form of multi-dimension array is 2-D array which store data in two dimensions. There are many situation where it’s useful to use multi-dimension array like
- To store marks of different students in different subjects ,2-D array can be used.
- To store marks of different students in different subject in different exams 3-D array can be used. etc.
Here we will talk about only 2-D array, a 2-D array of m rows and n columns has m*n elements. We can use Notation Array [ i,j ] to represent an element in 2-D array where i counts rows and j counts columns such that following condition holds true
0 <= i < m and 0 <= j < n
Left to right Columns ( 0 to n-1 ) and Top to bottom Rows (0 to m-1 ).
Example-4-> Matrix multiplication
To obtain Matrix C.
- C [ 0,0 ] = A [ 0,0 ]*B [ 0,0 ] + A [ 0,1] * B [ 1,0 ]
- C [ 0,1 ] = A [ 0,0 ]*B [ 0,1 ] + A [ 0,1] * B [ 1,1 ]
- C [ 1,0 ] = A [ 1,0 ]*B [ 0,0 ] + A [ 1,1] * B [ 1,0 ]
- C [ 1,1 ] = A [ 1,0 ]*B [ 0,1 ] + A [ 1,1] * B [ 1,1 ]
In above all operations observer that to obtain any value in C [ i,j ] , Row ( i) remains same in Matrix A and Column ( j ) remains same in Matrix B, but column in Matrix A and row in Matrix B varies and both have same values. second observation is number of Columns in Matrix A and number of rows in matrix B must be equal.
From above conclusion we can perform multiplication using three loop one for row , second for column and third inner loop which covers column in first matrix and row in second matrix.
Let A ( m1 *n1 ) and B ( m2*n2 ) are matrix to be multiplied.
- if n1 not equal to m2
- print ‘ Can’t multiply, invalid input’ else
- for i = 0 to m1 – 1
- for j = 0 to n2 – 1
- Set sum = 0
- for k = 0 to to m2 – 1
- sum = sum + A [ i,k ] * B [ k,j ]
- C [ i,j ] = sum.
- . Array : Part-one
- Print array in reverse order.
- Find a pair of elements in array whose sum is equal to 11.
- Rotate array from left to right N times and print final array.
- Update odd numbers to even numbers by adding one or subtracting one.
- Find sum of all prime numbers in array.
- Array: Part-two
- Write a program to insert an element at given index or delete an element from given index.
- Find the position of given element if not found return -1.
- Shift all even numbers to left side and all odd numbers to right side in array.
- Perform swap operations like: first with last, second with second last and so on.
- Return true if elements of array can be arranged in Fibonacci series.
- find sum of three matrix.
- Check matrix is spars or not.
- interchange rows and columns in m*m matrix.
- Try to create tic-tac-toe game on 3*3 matrix.
- Try to create Sudoku game on 5*5 board.
Link to answers will be available soon here………………………………..
Post your questions or suggestions in comment…..Thanks 🙂