0% found this document useful (0 votes)
27 views10 pages

2D Array 18-7-23

The document discusses how to create and manipulate 2D arrays in Java, including how to initialize a 2D array, input and print values in row-wise and column-wise format, and calculate the sum of each row and column. It also shows examples of how to print the major and minor diagonals, border elements, non-border elements, and how to separate the elements into odd and even arrays.
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
27 views10 pages

2D Array 18-7-23

The document discusses how to create and manipulate 2D arrays in Java, including how to initialize a 2D array, input and print values in row-wise and column-wise format, and calculate the sum of each row and column. It also shows examples of how to print the major and minor diagonals, border elements, non-border elements, and how to separate the elements into odd and even arrays.
Copyright
© © All Rights Reserved
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/ 10

1.

Creating a2D array


Empty Array
Integer A[2][3]
Int A[][]=new int[2][3]

34 5 8
5 6 9

Integer A[][]={{34,5,8},{5,6,9}}
Int A[][]={{34,5,8},{5,6,9}}

Input in Array(Row wise)


Loop I from 0 to A.length-1
Loop j from 0 to A[].length-1
Input A[i][j]
End loop J
End loop i

Printing Array(Col wise)


Loop I from 0 to col-1
Loop j from 0 to row-1
Print A[j][i]
End loop J
End loop i

Print Array(Row wise)


Loop I from 0 to A.length-1
Loop j from 0 to A[].length-1
Print A[i][j]
End loop J
End loop I
Print sum of Row
Loop I from 0 to A.length-1
Sum=0
Loop j from 0 to A[].length-1
SUM=SUM+a[I][J]
End loop J
Print Sum
End loop i
Printing sum of Colum)
Loop I from 0 to col-1
Sum=0
Loop j from 0 to row-1
Sum=sum+a[J][I]
End loop J
Print sum
End loop i

1. public class SumofRowColumn


2. {
3. public static void main(String[] args) {
4. int rows, cols, sumRow, sumCol;
5.
6. //Initialize matrix a
7. int a[][] = {
8. {1, 2, 3},
9. {4, 5, 6},
10. {7, 8, 9}
11. };
12.
13. //Calculates number of rows and columns present in given matrix
14. rows = a.length;
15. cols = a[0].length;
16.
17. //Calculates sum of each row of given matrix
18. for(int i = 0; i < rows; i++){
19. sumRow = 0;
20. for(int j = 0; j < cols; j++){
21. sumRow = sumRow + a[i][j];
22. }
23. System.out.println("Sum of " + (i+1) +" row: " + sumRow);
24. }
25.
26. //Calculates sum of each column of given matrix
27. for(int i = 0; i < cols; i++){
28. sumCol = 0;
29. for(int j = 0; j < rows; j++){
30. sumCol = sumCol + a[j][i];
31. }
32. System.out.println("Sum of " + (i+1) +" column: " + sumCol);
33. }
34. } }

18/7/2023
Printing Left/Major Diagonal
34 5 67
4 77 80
40 22 11

Printing Right/Minor Diagonal


34 5 67
4 77 80
40 22 11

Printing Border elements

34 5 67
4 77 80
40 22 11
7 8 4
Loop I from 0 to row-1
Loop j from 0 to col-1
If (i==0) or (j==0) or( i==row-1)or(j==col-1)
Print A[i][j]
endif
End loop J
End loop i
Printing non Border elements

34 5 67 7
4 77 80 32
40 22 11 3
7 8 4 55
Loop I from 0 to row-1
Loop j from 0 to col-1
If (i==0) or (j==0) or( i==row-1)or(j==col-1)
Print “”
Else
Print A[i][j]
endif
End loop J
End loop i
34 5 67 7
4 77 80 32
40 22 11 3
7 8 4 55

Odd

even

Oc=0
Ec=0
Loop I from 0 to row-1
Loop j from 0 to col-1
If (A[i][j] MOD 2==0)
Even[ec]=A[i][j]
Ec=ec+1
Else
odd[oc]=A[i][j]
oc=oc+1
endif
End loop J
End loop i

You might also like