More Related Content
Similar to WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-2D_Program_1.1.pptx
Similar to WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-2D_Program_1.1.pptx (17)
More from MaruMengesha (10)
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-2D_Program_1.1.pptx
- 3. Sample Input: Sample Output:
3 8
8 11
Question 1
Write a Java code to add the given two matrices.
row = 2
column = 2
m1[][]
1 3
7 8
m2[][]
2 5
1 3
- 4. import java.util.Scanner;
public class Main{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int column = sc.nextInt();
int m1[][] = new int[row][column];
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
m1[i][j] = sc.nextInt();
}
}
int m2[][] = new int[row][column];
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
m2[i][j] = sc.nextInt();
}
}
int sum[][] = new int[row][column];
addition(m1, m2, sum);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 5. public static void addition(int m1[][], int m2[][], int sum[][])
{
for(int i = 0; i <= sum.length - 1; i++)
{
for(int j = 0; j <= sum[i].length - 1; j++)
{
sum[i][j] = m1[i][j] + m2[i][j];
}
}
for(int i = 0; i <= sum.length - 1; i++)
{
for(int j = 0; j <= sum[i].length - 1; j++)
{
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 6. Question 2
Write a Java code to find the transpose of a given matrix.
Sample Input:
r = 2
c = 3
matrix[][]
1 3 7
5 4 9
Sample Output:
1 5
3 4
7 9
- 7. import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int r = in.nextInt();
int c = in.nextInt();
int matrix[][] = new int[r][c];
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
matrix[i][j] = in.nextInt();
}
}
int transpose[][] = new int[c][r];
for(int i = 0; i <= r-1; i++)
{
for(int j = 0; j <= c-1; j++)
{
transpose[j][i] = matrix[i][j];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 8. for(int i = 0; i <= c-1; i++)
{
for(int j = 0; j <= r-1; j++)
{
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 9. Sample Input: Sample Output:
5 14
22 59
Question 3
r1 = 2, c1 = 2
a[][]
1 3
7 8
r2 = 2, c2 = 2
b[][]
2 5
1 3
Write a Java code to multiply two matrices.
- 10. import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int r1 = in.nextInt();
int c1 = in.nextInt();
int a[][] = new int[r1][c1];
for(int i = 0; i < r1; i++){
for(int j = 0; j < c1; j++){
a[i][j] = in.nextInt();
}
}
int r2 = in.nextInt();
int c2 = in.nextInt();
int b[][] = new int[r2][c2];
for(int i = 0; i < r2; i++){
for(int j = 0; j < c2; j++){
b[i][j] = in.nextInt();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 11. if(c1 != r2){
System.out.print("Invalid Input");
return;
}
int result[][] = new int[r1][c2];
multiplication(a, b, result);
for(int i = 0; i < r1; i++){
for(int j = 0; j < c2; j++){
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 12. public static void multiplication(int a[][], int b[][], int result[][])
{
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < b[i].length; j++)
{
for(int k = 0; k < a[i].length; k++)
{
result[i][j] = result[i][j] + a[i][k] * b[k][j];
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 13. Sample Input: Sample Output:
Question 4
3
America
Java
Alpha
Write a Java code to print the words which starts with a vowel.
America
Alpha
- 14. import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int col_size = sc.nextInt();
String[][] str = new String[1][col_size];
for(int col_idx = 0; col_idx < col_size; col_idx++)
{
str[0][col_idx] = sc.next();
}
for(int col_idx = 0; col_idx < col_size; col_idx++)
{
char c = str[0][col_idx].charAt(0);
if(c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o'
|| c=='O' || c=='u' || c=='U')
System.out.println(str[0][col_idx]);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Editor's Notes
- 1st slide (Mandatory)
- Input:
r1 = 2, c1 = 2
a[][]
8 9
4 5
r2 =1, c2 =3
b[][]
1 2 3
Output:
Invalid Input
Description:
As per matrix multiplication rule c1 should be equal to r2.
- Thank you slide