Question

In: Computer Science

Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and...

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:

Min miles: -10
Max miles: 40

import java.util.Scanner;

public class ArraysKeyValue {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_ROWS = 2;
final int NUM_COLS = 2;
int [][] milesTracker = new int[NUM_ROWS][NUM_COLS];
int i;
int j;
int maxMiles; // Assign with first element in milesTracker before loop
int minMiles; // Assign with first element in milesTracker before loop

for (i = 0; i < milesTracker.length; i++){
for (j = 0; j < milesTracker[i].length; j++){
milesTracker[i][j] = scnr.nextInt();
}
}

/* your solution goes here*/
  
minMiles = milesTracker[0][0];
maxMiles = milesTracker[0][0];
for (i=0; i<NUM_ROWS; ++i) {
for(j=0; j< NUM_COLS; ++j) {
if (milesTracker[i][j]> maxMiles)
maxMiles= milesTracker[i][j];
if (milesTracker[i][j]< minMiles)
minMiles= milesTracker[i][i];
}
}
/* Your solution goes here */

System.out.println("Min miles: " + minMiles);
System.out.println("Max miles: " + maxMiles);
}
}

when using the inputs -10 20 30 40 the code works fine, but when its run with different inputs the code can't seem the recognize the Min Miles (ie, 73 0 50 12, the code is suggesting 12 is the minimum output

Solutions

Expert Solution

import java.util.Scanner;

public class ArraysKeyValue {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        final int NUM_ROWS = 2;
        final int NUM_COLS = 2;
        int[][] milesTracker = new int[NUM_ROWS][NUM_COLS];
        int i;
        int j;
        int maxMiles; // Assign with first element in milesTracker before loop
        int minMiles; // Assign with first element in milesTracker before loop

        for (i = 0; i < milesTracker.length; i++) {
            for (j = 0; j < milesTracker[i].length; j++) {
                milesTracker[i][j] = scnr.nextInt();
            }
        }

        minMiles = milesTracker[0][0];
        maxMiles = milesTracker[0][0];
        for (i = 0; i < NUM_ROWS; ++i) {
            for (j = 0; j < NUM_COLS; ++j) {
                if (milesTracker[i][j] > maxMiles) maxMiles = milesTracker[i][j];
                if (milesTracker[i][j] < minMiles) minMiles = milesTracker[i][j];
            }
        }

        System.out.println("Min miles: " + minMiles);
        System.out.println("Max miles: " + maxMiles);
    }
}


Related Solutions

(in java) Find the maximum value and minimum value in milesTracker. Assign the maximum value to...
(in java) 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: Min miles: -10 Max miles: 40 given code below (please bold the solution, thank you!) import java.util.Scanner; public class ArraysKeyValue { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_ROWS = 2; final int NUM_COLS = 2; int [][] milesTracker = new int[NUM_ROWS][NUM_COLS]; int...
Find the relative maximum and minimum value if the following one variable function (If there is...
Find the relative maximum and minimum value if the following one variable function (If there is any): a) y = x^2 + 2x + 1 b) y = 6x^2 - 12x c) y = x^2 + 2x + 1 d) y = x^4
Find the absolute maximum value and absolute minimum value, if any, of the function g(x) =...
Find the absolute maximum value and absolute minimum value, if any, of the function g(x) = x √(4 − x2) on the interval [−2, 2].
1. Find the absolute minimum and maximum value of f(x) = x4 − 18x 2 +...
1. Find the absolute minimum and maximum value of f(x) = x4 − 18x 2 + 7 (in coordinate form) on [-1,4] 2. If f(x) = x3 − 6x 2 − 15x + 3 discuss whether there are any absolute minima or maxima on the interval (2,∞) show work please
Find the intervals of increase and decrease, find the local maximum and minimum values, find the...
Find the intervals of increase and decrease, find the local maximum and minimum values, find the intervals of concave up and concave down, find the inflection points and sketch the graph f(deta) = 2cos(deta)+cos^2(deta), 0<=deta<=2pi
This extreme value problem has a solution with both a maximum value and a minimum value....
This extreme value problem has a solution with both a maximum value and a minimum value. Use Lagrange multipliers to find the extreme values of the function subject to the given constraint. f(x, y) = x2 − y2;    x2 + y2 = 16
Find the absolute maximum and absolute minimum values of the function, if they exist, on the...
Find the absolute maximum and absolute minimum values of the function, if they exist, on the indicated interval. 6) f(x) = x 4 - 32x 2 + 2; [-5, 5]
find the absolute minimum and absolutely maximum value of function f(x,y)= e^xy subject to the constraint...
find the absolute minimum and absolutely maximum value of function f(x,y)= e^xy subject to the constraint x^2+y^2= 18
Find the absolute maximum value and the absolute minimum value of the function f(x,y) = (1+x^2)(1−y^2)...
Find the absolute maximum value and the absolute minimum value of the function f(x,y) = (1+x^2)(1−y^2) on the disk D = {(x,y) | x2+y2⩽1}?
Find the absolute maximum and​ minimum, if either​ exists, for the function on the indicated interval....
Find the absolute maximum and​ minimum, if either​ exists, for the function on the indicated interval. f(x)=x^4+4x^3-7 (A) [-2,2] (B) [-4,0] (C) [-2,1]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT