Arrays and 2D Arrays Hacks (Macro Monkeys)
void setArray(int[] arr, int n) {
n = 10;
for (int i=0; i<array.length; i++) {
arr[i]=n;
}
}
int[] array = new int[10];
setArray(array, 10);
for (int i = 0; i<array.length; i++) {
System.out.println(array[i]);
}
// Should print all 10s when working properly
//Example finding the max in an array.
//Finds the maximum in an array
public static int average(int[] array) {
int sum=0;
for (int i=0; i<array.length; i++) {
sum+=array[i];
}
return sum/array.length;
}
//tester array
int[] test = {3, 5, 7, 2, 10};
//returns 10
System.out.println(average(test));
public static double averageDiagonal (int[][] array2D) {
int sum =0;
for (int r =0; r<array2D.length; r++)
{
sum+= array2D[r][r];
}
return (double) sum/array2D.length;
}
int[][] arr = {
{1,2,3,4,5,6},
{7,8,9,10,11,12},
{0,1,2,3,4,5},
{10,11,12,13,14,15},
{15,16,17,18,19,20}
};
System.out.println(averageDiagonal(arr));