In: Computer Science
Minimum value and its position
Create a program that:
Run the program 5 times with representative sizes
Since you have not provided the language of your preference, I am providing the code in Java.
CODE
import java.util.Random;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m, n, minValue, maxValue;
System.out.println("Enter the value of m and n: ");
m = sc.nextInt();
n = sc.nextInt();
System.out.println("Enter the value of minValue and maxValue: ");
minValue = sc.nextInt();
maxValue = sc.nextInt();
Random rand = new Random();
int[][] arr = new int[m][n];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
arr[i][j] = rand.nextInt(maxValue - minValue + 1) + minValue;
}
}
int min = Integer.MAX_VALUE;
int minRow = -1, minColumn = -1;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (arr[i][j] < min) {
min = arr[i][j];
minRow = i;
minColumn = j;
}
}
}
System.out.println("The array is: ");
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println("The minimum value is: " + min + " which is present at location (" + minRow + ", " + minColumn + ")");
}
}