-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.java
More file actions
161 lines (155 loc) · 2.56 KB
/
array.java
File metadata and controls
161 lines (155 loc) · 2.56 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
import java.util.Scanner;
class array
{
void add(int[][] x,int[][] y)
{
int [][] z=new int[2][2];
System.out.println("Addtion of Two Matrix :");
for(int i=0;i<x.length;i++)
{
for(int j=0;j<y.length;j++)
{
z[i][j]=x[i][j]+y[i][j];
}
}
for(int i=0;i<z.length;i++)
{
for(int j=0;j<z.length;j++)
{
System.out.print(" "+z[i][j]);
}
System.out.println();
}
}
void transpose(int[][] a,int[][] b)
{
Scanner sc=new Scanner(System.in);
int[][] c=new int[2][2];
int y;
System.out.println("Enter your choice for Transponse of Matrix 1 or 2:");
y=sc.nextInt();
if(y==1)
{
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
c[i][j]=a[j][i];
}
}
}
else
{
for(int i=0;i<b.length;i++)
{
for(int j=0;j<b.length;j++)
{
c[i][j]=b[j][i];
}
}
}
System.out.println("Tranpose of Martix : ");
for(int i=0;i<c.length;i++)
{
for(int j=0;j<c.length;j++)
{
System.out.print(" "+c[i][j]);
}
System.out.println();
}
}
void multiply(int[][] a,int[][] b)
{
int[][] c=new int[2][2];
if(a.length==b.length)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
else
{
System.out.println("Mulitply of array is not possible .");
}
System.out.println("Multiply of array is : ");
for(int i=0;i<c.length;i++)
{
for(int j=0;j<c.length;j++)
{
System.out.print(" "+c[i][j]);
}
System.out.println();
}
}
public static void main(String[] args)
{
array c=new array();
Scanner sc=new Scanner(System.in);
System.out.println("Enter First array ");
int[][] a=new int[2][2];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.println("Enter value for array");
a[i][j]=sc.nextInt();
}
}
System.out.println("Your first array: ");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println();
}
System.out.println(" Enter Second array ");
int[][] b=new int[2][2];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.println("Enter values for array ");
b[i][j]=sc.nextInt();
}
}
System.out.println("Your Second array: ");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(" "+b[i][j]);
}
System.out.println();
}
int y;
System.out.println("1. Addtion of Matrix .");
System.out.println("2. Transpose of Matrix .");
System.out.println("3. Multiply of Matrix .");
System.out.println("Enter Your Choice: ");
y=sc.nextInt();
switch(y)
{
case 1:
c.add(a,b);
break;
case 2:
c.transpose(a,b);
break;
case 3:
c.multiply(a,b);
break;
default:
System.out.println("Error!!");
break;
}
}
}