Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
2.0 kB
2
Indexable
Never
import java.util.Scanner;

public class q1 {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		System.out.println("numbers of colums and rows: ");
		int n = input.nextInt();
		int[][] numbers = new int[n][n];
		for (int row = 0; row < numbers.length; row++) {
			for (int col = 0; row < numbers[row].length; col++) {

				numbers[row][col] = (int) (Math.random() * (100 - 1) + 1);

			}
		}
		System.out.println("Sum of the array: " + sum(numbers));
		System.out.println("Average of the array: " + average(numbers));
		System.out.println("Highest of the array: " + high(numbers));
		System.out.println("Lowest of the array: " + sum(numbers));
		System.out.println("Value that occured most frequently: " + mode(numbers));

		input.close();

	}

	public static int sum(int[][] matrix) {
		int total = 0;
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				total += matrix[i][j];
			}
		}
		return total;

	}

	public static double average(int[][] matrix) {
		return sum(matrix) / (matrix.length * matrix.length) / 1.0;
	}

	public static int high(int[][] matrix) {
		int high = matrix[0][0];
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				if (matrix[i][j] > high) high = matrix[i][j];
		}
		}
		return high;
	}

	public static int low(int[][] matrix) {
		int low = matrix[0][0];
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				if(matrix[i][j] < low) low = matrix[i][j];
}}
		return low;
}
		

	public static int mode(int[][] matrix) {
			int[] count = new int[1000];
			for (int i = 0; i < matrix.length; i++) {
				for (int j = 0; i < matrix[i].length; j++) {
					int n = matrix[i][j];
					count[n-1]++;
		}
			}
int highIndex = 0;
for (int i = 0; i < matrix[i].length; i++) {
	if(count[i] > count[highIndex]) highIndex = i;
}
return highIndex + 1;
		
	}
}
Leave a Comment