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...
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,...
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 =...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
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.       ...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT