# Thread: Finding Min and Max without using arrays

1. Member
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);
//}
}
}```

2. ## 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

3. Member
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.

4. ## Re: Finding Min and Max without using arrays

Originally Posted by son012189
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

5. Member
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);
}
}```

6. ## Re: Finding Min and Max without using arrays

Originally Posted by son012189
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

7. Member
Join Date
Oct 2010
Posts
38
Rep Power
0

## Re: Finding Min and Max without using arrays

Thanks man.

#### Posting Permissions

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