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:

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

//}

}

}

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

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.

Re: Finding Min and Max without using arrays

Quote:

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

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.

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

}

}

Re: Finding Min and Max without using arrays

Quote:

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:

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

Re: Finding Min and Max without using arrays