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);
}
}
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:
Get Answers For Free
Most questions answered within 1 hours.