Question

I need to get the Min and Max value of an array when a user inputs...

I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!!

_________________________________________________________________________________________________

import java.util.Scanner;

public class ArrayMenu{
static int count;
static Scanner kb = new Scanner(System.in);
  
  
  
  
public static void main(){
int item=0;
int[] numArray=new int[100];
count=0;
  
  
while (item !=8){
menu();
item = kb.nextInt();
if (item==1)
initializeArray(numArray);
else if (item==2)
printArray(numArray);
else if (item==3)
addToEnd(numArray);
/* else if (item == 4)
changeSpecificIndex(numArray);
else if (item == 5)
removeElement(numArray);
*/ else if (item == 6)
minMaxSumAvg(numArray);
/* else if (item == 7)
Search(numArray);
*/
}
  
System.out.println("Goodby!");
  
}

public static void menu(){
System.out.println("1. Initialize Array");
System.out.println("2. Display Array");
System.out.println("3. Add element to the end");
System.out.println("4. Add an element at a specific index");
System.out.println("5. Remove an element at specific index");
System.out.println("6. Show min, max, sum and average");
System.out.println("7. Search");
System.out.println("8. Exit");
System.out.println("________________________________________");
}
  
public static void initializeArray(int[] arr){
count=0;
int num;
System.out.println("Enter integer values to fill the array -value to stop: ");
do
{
num = kb.nextInt();
if (num >=0)
{
arr[count]=num;
count++;
}
} while (num > 0);
  
}
public static void printArray(int[] arr){
for (int i=0; i< count; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
  
public static void addToEnd(int[] arr){
System.out.print("Enter number to add: ");
int num = kb.nextInt();
arr[count] = num;
count++;
System.out.println();

}
  
public static void minMaxSumAvg(int[] arr){
  
int max = arr[0];
int min = Integer.MAX_VALUE;
  
for(int i=0; i<arr.length; i++ ) {
if(arr[i]>max) {
max = arr[i];
}
if(arr[i]<min){
min = arr[i];
}
}
  

  
System.out.println("Max value is: " + max);
System.out.println("Min value is: " + min);
  
}
  
  

  
  
  
  
  
  
}

Homework Answers

Answer #1

Fix:

The problem is in for loop iteration... It should iterate from 0 to count.

Statement arr.length will return 100 and all the uninitialized values in array are treated as 0. Hence you are getting min value as 0.

_______________________________________________________________________________________________________

Updated Java Program:

import java.util.Scanner;
public class ArrayMenu{
static int count;
static Scanner kb = new Scanner(System.in);
  
public static void main(String args[]){
int item=0;
int[] numArray=new int[100];
count=0;
  
  
while (item !=8){
menu();
item = kb.nextInt();
if (item==1)
initializeArray(numArray);
else if (item==2)
printArray(numArray);
else if (item==3)
addToEnd(numArray);
/* else if (item == 4)
changeSpecificIndex(numArray);
else if (item == 5)
removeElement(numArray);
*/ else if (item == 6)
minMaxSumAvg(numArray);
/* else if (item == 7)
Search(numArray);
*/
}
  
System.out.println("Goodby!");
  
}
public static void menu(){
System.out.println("1. Initialize Array");
System.out.println("2. Display Array");
System.out.println("3. Add element to the end");
System.out.println("4. Add an element at a specific index");
System.out.println("5. Remove an element at specific index");
System.out.println("6. Show min, max, sum and average");
System.out.println("7. Search");
System.out.println("8. Exit");
System.out.println("________________________________________");
}
  
public static void initializeArray(int[] arr){
count=0;
int num;
System.out.println("Enter integer values to fill the array -value to stop: ");
do
{
num = kb.nextInt();
if (num >=0)
{
arr[count]=num;
count++;
}
} while (num > 0);
  
}
public static void printArray(int[] arr){
for (int i=0; i< count; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
  
public static void addToEnd(int[] arr){
System.out.print("Enter number to add: ");
int num = kb.nextInt();
arr[count] = num;
count++;
System.out.println();

}
  
public static void minMaxSumAvg(int[] arr){
  
   int max = arr[0];
   int min = Integer.MAX_VALUE;
  
/* Loop should iterate from 0 to count */
   for(int i=0; i<count; i++ ) {
    if(arr[i]>max) {
           max = arr[i];
       }

       if(arr[i]<min){
           min = arr[i];
       }
   }

   System.out.println("Max value is: " + max);
   System.out.println("Min value is: " + min);
}
}

_______________________________________________________________________________________________________

Sample Run:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I need to come up with the pseudocode for a min and max value finder for...
I need to come up with the pseudocode for a min and max value finder for an array, uses recursion. first I started writing a java program to get a better understanding but it seams not to work properly. also Im having trouble calculating the time efficiency for the code. public class Max_min {     /**      * @param args the command line arguments      */     static int Max;     static int Min;     static int index;     static...
Please find the BigO function with explanation for each of the programs below: - Recursive binary...
Please find the BigO function with explanation for each of the programs below: - Recursive binary search - Computing the factorial of N - Computing the N-th Fibonacci number. import java.util.Scanner; public class Recursive { public static void main(String args[]) { int arr[]= {7,9,12,15,28,57,92}; Scanner in = new Scanner(System.in); int key; System.out.print("Enter key to search in array : "); key = in.nextInt(); int index = binarySearch(arr,0,arr.length-1,key); if(index==-1)System.out.println(key + " is not found in array\n"); else System.out.println(key + " is found...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Question: public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should...
Question: public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should be implemented recursively. Your method may not contain any while or for loops. Hint: the public findMinRecursive method should not, itself, be recursive. Instead, write an additional private helper method with additional parameters. This private helper method should be called from findMinRecursive. This must run in O(n) time. Here is findMin: public static <T extends Comparable<T>> int findMin(T[] arr) { int index = 0;...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial Scores: 10, 20, 30, 40 Scores after loop: 30, 50, 70, 40 Import. java.util.Scanner; public class StudentScores { public static void main (String [] args){ Scanner scnr = new Scanner (System.in); final int SCORES_SIZE = 4; int [] bonusScores = new int[SCORES_SIZE]; int...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20,...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20, 31, 40, 55, 60, 65525}; System.out.println(findPattern(array)); } private static int findPattern(int[] arr) { for (int i = 0; i < arr.length - 2; i++) { int sum = 0; for (int j = i; j < i + 2; j++) { sum += Math.abs(arr[j] - arr[j + 1]); // finding the difference } if (sum == 20) return i; } return -1; } }...
//please debug program // need Scanner output import java.util.Scanner; // Display every character between Unicode 65...
//please debug program // need Scanner output import java.util.Scanner; // Display every character between Unicode 65 and 122 // Start new line after 20 characters public class DebugSix2 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); char letter; int a; final int MIN = 65; final int MAX = 122; final int NUMPERLINE = 200; final int STOPLINE1 = 0; final int STOPLINE2 = STOPLINE1 + NUMPERLINE; for(a = MIN; a <= MAX; a++) { letter...
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...