IN JAVA Task: Find the maximum value and minimum value in milesTracker. Assign t
ID: 3888530 • Letter: I
Question
IN JAVA
Task: Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program:
Given Code:
import java.util.Scanner;
public class ArraysKeyValue {
public static void main (String [] args) {
final int NUM_ROWS = 2;
final int NUM_COLS = 2;
int [][] milesTracker = new int[NUM_ROWS][NUM_COLS];
int i = 0;
int j = 0;
int maxMiles = 0; // Assign with first element in milesTracker before loop
int minMiles = 0; // Assign with first element in milesTracker before loop
milesTracker[0][0] = -10;
milesTracker[0][1] = 20;
milesTracker[1][0] = 30;
milesTracker[1][1] = 40;
/* Your solution goes here */
System.out.println("Min miles: " + minMiles);
System.out.println("Max miles: " + maxMiles);
}
}
Explanation / Answer
so here is the program in java
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main(String[] args) {
final int NUM_ROWS = 2;
final int NUM_COLS = 2;
int [][] milesTracker = new int[NUM_ROWS][NUM_COLS];
int i = 0;
int j = 0;
int maxMiles = 0; // Assign with first element in milesTracker before loop
int minMiles = 0; // Assign with first element in milesTracker before loop
milesTracker[0][0] = -10;
milesTracker[0][1] = 20;
milesTracker[1][0] = 30;
milesTracker[1][1] = 40;
minMiles = getMinValue(milesTracker);
maxMiles= getMaxValue(milesTracker);
System.out.println("Min miles: " + minMiles);
System.out.println("Max miles: " + maxMiles);
}
public static int getMaxValue(int[][] milesTracker) {
int maxValue = milesTracker[0][0];
for (int j = 0; j < milesTracker.length; j++) {
for (int i = 0; i < milesTracker[j].length; i++) {
if (milesTracker[j][i] > maxValue) {
maxValue = milesTracker[j][i];
}
}
}
return maxValue;
}
public static int getMinValue(int[][] milesTracker) {
int minValue = milesTracker[0][0];
for (int j = 0; j < milesTracker.length; j++) {
for (int i = 0; i < milesTracker[j].length; i++) {
if (milesTracker[j][i] < minValue ) {
minValue = milesTracker[j][i];
}
}
}
return minValue ;
}
}
here i have created 2 functions which is taking the 2 dim array as input and returning the max and min value as output.
for the function getMinValue(int[][] milesTracker) HERE i have assigned the 2 dim array 0,0 element as min value of array and apply a for loop to its row and column and find whether the if (milesTracker[j][i] < minValue ) then assign the minValue = milesTracker[j][i]; and when loop finishes it will return the min value from 2 dim array
and same logic i have applied for finding the max value from array.
for the function getMaxValue(int[][] milesTracker) HERE i have assigned the 2 dim array 0,0 element as max value of array and apply a for loop to its row and column and find whether the if (milesTracker[j][i] > maxValue) then assign the maxValue = milesTracker[j][i]; and when loop finishes it will return the maximum value from 2 dim array