Skip to content

Commit 499800e

Browse files
committed
Tasks 2.1, 2.2 added
1 parent fca1930 commit 499800e

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

‎Answers/src/Task2_1.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Scanner;
2+
3+
public class Task2_1 {
4+
5+
// Дана матрица. Вывести на экран все нечетные столбцы, у которых первый элемент
6+
// больше последнего.
7+
8+
public static void main(String[] args) {
9+
10+
try (Scanner scan = new Scanner(System.in)) {
11+
System.out.println("Enter number of matrix rows: ");
12+
int row = scan.nextInt();
13+
System.out.println("Enter number of matrix columns: ");
14+
int column = scan.nextInt();
15+
int columnCount = 0;
16+
int matrix[][] = new int[row][column];
17+
18+
System.out.println("Insert matrix elements:");
19+
20+
for (int i = 0; i < row; i++) {
21+
22+
for (int j = 0; j < column; j++) {
23+
matrix[i][j] = scan.nextInt();
24+
}
25+
}
26+
27+
for (int i = 0; i < column; i += 2) {
28+
if (matrix[0][i] > matrix[row - 1][i]) {
29+
columnCount++;
30+
if (columnCount == 1) {
31+
System.out.println();
32+
System.out.println("Your answer: ");
33+
System.out.println();
34+
}
35+
System.out.println("column " + (i + 1));
36+
37+
for (int j = 0; j < row; j++) {
38+
System.out.println(matrix[j][i]);
39+
}
40+
System.out.println();
41+
}
42+
}
43+
if (columnCount == 0) {
44+
System.out.println();
45+
System.out.println("There is not any matching columns");
46+
}
47+
}
48+
}
49+
}

‎Answers/src/Task2_2.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.Scanner;
2+
3+
public class Task2_2 {
4+
5+
// Дана матрица. Вывести на экран все нечетные столбцы, у которых первый элемент
6+
// больше последнего.
7+
8+
public static void main(String[] args) {
9+
10+
try (Scanner scan = new Scanner(System.in)) {
11+
System.out.println("Enter side size of a square matrix: ");
12+
int size = scan.nextInt();
13+
14+
int matrix[][] = new int[size][size];
15+
16+
System.out.println("Insert matrix elements:");
17+
18+
for (int i = 0; i < size; i++) {
19+
20+
for (int j = 0; j < size; j++) {
21+
matrix[i][j] = scan.nextInt();
22+
}
23+
}
24+
25+
System.out.println();
26+
System.out.print("The first diagonal: ");
27+
28+
for (int i = 0; i < size; i++) {
29+
System.out.print(matrix[i][i] + " ");
30+
}
31+
System.out.println();
32+
System.out.print("The second diagonal: ");
33+
34+
for (int i = 0; i < size; i++) {
35+
System.out.print(matrix[i][size - 1 - i] + " ");
36+
}
37+
}
38+
39+
}
40+
}

0 commit comments

Comments
 (0)