Problem 2.
2.1.Make a program that allows you to create a 3 X 4 integer array. Your program will initialize the array with random numbers from 0 to 1000. Your program also will identify and display the largest value, smallest value, sum and average value of all data in the array.
1
Expert's answer
2012-03-27T12:03:33-0400
public class Array { private static int[][] mas = new int[3][4]; public static void initialize(){ Random rand = new Random(); for(int i=0;i<3;i++) for(int j=0; j<4;j++) mas[i][j]= rand.nextInt(1000); } public static void showArray(){ for(int i=0;i<3;i++){ for(int j=0; j<4;j++) System.out.print(mas[i][j]+" "); System.out.println(""); } } public static void showStats(){ int max = 0; int min = mas[0][0]; int sum = 0; for(int i=0;i<3;i++) for(int j=0;j<4;j++){ if(mas[i][j]>max) max = mas[i][j]; if(mas[i][j]<min) min = mas[i][j]; sum+=mas[i][j]; } System.out.println("Max element: "+max); System.out.println("Min element: "+min); System.out.println("Sum of elements: "+sum); System.out.println("Average: "+(sum/12)); } public static void main(String[] args){ initialize(); showArray(); showStats(); } }
Comments
Leave a comment