OOPM JAVA - program for matrix multiplication and transpose of a matrix.

 Experiment No. 07

Aim: Write a program to perform matrix multiplication and transpose of a matrix.

Objective: To make aware of two-dimensional array and its use 

Outcome:

Students implemented two-dimensional array.

They are exposed to method of matrix multiplication and transpose.

They learned to make use of nested loops

Theory: 

Arrays

An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. Aspecific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information.

Multidimensional Arrays

In Java, multidimensional arrays are actually arrays of arrays. These, as you might expect, look and act like regular multidimensional arrays. However, as you will see, there are a couple of subtle differences. To declare a multidimensional array variable, specify each additional index using another set of square brackets. For example, the following declares a two dimensional array variable called twoD.int twoD[][] = new int[4][5];

This allocates a 4 by 5 array and assigns it to twoD. Internally this matrix is implemented as an array of arrays of int.


Algorithm:

1. Start.

2. Take input for size of an array (m x n)

3. Display “enter array elements”

4. Take input for array A[3][3] and B [3][3]

5. i=0.

6. while (i<3)

6.1 j=0.

6.2 while (j<3)

6.2.1 k=1

6.2.2 while k<3

C [i][k]+= A [i][j] * B[j] [k]

k++

j++

6.3 i++

i=0

while (i<3)

j=0

while j<3

Display C[i][j]

j++

8.3 i++

i=0.

while (i<3)

j=0.

while (j<i)

temp = A [i][j]

A[i][j] = A[j][i] 

A[j][i] = temp

j++

i++

i=0

while (i<3)

j=0

while j<3

Display A[i][j]

j++

12.3 i++

Stop



Code : 

import java.util.Scanner;

class Matrix

{

  void matrixMul(int m, int n, int p, int q)

  {

    int[][] a,b,c,t;

    a = new int[m][n];

    b = new int[p][q];

    c = new int[m][q];

    t = new int[q][m];

    Scanner s = new Scanner(System.in);

    System.out.println("Enter the elements of matrix A: ");

    for(int i = 0; i < m; i++)

    {

      for(int j = 0; j < n; j++)

      {

        a[i][j] = s.nextInt();

      }

    }

    System.out.println("Enter the elements of matrix B: ");

    for(int i = 0; i < p; i++)

    {

      for(int j = 0; j < q; j++)

      {

        b[i][j] = s.nextInt();

      }

    }

    for(int i = 0; i < m; i++)

    {

      for(int j = 0; j < q; j++)

      {

        for(int k = 0; k < n; k++)

        {

          c[i][j] += a[i][k]*b[k][j];

        }

      }

    }

    System.out.println("Multiplication of matrix A and B is: ");

    for(int i = 0; i < m; i++)

    {

      for(int j = 0; j < q; j++)

      {

        System.out.print(c[i][j]+"\t");

      }

      System.out.print("\n");

    }

    for(int i = 0; i < q; i++)

    {

      for(int j = 0; j < m; j++)

      {

        t[i][j] = c[j][i];

      }

    }

    System.out.println("Elements of transpose matrix T are: ");

    for(int i = 0; i < q; i++)

    {

      for(int j = 0; j < m; j++)

      {

        System.out.print(t[i][j]+"\t");

      }

      System.out.print("\n");

    }

  }

}

class Driver

{

  public static void main(String[] args)

  {

    Scanner s = new Scanner(System.in);

    System.out.println("Enter no of rows in matrix A: ");

    int m = s.nextInt();

    System.out.println("Enter no of columns in matrix A: ");

    int n = s.nextInt();

    System.out.println("Enter no of rows in matrix B: ");

    int p = s.nextInt();

    System.out.println("Enter no of columns in matrix B: ");

    int q = s.nextInt();

    if(n == p)

    {

      Matrix obj = new Matrix();

      obj.matrixMul(m,n,p,q);

    }

    else

    {

      System.out.println("Matrix multiplication cannot be performed...");

    }

  }

}


Output: 

program to perform matrix multiplication and transpose of a matrix.


Observations and learning: 

In this experiment we have used nested for loops with various conditions to get the desired output. We performed the matrix multiplication operation in this experiment.

 

 Conclusion: 

We get the correct output for the matrix multiplication operation and also the transpose matrix with this program.

 

Question of Curiosity 

1. What is an array?

Ans: An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information.

 

2. How do you declare an array?

Ans: Instantiating an Array in Java var-name = new type [size];

Here, type specifies the type of data being allocated, size specifies the number of elements in the array, and var-name is the name of array variable that is linked to the array.

 

3. Advantages and disadvantages of Array?

Ans: Advantages of Arrays

● Arrays represent multiple data items of the same type using a single name.

● In arrays, the elements can be accessed randomly by using the index number.

● Arrays allocate memory in contiguous memory locations for all its elements.

 

Disadvantages of Arrays

● The number of elements to be stored in an array should be known in advance.

● An array is a static structure (which means the array is of fixed size). ...

● Insertion and deletion are quite difficult in an array as the elements are stored in consecutive memory locations and the shifting operation is costly.

 

4. Can we change the size of an array at run time?

Ans: If you create an array by initializing its values directly, the size will be the number of elements in it. Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array.

 

5. Can you declare an array without assigning the size of an array? 

Ans: In certain situations, you can declare an array without putting a number in the brackets. For example, you can initialize an array without specifying the number of elements: int MyNumbers[] = {1,2,3,4,5,6,7,8,9,10};

 

6. What is the default value of Array?

Ans: Each class variable, instance variable, or array component is initialized with a default value when it is created. For type byte, the default value is zero, that is, the value of 0. For type short, the default value is zero, that is, the value of 0.

 

7. How to print element of Array?

Ans: You cannot print array elements directly in Java, you need to use Arrays. toString() or Arrays. deepToString() to print array elements. Use toString() if you want to print one-dimensional array and use deepToString() method if you want to print two-dimensional array.

 

8. Can we declare array size as a negative number?

Ans: No, you cannot use a negative integer as size, the size of an array represents the number of elements in it, –ve number of elements in an array makes no sense.

 

9. When will we get ArrayStoreException?

Ans: ArrayStoreException in Java occurs whenever an attempt is made to store the wrong type of object into an array of objects. The ArrayStoreException is a class which extends RuntimeException, which means that it is an exception thrown at the runtime.

 

10. What is the meaning of anonymous array? Explain with an example?

Ans: An array without name is known as anonymous array in java. As the array do not have any name so it can be used only once. Anonymous array is passed as an argument of method. Anonymous array is created and initialized in the same line.

  

Similar Programs

  1. write a program to Check whether Matrix is Magic Square or Not ?
  2. write a program to Print Square of Each Element of 2D Array Matrix
  3. write a program to find addition of Lower Triangular Elements 
  4. write a program to calculate sum of Upper Triangular Elements
  5. write a program to evaluate Subtraction of two matrices ( matrix )
  6. write a program for addition of two matrices 
  7. write a program Addition of Diagonal Elements in Matrix
  8. write a program Addition of All Elements in Matrix
  9. write a program Accessing 2-D Array Elements
  10. write a program to Find Inverse Of 3 x 3 Matrix in 10 Lines


Comments