The document contains a series of Java programs that demonstrate various operations on double-dimensional arrays, including printing elements, finding sums, sorting rows and columns, and manipulating array structures. Each program is presented with its class name and main method, showcasing specific functionalities like printing diagonal elements, swapping rows and columns, and finding minimum and maximum values. The document serves as a comprehensive guide for handling array operations in Java.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views17 pages
Class X Double Dimensional Array Programs
The document contains a series of Java programs that demonstrate various operations on double-dimensional arrays, including printing elements, finding sums, sorting rows and columns, and manipulating array structures. Each program is presented with its class name and main method, showcasing specific functionalities like printing diagonal elements, swapping rows and columns, and finding minimum and maximum values. The document serves as a comprehensive guide for handling array operations in Java.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17
Double Dimensional Array
1 . Write a java program to print all elements of an array
public class printAllElements { public static void main(String[] args) { int [][]arr = {{1,2},{3,4},{5,6}}; for(int row = 0; row < arr.length; row++) { for(int col = 0; col < arr[0].length; col++) { System.out.print(arr[row][col]+" "); } System.out.println(); } } } 2 . Write a java program to print first and last row of an array public class print1StAndLast { public static void main(String[] args) { int [][]arr = {{1,2,3},{4,5,6},{7,8,9}}; for(int row = 0; row < arr.length; row++) { for(int col = 0; col < arr[0].length; col++) { if(row==0 || row==arr.length-1) System.out.print(arr[row][col]+" "); } System.out.println(); } } } 3 . Write a java program to print first and last row of an array public class borderElements { public static void main(String[] args) { int [][]arr = {{1,2,3},{4,5,6},{7,8,9}}; for(int row = 0; row < arr.length; row++) { for(int col = 0; col < arr[0].length; col++) { if(row==0 || row==arr.length-1 || col==0||col==arr[0].length- 1) System.out.print(arr[row][col]+" "); else System.out.print(" "); } System.out.println(); } } } 4 . Write a java program to print only diagonal elements of an array public class onlyDiagonal { public static void main(String[] args) { int [][]arr = {{1,2,3},{4,5,6},{7,8,9}}; for(int row = 0; row < arr.length; row++) { for(int col = 0; col < arr[0].length; col++) { if(row==col || row+col==arr.length-1) System.out.print(arr[row][col]+" "); else System.out.print(" "); } System.out.println(); } } } 5 . Write a java program to print all elements below the major Diagonal of an array public class majorDiagonal { public static void main(String[] args) { int [][]arr = {{1,2,3},{4,5,6},{7,8,9}}; for(int row = 0; row < arr.length; row++) { for(int col = 0; col < arr[0].length; col++) { if(row>=col) System.out.print(arr[row][col]+" "); else System.out.print(" "); } System.out.println(); } } } 6 . Write a java program to print all elements above the major Diagonal of an array public class aboveMajorDiagonal { public static void main(String[] args) { int [][]arr = {{1,2,3},{4,5,6},{7,8,9}}; for(int row = 0; row < arr.length; row++) { for(int col = 0; col < arr[0].length; col++) { if(row<=col) System.out.print(arr[row][col]+" "); else System.out.print(" "); } System.out.println(); } } } 7 . Write a java program to print all even elements of an array public class evenElements { public static void main(String[] args) { int [][]arr = {{1,2,3},{4,5,6},{7,8,9}}; for(int row = 0; row < arr.length; row++) { for(int col = 0; col < arr[0].length; col++) { if(arr[row][col]%2==0) System.out.print(arr[row][col]+" "); else System.out.print(" "); } System.out.println(); } } } 8 . Write a java program to multiply all elements of an array by 10 import java.util.*; public class MultiplyBy10 { void alterArr(int[][]arr){ for(int row = 0; row < arr.length; row++) { for(int col = 0; col < arr[0].length; col++) { System.out.println(arr[row][col]*=10); } } } } 9 . Write a java program to replace all negative elements of an array by 0 (zero). public class ReplaceNegativeElem { void alterArr(int[][] arr) { for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { if(arr[row][col] < 0) arr[row][col] = 0; System.out.println(arr[row][col]); } } } } 10 . Write a java program to swap first row with last row public class Swap1rowWithLast { void alterArr(int [][] arr) { int row1 = 0; int row2 = arr.length -1; for (int col=0;col<arr[0].length;col++) { int temp = arr[row1][col]; arr[row1][col] = arr[row2][col]; arr[row2][col] = temp; } for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { System.out.print(arr[row][col]+ " "); } System.out.println(); } } } 11 . Write a java program to swap first column with last column public class Swap1stColWithLastCol { void alterArr(int [][] arr) { int col1 = 0; int col2 = arr[0].length -1; for (int row=0;row<arr.length;row++) { int temp = arr[row][col1]; arr[row][col1] = arr[row][col2]; arr[row][col2] = temp; } } public static void main(String[] args) { Swap1stColWithLastCol sw = new Swap1stColWithLastCol(); int [][] arr = {{1,2,3},{4,5,6},{7,8,9}}; sw.alterArr(arr); for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { System.out.print(arr[row][col]+ " "); } System.out.println(); } } } 12 . Write a java program to circular shift the array up public class CircularShift { void alterArr(int [][] arr) { for(int row=0; row<arr.length-1;row++) { for (int col=0;col<arr[0].length;col++){ int temp = arr[row][col]; arr[row][col] = arr[row+1][col]; arr[row+1][col] = temp; } } for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { System.out.print(arr[row][col]+ " "); } System.out.println(); } } } 13 . Write a java program to Circular shift the array down public class CircularShiftDown1 { void alterArr(int [][] arr) { for(int row=arr.length-1; row>0;row--) { for (int col=0;col<arr[0].length;col++){ int temp = arr[row][col]; arr[row][col] = arr[row-1][col]; arr[row-1][col] = temp; } } for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { System.out.print(arr[row][col]+ " "); } System.out.println(); } } } 14 . Write a java program to shift the array one column left public class ShiftArrayLeft { void alterArr(int [][] arr) { for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length-1;col++){ int temp = arr[row][col]; arr[row][col] = arr[row][col+1]; arr[row][col+1] = temp; } } for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { System.out.print(arr[row][col]+ " "); } System.out.println(); } } } 15 . Write a java program to create mirror image of array public class MirrorImageOfTheArray { void alterArr(int [][] arr) { for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length/2;col++){ int temp = arr[row][col]; arr[row][col] = arr[row][arr[0].length-1-col]; arr[row][arr[0].length-1-col] = temp; } } for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { System.out.print(arr[row][col]+ " "); } System.out.println(); } } } 16 . Write a java program to create transpose of the array public class TransposeImage { void alterArr(int [][] arr, int [][] arr1) { for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++){ arr1[row][col] = arr[col][row]; } } for(int row=0; row<arr1.length;row++) { for (int col=0;col<arr1[0].length;col++) { System.out.print(arr1[row][col]+ " "); } System.out.println(); } } public static void main (String[] args) { int arr[][] = { {1, 1, 1},{2, 2, 2},{3, 3, 3}}; int arr1[][] = new int[3][3], i, j; TransposeImage t= new TransposeImage(); t. alterArr(arr, arr1); for(int row=0; row<arr1.length;row++) { for (int col=0;col<arr1[0].length;col++) { System.out.print(arr1[row][col]+ " "); } System.out.println(); } } } 17 . Write a java program to sort each row of the array public class SortEachRow { void alterArr(int [][] arr) { for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length/2;col++){ for (int k=0; k<arr.length-col-1; k++) { if (arr[row][k] > arr[row][k + 1]) { int t = arr[row][k]; arr[row][k] = arr[row][k + 1]; arr[row][k + 1] = t; } } } } for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { System.out.print(arr[row][col]+ " "); } System.out.println(); } } } 18 . Write a java program to sort each column of the array public class SortEachColumn { void alterArr(int [][] arr) { for (int col=0;col<arr[0].length;col++) { for (int row=0; row<arr.length;row++){ for (int k=0; k<arr.length-row-1; k++) { if (arr[k][col] > arr[k + 1][col]) { int t = arr[k][col]; arr[k][col] = arr[k + 1][col]; arr[k + 1][col] = t; } } } } for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { System.out.print(arr[row][col]+ " "); } System.out.println(); } } } 19 . Write a java program to find sum of diagonal elements in an array public class SumDiagonalElements { public static void main(String[] args) { int [][] arr = {{1,2},{3,4},{5,6}}; int sum = 0; for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { if(row==col || row+col+1==arr.length) sum+= arr[row][col]; } System.out.println("Sum = "+sum); } } } 20 . Write a java program to find sum of each row in an array public class sumOfEachRow { public static void main(String[] args) { int [][] arr = {{1,2},{3,4},{5,6}}; for(int row=0; row<arr.length;row++) { int sum = 0; for (int col=0;col<arr[0].length;col++) { sum+= arr[row][col]; } System.out.println("Sum = "+sum); } } } 21 . Write a java program to find sum of each Columns in an array public class sumOfAllColumns { public static void main(String[] args) { int [][] arr = {{1,2},{3,4},{5,6}}; for(int col=0;col<arr[0].length;col++) { int sum = 0; for (int row=0; row<arr.length;row++) { sum+= arr[row][col]; } System.out.println("Sum = "+sum); } } } 22 . Write a java program to find Minimum of all elements in an array public class MinInEachRow { public static void main(String[] args) { int [][] arr = {{1,2,0},{10,3,4},{5,11,6}}; int min = arr[0][0]; for (int row=0; row<arr.length;row++) { for(int col=0;col<arr[0].length;col++) { if(arr[row][col]<min) min = arr[row][col]; } System.out.println("Min = "+min); } } } 23 . Write a java program to find Maximum of all elements in an array public class MaxOFAll { public static void main(String[] args) { int [][] arr = {{1,2,0},{10,3,4},{5,11,6}}; int max = arr[0][0]; for (int row=0; row<arr.length;row++) { for(int col=0;col<arr[0].length;col++) { if(arr[row][col]>max) max = arr[row][col]; } System.out.println("Max = "+max); } } } 24 . Write a java program to find Minimum of each row in an array public class MinInEachRow { public static void main(String[] args) { int [][] arr = {{1,2,0},{10,3,4},{5,11,6}}; for (int row=0; row<arr.length;row++) { int min= arr[row][0]; for(int col=0;col<arr[0].length;col++) { if(arr[row][col]<min) min = arr[row][col]; } System.out.println(row+"Min = "+min); } } } 25 . Write a java program to find min of each row and its index in a new array public class MinOfEachRow { void alterArr(int [][] arr, int [][] mrow) { for(int row=0; row<arr.length;row++) { mrow[row][0] = arr[0][row]; for(int col=0;col<arr[0].length;col++) { if(mrow[row][0] > arr[row][col]) { mrow[row][0] = arr[row][col]; mrow[row][1] = col; } } System.out.print(" "); } } public static void main (String[] args) { int arr[][] = { {1, 4, 6},{10, 2, 8},{7, 5, 3}}; int mrow[][] = new int[3][3]; MinOfEachRow m= new MinOfEachRow(); m. alterArr(arr, mrow); for(int row=0; row<mrow.length;row++) { for (int col=0;col<mrow[0].length;col++) { System.out.print(mrow[row][col]+ " "); } System.out.println(); } } } 26 . Write a java program to find max of each col and its index in a new array public class maxOfEachColumn1 { void alterArr(int [][] arr, int [][] mcol) { for(int col=0;col<arr[0].length;col++) { mcol[col][0] = arr[0][col]; for (int row=0; row<arr.length;row++){ if(mcol[col][0] < arr[row][col]) { mcol[col][0] = arr[row][col]; mcol[col][1] = row; } } } } public static void main (String[] args) { int arr[][] = { {1, 4, 6},{10, 2, 8},{7, 5, 3}}; int mcol[][] = new int[3][3]; maxOfEachColumn1 s= new maxOfEachColumn1(); s. alterArr(arr, mcol); for(int row=0; row<mcol.length;row++) { for (int col=0;col<mcol[0].length;col++) { System.out.print(mcol[row][col]+ " "); } System.out.println(); } } } 27 . Write a java program to count all even elements in an array public class countAllEvenElements { public static void main(String[] args) { int [][] arr = {{1,2,3},{4,5,6},{7,8,9}}; int count=0; for(int row=0; row<arr.length;row++) { for (int col=0;col<arr[0].length;col++) { if(arr[row][col]%2 == 0) count++; } } System.out.print("count:"+count); } }