Results 1 to 7 of 7
  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool 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);
          //}
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Finding Min and Max without using arrays

    Quote Originally Posted by son012189 View Post
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default 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);
        }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Finding Min and Max without using arrays

    Quote Originally Posted by son012189 View Post
    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.
    Well done; indeed you don't need the Math.max( ... ) method but it can be convenient:

    Java Code:
    max= Math.max(x, max);
    min= Math.min(x, min);
    but not using it is a bit cheaper (no method call) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

Similar Threads

  1. Help with finding CLI program
    By russb87 in forum Forum Lobby
    Replies: 0
    Last Post: 06-24-2011, 04:22 PM
  2. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  3. finding average in 2d arrays
    By the beginner in forum New To Java
    Replies: 18
    Last Post: 02-24-2011, 04:32 AM
  4. finding max value
    By rocky86 in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 01:37 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •