-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwodarray.java
More file actions
173 lines (159 loc) · 5.08 KB
/
Copy pathtwodarray.java
File metadata and controls
173 lines (159 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package datastructures;
import java.util.*;
public class twodarray {
static int rows1;
static int cols1;
static int rows2,cols2;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("Enter rows and columns:");
rows1=sc.nextInt();
cols1=sc.nextInt();
System.out.println("Enter 2D Array in matrix order:");
int a[][]=new int[rows1][cols1];
for(int i=0;i<rows1;i++)
{
for(int j=0;j<cols1;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Select option You want to perform");
System.out.println("1. Addition of 2-D Matrix ");
System.out.println("2. Multiplication of 2-D matrix");
System.out.println("3. Transponse");
System.out.println("4. Sum of Upper Triangular Matrix");
System.out.println("5. Sum of lower Triangular Matrix");
System.out.println("6. Sum of all rows");
System.out.println("7. Sum of all columns");
System.out.println("8.Check identity matrix or not");
System.out.println("Enter your Choice");
int choice=sc.nextInt();
if(choice==1)
{
System.out.println("Enter rows and columns:");
rows2=sc.nextInt();
cols2=sc.nextInt();
System.out.println("Enter 2D Array in matrix order to add:");
int b[][]=new int[rows2][cols2];
for(int i=0;i<rows2;i++)
{
for(int j=0;j<cols2;j++)
{
b[i][j]=sc.nextInt();
}
}
if(rows1==rows2 && cols2==cols2)
{
addmatrix(a,b);
}
else
{
System.out.println("Cannot add ,not a square matrix");
}
}
if(choice==2)
{
System.out.println("Enter rows and columns for second matrix:");
rows2=sc.nextInt();
cols2=sc.nextInt();
System.out.println("Enter 2D Array in matrix order to multiply:");
int b[][]=new int[rows2][cols2];
for(int i=0;i<rows2;i++)
{
for(int j=0;j<cols2;j++)
{
b[i][j]=sc.nextInt();
}
}
if(rows1==cols2)
multiplication(a,b);
else
System.out.println("Cannot multiply");
}
if(choice==3)
{
transpose(a);
}
if(choice==4)
{
uppertriangular(a);
}
if(choice==5)
{
lowertriangular(a);
}
if(choice==6)
{
sumofrows(a);
}
if(choice==7)
{
sumofcol(a);
}
if(choice==8)
{
identity(a);
}
System.out.println("Do want to continue with operations. Press 0 to end and 1 to continue");
int terminate=sc.nextInt();
if(terminate==0)
return;
if(terminate==1)
continue;
}
}
public static void addmatrix(int a[][],int b[][])
{
int c[][]=new int[rows1][cols1];
for(int i=0;i<rows1;i++)
{
for(int j=0;j<cols1;j++)
c[i][j]=a[i][j]+b[i][j];
}
for(int i=0;i<rows1;i++)
{
for(int j=0;j<cols1;j++)
System.out.print(c[i][j]+" ");
System.out.println();
}
}
public static void multiplication(int a[][],int b[][])
{
}
public static void transpose(int a[][])
{
}
public static void uppertriangular(int a[][])
{
}
public static void lowertriangular(int a[][])
{
}
public static void sumofrows(int a[][])
{
int sum=0;
for(int i=0;i<rows1;i++)
{
for(int j=0;j<cols1;j++)
sum=sum+a[i][j];
}
System.out.println("Sum of all rows: "+sum);
}
public static void sumofcol(int a[][])
{
int sum=0;
for(int i=0;i<cols1;i++)
{
for(int j=0;j<rows1;j++)
sum=sum+a[i][j];
}
System.out.println("Sum of all columns: "+sum);
}
public static void identity(int a[][])
{
}
}