-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayOperations.java
More file actions
127 lines (108 loc) · 4.23 KB
/
ArrayOperations.java
File metadata and controls
127 lines (108 loc) · 4.23 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
import java.util.Random;
import java.util.Scanner;
public class ArrayOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user for dimensions of the 2D array
int rows = getDimension("Enter the number of rows (5-10): ", scanner, 5, 10);
int columns = getDimension("Enter the number of columns (5-10): ", scanner, 5, 10);
// Generate and populate the 2D array with random integers
int[][] array = generateArray(rows, columns);
// Display the array in table format
System.out.println("2D Array:");
printArray(array);
// Calculate and display the sum and average of the entire array
int sum = calculateSum(array);
double average = calculateAverage(array);
System.out.println("Sum of the entire array: " + sum);
System.out.println("Average of the entire array: " + average);
// Calculate and display the number of concentric rings
int concentricRings = calculateConcentricRings(rows, columns);
System.out.println("Number of concentric rings: " + concentricRings);
// Print out the contents of the outside ring
int[] outsideRing = getOutsideRing(array);
System.out.println("Contents of the outside ring:");
for (int element : outsideRing) {
System.out.print(element + " ");
}
System.out.println();
// Calculate and display the sum and average of the outside ring
int outsideRingSum = calculateSum(outsideRing);
double outsideRingAverage = calculateAverage(outsideRing);
System.out.println("Sum of the outside ring: " + outsideRingSum);
System.out.println("Average of the outside ring: " + outsideRingAverage);
}
public static int getDimension(String prompt, Scanner scanner, int min, int max) {
int dimension;
do {
System.out.print(prompt);
dimension = scanner.nextInt();
} while (dimension < min || dimension > max);
return dimension;
}
public static int[][] generateArray(int rows, int columns) {
int[][] array = new int[rows][columns];
Random random = new Random();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = random.nextInt(900) + 100; // Random integers from 1 to 1000 inclusive
}
}
return array;
}
public static void printArray(int[][] array) {
for (int[] row : array) {
for (int element : row) {
System.out.printf("%4d", element); // Adjust spacing for alignment
}
System.out.println();
}
}
public static int calculateSum(int[][] array) {
int sum = 0;
for (int[] row : array) {
for (int element : row) {
sum += element;
}
}
return sum;
}
public static double calculateAverage(int[][] array) {
int totalElements = array.length * array[0].length;
int sum = calculateSum(array);
return (double) sum / totalElements;
}
public static int calculateConcentricRings(int rows, int columns) {
return Math.min(rows, columns) / 2;
}
public static int[] getOutsideRing(int[][] array) {
int rows = array.length;
int columns = array[0].length;
int[] outsideRing = new int[2 * rows + 2 * (columns - 2)];
int index = 0;
for (int i = 0; i < columns; i++) {
outsideRing[index++] = array[0][i];
}
for (int i = 1; i < rows; i++) {
outsideRing[index++] = array[i][columns - 1];
}
for (int i = columns - 2; i >= 0; i--) {
outsideRing[index++] = array[rows - 1][i];
}
for (int i = rows - 2; i >= 1; i--) {
outsideRing[index++] = array[i][0];
}
return outsideRing;
}
public static int calculateSum(int[] array) {
int sum = 0;
for (int element : array) {
sum += element;
}
return sum;
}
public static double calculateAverage(int[] array) {
int sum = calculateSum(array);
return (double) sum / array.length;
}
}