Skip to content

Commit 2f1066b

Browse files
committed
2-D Array
1 parent cef4b74 commit 2f1066b

File tree

7 files changed

+456
-0
lines changed

7 files changed

+456
-0
lines changed

Two Dimensional Arrays/Array2Dim.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Create 2-d array.*/
2+
3+
import java.util.Scanner;
4+
5+
public class Array2Dim
6+
{
7+
public static void main(String[] args)
8+
{
9+
Scanner sc = new Scanner(System.in);
10+
System.out.println("Enter the number of rows");
11+
int rows = sc.nextInt();
12+
System.out.println("Enter the number of columns");
13+
int columns= sc.nextInt();
14+
int[][] arr2d = new int[rows][columns];
15+
16+
for(int i=0;i<rows;i++)
17+
{
18+
for(int j=0;j<columns;j++)
19+
{
20+
System.out.println("Enter the element at "+ i + "th row and "+ j +"th columns");
21+
arr2d[i][j] = sc.nextInt();
22+
}
23+
}
24+
25+
for(int i=0;i<rows;i++)
26+
{
27+
for(int j=0;j<columns;j++)
28+
{
29+
System.out.print(arr2d[i][j]+ " ");
30+
}
31+
System.out.println();
32+
33+
}
34+
}
35+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*For a given two-dimensional integer array/list of size (N x M), you need to find out which row or column has the largest sum(sum of all the elements in a row/column) amongst all the rows and columns.*/
2+
3+
import java.util.Scanner;
4+
5+
public class LargestRowOrColumn
6+
{
7+
public static void Largest_Element(int arr[][])
8+
{
9+
if(arr.length==0)
10+
{
11+
System.out.print("row "+"0 "+Integer.MIN_VALUE);
12+
return;
13+
}
14+
15+
int i, j, sum;
16+
int rows=arr.length;
17+
int cols=arr[0].length;
18+
19+
int max_val_row = Integer.MIN_VALUE;
20+
int val_row=0;
21+
for(i=0;i<rows;i++)
22+
{
23+
sum =0;
24+
String s;
25+
for(j=0;j<cols;j++)
26+
{
27+
sum += arr[i][j];
28+
if(sum>max_val_row)
29+
{
30+
max_val_row = sum;
31+
val_row = i;
32+
}
33+
}
34+
}
35+
36+
int max_val_col = Integer.MIN_VALUE;
37+
int val_col=0;
38+
for(j=0;j<cols;j++)
39+
{
40+
sum =0;
41+
for(i=0;i<rows;i++)
42+
{
43+
sum += arr[i][j];
44+
if(sum>max_val_col)
45+
{
46+
max_val_col = sum;
47+
val_col = j;
48+
}
49+
}
50+
}
51+
52+
if(max_val_row>max_val_col)
53+
{
54+
System.out.println("row "+ val_row +" " + max_val_row);
55+
}
56+
else
57+
{
58+
System.out.println("column "+ val_col +" " + max_val_col);
59+
}
60+
}
61+
62+
public static int[][] takeInput()
63+
{
64+
Scanner sc =new Scanner(System.in);
65+
System.out.println("Enter the number of rows");
66+
int rows=sc.nextInt();
67+
System.out.println("Enter number of cols");
68+
int cols=sc.nextInt();
69+
int[][] arr=new int[rows][cols];
70+
for(int i=0;i<rows;i++)
71+
{
72+
for(int j=0;j<cols;j++)
73+
{
74+
System.out.println("Enter the element at "+ i+ "th row and "+j+"th column");
75+
arr[i][j]=sc.nextInt();
76+
}
77+
}
78+
return arr;
79+
}
80+
81+
public static void main(String args[])
82+
{
83+
int [][]input=takeInput();
84+
Largest_Element(input);
85+
}
86+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*Print Like a Wave
2+
For two-dimensional integer array/list of size (N x M), print the array/list in a sine wave order, i.e, print the first column top to bottom, next column bottom to top and so on.*/
3+
4+
import java.util.Scanner;
5+
6+
public class PrintLikeWave
7+
{
8+
public static void LikeWave(int arr[][])
9+
{
10+
if (arr.length == 0) {
11+
return;
12+
}
13+
14+
int i, j;
15+
if (arr.length == 0)
16+
{
17+
return;
18+
}
19+
20+
int rows=arr.length;
21+
int cols=arr[0].length;
22+
23+
for(j=0;j<cols;j++)
24+
{
25+
if(j%2==0)
26+
{
27+
for(i=0;i<rows;i++)
28+
{
29+
System.out.print(arr[i][j]+ " ");
30+
}
31+
}
32+
else
33+
{
34+
for(i=rows-1;i>=0;i--)
35+
{
36+
System.out.print(arr[i][j]+ " ");
37+
}
38+
}
39+
}
40+
}
41+
42+
public static int[][] takeInput()
43+
{
44+
Scanner sc=new Scanner(System.in);
45+
System.out.println("Enter the row size");
46+
int rows=sc.nextInt();
47+
System.out.println("Enter the column size");
48+
int cols=sc.nextInt();
49+
50+
int[][] arr=new int[rows][cols];
51+
for(int i=0;i<rows;i++)
52+
{
53+
for(int j=0;j<cols;j++)
54+
{
55+
System.out.println("Enter the element at "+ i+ "th row and "+j+"th column");
56+
arr[i][j]=sc.nextInt();
57+
}
58+
}
59+
return arr;
60+
}
61+
62+
public static void main(String args[])
63+
{
64+
int [][]input=takeInput();
65+
LikeWave(input);
66+
}
67+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*Print Spiral
2+
For a given two-dimensional integer array/list of size (N x M), print it in a spiral form. That is, you need to print in the order followed for every iteration:
3+
a. First row(left to right) b. Last column(top to bottom)
4+
c. Last row(right to left) d. First column(bottom to top)*/
5+
6+
import java.util.Scanner;
7+
8+
public class PrintSpiral
9+
{
10+
public static void PrintSpiral(int arr[][])
11+
{
12+
if (arr.length == 0)
13+
{
14+
return;
15+
}
16+
17+
int row = arr.length;
18+
int col = arr[0].length;
19+
if (arr.length == 0){return ;}
20+
21+
int k=1;
22+
int r1=0,r2=row-1;
23+
int c1=0,c2=col-1;
24+
while(k<=row*col)
25+
{
26+
for(int i=c1;i<=c2;i++)
27+
{
28+
System.out.print(arr[r1][i] + " ");
29+
k++;
30+
}
31+
r1++;
32+
33+
for(int i=r1;i<=r2;i++)
34+
{
35+
System.out.print(arr[i][c2] + " ");
36+
k++;
37+
}
38+
c2--;
39+
40+
for(int i=c2;i>=c1;i--)
41+
{
42+
System.out.print(arr[r2][i] + " ");
43+
k++;
44+
}
45+
r2--;
46+
47+
for(int i=r2;i>=r1;i--)
48+
{
49+
System.out.print(arr[i][c1] + " ");
50+
k++;
51+
}
52+
c1++;
53+
}
54+
}
55+
56+
public static int[][] takeInput()
57+
{
58+
Scanner s=new Scanner(System.in);
59+
System.out.println("Enter the row size");
60+
int rows=s.nextInt();
61+
System.out.println("Enter the column size");
62+
int cols=s.nextInt();
63+
64+
int[][] arr=new int[rows][cols];
65+
for(int i=0;i<rows;i++)
66+
{
67+
for(int j=0;j<cols;j++)
68+
{
69+
System.out.println("Enter the element at "+ i+ "th row and "+j+"th column");
70+
arr[i][j]=s.nextInt();
71+
}
72+
}
73+
return arr;
74+
}
75+
76+
public static void main(String args[])
77+
{
78+
int [][]input=takeInput();
79+
PrintSpiral(input);
80+
}
81+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*Row Wise Sum
2+
For two-dimensional integer array/list of size (N x M), find and print the sum of each of the row elements in a single line, separated by a single space.*/
3+
4+
import java.util.Scanner;
5+
6+
public class RowWiseSum
7+
{
8+
public static void RowSum(int arr[][])
9+
{
10+
String str = "";
11+
int rows=arr.length;
12+
int cols=arr[0].length;
13+
14+
for(int i=0;i<rows;i++)
15+
{
16+
int sum =0;
17+
for(int j=0;j<cols;j++)
18+
{
19+
sum += arr[i][j];
20+
}
21+
System.out.print(sum + " ");
22+
}
23+
}
24+
25+
public static int[][] takeInput()
26+
{
27+
Scanner sc=new Scanner(System.in);
28+
System.out.println("Enter the number of rows");
29+
int rows=sc.nextInt();
30+
System.out.println("Enter number of cols");
31+
int cols=sc.nextInt();
32+
int[][] arr=new int[rows][cols];
33+
34+
for(int i=0;i<rows;i++)
35+
{
36+
for(int j=0;j<cols;j++)
37+
{
38+
System.out.println("Enter the element at "+ i+ " row "+j+"column");
39+
arr[i][j]=sc.nextInt();
40+
}
41+
}
42+
return arr;
43+
}
44+
45+
public static void main(String args[])
46+
{
47+
int [][]input=takeInput();
48+
RowSum(input);
49+
}
50+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*For two-dimensional integer array/list of size (N x M), find and print the sum of all elements */
2+
3+
import java.util.Scanner;
4+
5+
public class SumofAllElement
6+
{
7+
public static int fun(int[][] arr2d)
8+
{
9+
int sum=0;
10+
for(int i=0;i<arr2d.length;i++)
11+
{
12+
for(int j=0;j<arr2d[i].length;j++)
13+
{
14+
sum+=arr2d[i][j];
15+
}
16+
}
17+
return sum;
18+
}
19+
20+
public static int[][] takeInput()
21+
{
22+
Scanner s=new Scanner(System.in);
23+
System.out.println("Enter the number of rows");
24+
int rows=s.nextInt();
25+
System.out.println("Enter number of cols");
26+
int cols=s.nextInt();
27+
int[][] arr=new int[rows][cols];
28+
for(int i=0;i<rows;i++)
29+
{
30+
for(int j=0;j<cols;j++)
31+
{
32+
System.out.println("Enter the element at "+ i+ " row "+j+"column");
33+
arr[i][j]=s.nextInt();
34+
}
35+
}
36+
return arr;
37+
}
38+
39+
public static void main(String[] args)
40+
{
41+
int[][] arr = takeInput();
42+
System.out.println("Sum of all element is = "+ fun(arr));
43+
}
44+
}

0 commit comments

Comments
 (0)