How to add two matrix in Multi dimensional Array




Hi reader's
I hope you will learn something new after you read this new blog post?
This article will help those people who are still looking for a solution in a complex method of
Multi dimension Array in java. we know that programming is a little bit sick in terms of coding, if this is your first time to study the code then I would highly suggest you should get an instructor that could really emphasis what the codes really means and how it works, before proceeding, I assume that you have a  basic to intermediate knowledge about programming languages. so you will not get lost about the Syntax that I use below,  if ever in some way you lost? just feel free to write your question in the comment box.. then I will be happy to respond to you soon.
The codes below  will show you how  to add  two matrices in a multi-dimensional array:

import java.io.*;
import java.util.Arrays.*;
public class ToSum
{
    public static void main (String[]args)throws IOException
    {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        //First array
        System.out.println("Enter a length of rows:");
        int a=Integer.parseInt(input.readLine());
        System.out.println("Enter a length of columns:");
        int b =Integer.parseInt(input.readLine());
        int uno[][]= new int [a][b];
        for(int i=0;i<a;i++)
            {
         for(int j=0;j<b;j++)
                {
           System.out.println("Enter the elements:");
           uno[i][j]=Integer.parseInt(input.readLine());
           System.out.print("\t"+uno[i][j]);
            }
          System.out.println("");
        }
        //Calling the method 1:
        System.out.println("This is the 1st array:");
        displayMethod(uno);
      
        
        //Second array:
        System.out.println("Enter the length of rows in 2nd array:");
        int c = Integer.parseInt(input.readLine());
        System.out.println("Enter the length of columns in 2nd array:");
        int d = Integer.parseInt(input.readLine());
        int dos[][] = new int [c][d];
        for(int i =0; i<c;i++)
        {
            for(int j=0;j<d;j++)
            {
               System.out.println("Enter the elements 2nd array:");
               dos[i][j]=Integer.parseInt(input.readLine());
                System.out.print("\t"+dos[i][j]);
            }
            System.out.println("");
          
                
        }
        //calling the method 2
        System.out.println("This is second array:");
        displayMethod2(dos);
        int sum[][]= new int[uno.length][dos.length];
        System.out.println("Result of 2Array added:");
        for(int i=0;i<uno.length;i++)
        {
            for(int j=0; j<dos.length;j++)
            {
               sum[i][j] = uno[i][j]+dos[i][j];
               System.out.print("\t"+sum[i][j]);
            }
            
            System.out.println("");
        }
        
      }
      //this is method 1
      public static void displayMethod(int x[][])
      {
       for(int i=0;i<x.length;i++)
       {
           for(int j=0;j<x[i].length;j++)
           {
           System.out.print("\t"+x[i][j]);
           }
           System.out.println("");
       } 
       //This is method 2
      }
      public static void displayMethod2(int y[][])
      {
          for(int i =0;i<y.length;i++)
          {
              for(int j=0;j<y[i].length;j++)
              {
               System.out.print("\t"+y[i][j]);
               
              }
              System.out.println("");
          }
          
       }
  }

Post a Comment

0 Comments