Repository navigation
Expand file tree
/
Copy pathTablesOperations.java
More file actions
128 lines (111 loc) · 4.1 KB
/
Copy pathTablesOperations.java
File metadata and controls
128 lines (111 loc) · 4.1 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
import java.util.Arrays;
public class TablesOperations {
public static void main(String[] args){
// demo variables
int[][] example_variables = {
{1, 2, 3, 4, 5, 6, 7},
{-5, -98, 1000, 1024, 0, 49, -15},
{0, 0, 0, 23, 25, -3, 0},
{4},
{},
{37, 37, 37},
{45, -7},
};
// performing demo
for (int i = 0; i < example_variables.length; i++) {
print(example_variables[i]);
try{
int min_value = min(example_variables[i]);
int max_value = max(example_variables[i]);
int min_value_index = minIndex(example_variables[i]);
int max_value_index = maxIndex(example_variables[i]);
int sum_of_all = sum(example_variables[i]);
int total_product = prod(example_variables[i]);
int[] smooth_tab = smooth(example_variables[i]);
System.out.println("Minimum value within the table is: " + min_value + ".");
System.out.println("Maximum value within the table is: " + max_value + ".");
System.out.println("Minimum value index is: " + min_value_index + ".");
System.out.println("Maximum value index is: " + max_value_index + ".");
System.out.println("Sum of all table values is: " + sum_of_all + ".");
System.out.println("Product of all table values is: " + total_product + ".");
System.out.println("Performing Smooth results in: ");
print(smooth_tab);
} catch(ArrayIndexOutOfBoundsException e){
System.err.println("Cannot perform demo - The table is empty.");
}
}
}
private static void print(int[] table){
System.out.println(Arrays.toString(table));
}
private static int min(int[] table){
int min = table[0];
for(int i = 0; i < table.length; i++) {
if (table[i] < min) {
min = table[i];
}
}
return min;
}
private static int max(int[] table) {
int max = table[0];
for (int i = 0; i < table.length; i++) {
if (table[i] > max) {
max = table[i];
}
}
return max;
}
private static int minIndex(int[] table){
int min_value_index = 0;
for(int i = 0; i < table.length; i++){
if(table[i]<table[min_value_index]){
min_value_index = i;
}
}
return min_value_index;
}
private static int maxIndex(int[] table){
int max_value_index = 0;
for(int i = 0; i < table.length; i++){
if(table[i]>table[max_value_index]){
max_value_index = i;
}
}
return max_value_index;
}
private static int sum(int[] table){
int sum = 0;
for(int i = 0; i < table.length; i++){
sum = sum + table[i];
}
return sum;
}
private static int prod(int[] table){
int product = 1;
for(int i = 0; i < table.length; i++){
product = product * table[i];
}
return product;
}
private static int[] smooth(int[] table){
int[] new_table = new int[table.length];
if (table.length == 1) {
new_table = table;
} else if (table.length == 2) {
new_table[0] = (table[0] + table[1])/2;
new_table[1] = new_table[0];
} else {
for (int i = 0; i < table.length; i++) {
if (i == 0) {
new_table[i] = (table[i] + table[i + 1])/2;
} else if (i == table.length - 1) {
new_table[i] = (table[i - 1] + table[i])/2;
} else {
new_table[i] = (table[i - 1] + table[i] + table[i + 1])/3;
}
}
}
return new_table;
}
}