Results 1 to 7 of 7
 02012012, 08:02 AM #1Member
 Join Date
 Oct 2010
 Posts
 38
 Rep Power
 0
Finding Min and Max without using arrays
I'm am trying to write a program that inputs a list of numbers using a scanner and then outputs the maximum, minimum, count, sum, and average. ARRAYS MAY NOT BE USED. There are no errors in my code. It compiles and runs the way it should. The problem is finding the maximum and minimum values. I know how to do this using arrays. If I were allowed to use arrays, I would use a selection sort method to sort the numbers in the array, then the first number would be the minimum and the last number would be the maximum. The problem is I'm NOT allowed to use any arrays in my program. How would I find the minimum and maximum without using any arrays in my program??
Here is my code:
Java Code:import java.util.*; import java.lang.Math; public class Assignment1 { public static void main(String[] args) { int x; int sum = 0; int min = 0; int max = 0; double average = 0; int count = 0; Scanner input = new Scanner(System.in); System.out.print("Enter Integer Values: "); do { x = input.nextInt(); if (x != 999) { count ++; sum = sum + x; } else { break; } } while (x != 999); //{ average = (double)sum/count; for (int i = 0; i < count; i++) { //Some code to calculate maximum //Some code to calculate minimum } System.out.println("The maximum value is " + max); System.out.println("The minimum value is " + min); System.out.println("The count of how many values is " + count); System.out.println("The sum is " + sum); System.out.println("The average is " + average); //} } }
 02012012, 09:56 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
Re: Finding Min and Max without using arrays
Suppose you were already given some numbers and you know the minimum and maximum values of them; now suppose you are given another number; how would you update the minimum and maximum values (if needed)?
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 02012012, 11:26 PM #3Member
 Join Date
 Oct 2010
 Posts
 38
 Rep Power
 0
Re: Finding Min and Max without using arrays
I'll suppose I was given 5, 0, 10, 1. The minimum would be 5 and the maximum would be 10. And another number 25 is added to the list and the maximum would now be 25. I would update this using the math max function Math.max(10, 25);. I know you are suggesting that I use the math max function to compare the first two numbers, then compare the max of those 2 numbers with the third number and so on. However that is impossible to do without an array. If I were able to use arrays, I would use a for loop and then the Math max fuction to compare num[i] with the currentMax and so on. Since I can't use arrays, there is no num[i]. I honestly have no idea what to type.
 02022012, 07:39 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
Re: Finding Min and Max without using arrays
You've lost me with your last sentence; suppose I know the maximum of 100 numbers, now number 101 comes in; all I have to do is compare it with my maximum number and update it if necessary; I can use the Math.max( ... ) method for it if I want to. There is no need for an array whatsoever.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 02022012, 08:04 AM #5Member
 Join Date
 Oct 2010
 Posts
 38
 Rep Power
 0
Re: Finding Min and Max without using arrays
Well, I figured it out a few minutes ago, after spending about 3 hours randomly messing around with my code. I didn't even have to use the Math.max method.
Java Code:import java.util.*; public class Assignment1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Integer Values: "); //int max = input.nextInt(); int x = input.nextInt(); int max = x; int min = x; //int x = max; int sum = 0; double average = 0.0; int count = 0; do { count ++; sum = sum + x; x = input.nextInt(); average = (double)sum/count; if (x != 999) { if (x > max) { max = x; } else if (x < min) { min = x; } } else { break; } } while (x != 999); System.out.println("The maximum value is " + max); System.out.println("The minimum value is " + min); System.out.println("The count of how many values is " + count); System.out.println("The sum is " + sum); System.out.println("The average is " + average); } }
 02022012, 08:16 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
Re: Finding Min and Max without using arrays
cenosillicaphobia: the fear for an empty beer glass
 02022012, 08:58 AM #7Member
 Join Date
 Oct 2010
 Posts
 38
 Rep Power
 0
Similar Threads

Help with finding CLI program
By russb87 in forum Forum LobbyReplies: 0Last Post: 06242011, 05:22 PM 
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06142011, 02:28 PM 
finding average in 2d arrays
By the beginner in forum New To JavaReplies: 18Last Post: 02242011, 05:32 AM 
finding max value
By rocky86 in forum New To JavaReplies: 6Last Post: 10102010, 02:37 AM 
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03232010, 02:40 AM
Bookmarks