CodeMonkey's Hacks
Hacks for Codemonkeys
public class ElementN {
public void changeArray(int[] array, int n) {
for (int i = 0; i < array.length; i++) {
array[i] = n;
}
}
public static void main(String[] args) {
ElementN elementN = new ElementN();
int[] array = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(array));
elementN.changeArray(array, 10);
System.out.println(Arrays.toString(array));
}
}
ElementN.main(null);
public class FindAverage {
public double findAverage(ArrayList<Integer> arr) {
int sum = 0;
for (int i =0; i< arr.size(); i++) {
sum += arr.get(i);
}
return (double) sum / arr.size();
}
public static void main(String[] args) {
FindAverage average = new FindAverage();
ArrayList<Integer> arr = new ArrayList<>();
arr.add(1);
arr.add(2);
arr.add(3);
System.out.println(arr);
System.out.println(average.findAverage(arr));
}
}
FindAverage.main(null);
public class AverageDiagonal {
public double findAverage(int[][] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length - i; j++) {
if (i == j) {
sum += arr[i][j];
}
}
}
return (double) sum / arr.length;
}
public static void main(String[] args) {
AverageDiagonal average = new AverageDiagonal();
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(Arrays.deepToString(arr));
System.out.println(average.findAverage(arr));
}
}
AverageDiagonal.main(null);