0% found this document useful (0 votes)
7 views15 pages

Mod3 2

Uploaded by

pp6524878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Mod3 2

Uploaded by

pp6524878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Multidimensional Arrays

multidimensional arrays are arrays of


arrays.
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
When allocate memory for a
multidimensional array, we need only specify
the memory for the first (leftmost) dimension.
we can allocate the remaining dimensions
separately.
int x[][] = new int[4][];
x[0] = new int[5];
x[1] = new int[5];
x[2] = new int[5];
x[3] = new int[5];
Program to find the sum of two matrices
import java.util.Scanner;
public class MatrixAddition {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int m = s.nextInt();
System.out.print("Enter number of columns: ");
int n = s.nextInt();
int a[][] = new int[m][n];
int b[][] = new int[m][n];
System.out.println("Enter the first matrix");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("Enter the second matrix");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
b[i][j] = s.nextInt();
}
}
int c[][] = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}}
System.out.println("The sum of the two
matrices is");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}}}
Program to search an element in an
array
class Searcharray
{
public static void main(String args[])
{
int a[]={10,20,20,10,40,70,10};
int s=10,count=0;
System.out.println("s="+s); // read a value
from keyboard
for(int i=0;i<7;i++)
{
if(a[i]==s)
{
System.out.println(s+" is present in the index
of"+" "+i+" ");
count++;
}
}
if(count==0)
{
System.out.println("item does not found");
}
}
}
o/p
javac Searcharray.java
java Searcharray
s=10
10 is present in the index of 0
10 is present in the index of 3
10 is present in the index of 6

You might also like