Multi dimension Array in Java

0
Multi dimension Array in Java



using this array we can store element using rows and columns ,Multi dimension array base index will be started from 0,0 .

using this we can create matrix.


Syntax of multi dimension array:-


Datatype array-name [][] = new Datatype[row][col];

int arr[][] = new int[2][2];   //2 rows and 2 columns

int a[][] = {{1,2},{3,4}};


Simple Program of Matrix without user input?


class MArr
{
   public static void main(String args[])
   {
        int arr[][] = {{1,2},{3,4}};
        for(int i=0;i<2;i++)
        {
           for(int j=0;j<2;j++)
             {
               System.out.print(arr[i][j]+" ");
             }
             System.out.println();

        }


   }



}




Simple program of matrix to take user Input

import java.util.Scanner;
class MArr
{
   public static void main(String args[])
   {
        Scanner sc = new Scanner(System.in);
        int r,c;
        System.out.println("enter row");
        r=sc.nextInt();
        System.out.println("enter column");
        c=sc.nextInt(); 
        int arr[][] = new int[r][c];
        for(int i=0;i<r;i++)
        {
           for(int j=0;j<c;j++)
             {
               arr[i][j]=sc.nextInt();
             }
           

        }
        System.out.println("Matrix is");
        for(int i=0;i<r;i++)
        {
           for(int j=0;j<c;j++)
             {
               System.out.print(arr[i][j]+" ");
             }
             System.out.println();

        }


   }



}


1) WAP to perform addition and multiplication of matrix?


2) WAP to calculate sum of matrix elements?











Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)