Array:Part-three

Posted by

Continue from Array: Part-two

MULTI-DIMENSION ARRAY

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

  1. To store marks of different students in different subjects ,2-D array can be used.
  2. 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.

  1. C [ 0,0 ]  = A [ 0,0 ]*B [ 0,0 ] + A [ 0,1] * B [ 1,0 ]
  2. C [ 0,1 ]  = A [ 0,0 ]*B [ 0,1 ]  +  A [ 0,1] * B [ 1,1 ]
  3. C [ 1,0 ]  = A [ 1,0 ]*B [ 0,0 ]  +  A [ 1,1] * B [ 1,0 ]
  4. 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.


Algorithm

Let A ( m1 *n1 )  and B ( m2*n2 ) are matrix to be multiplied.

  1. if n1 not equal to m2
  2. print ‘ Can’t multiply, invalid input’ else
  3. for i = 0 to m1 – 1
  4. for j = 0 to n2 – 1
  5. Set sum = 0
  6. for k = 0 to to m2 – 1
  7. sum = sum + A [ i,k ] * B [ k,j ]
  8. C [ i,j ] = sum.
  9. Exit.

PRACTICE 

practice yourself

  1. Array : Part-one
    1. Print array in reverse order.
    2. Find a pair of elements in array whose sum is equal to 11.
    3. Rotate array from left to right N times and print final array.
    4. Update odd numbers to even numbers by adding one or subtracting one.
    5. Find sum of all prime numbers in  array.
  2. Array: Part-two
    1. Write a program to insert an element at given index or delete an element from given index.
    2. Find the position of given element if not found return -1.
    3. Shift all even numbers to left side and all odd numbers to right side in array.
    4. Perform swap operations like: first with last, second with second last and so on.
    5. Return true if elements of array can be arranged in  Fibonacci series.
  3. Array:Part-three
    1.  find sum of three matrix.
    2. Check matrix is spars or not.
    3. interchange rows and columns in m*m matrix.
    4. Try to create tic-tac-toe game on 3*3 matrix.
    5. 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 🙂

Leave a Reply