``````import java.io.*;

public class getLowestValue {
public static void main(String[]args) throws Exception {

DataInputStream input=new DataInputStream(System.in);

System.out.println("Get lowest value ");
int a[]=new int;
int x =0, i=0, j=0, tmp=0;

// display and get inputs 
for (x=0;x<5;x++){
System.out.print(x+" Enter a number: ");

}

// sorting
for (x=0;x<5;x++){ // loop for 5 times
i = x; // store value of x to i
for (j=x+1;j<5;j++){
if (a[j] < a[i])
i =j;
}

tmp = a[x];
a[x]=a[i];
a[i]=tmp;
}
x=0;
System.out.println(a[x]+ " is the lowest!");

}
}``````

THIS IS THE PROBLEM:
write a program using one-dimensional array that determines the lowest value among the five input values typed from the keyboard and print the difference of each, value from the lowest.
Like if the user inputs: 56421
It will display:
4
5
3
1

My problem is: I was able to get the lowest number on all the user inputs but the thing is, i scrambled the array in the processed. I arranged it into lowest to highest. My question would be how would i be able to get the lowest value on the inputs without disarranging the original sequence of inputs?

## All 11 Replies

Try to use the methods available in java to show some smart work.
It willhelp toreduce thecomplexity.

Dont try to arrange the array.Keep it intact.
Try to find the lowest number then get the diffeerences
between them without rearranging the array.

Try to use the methods available in java to show some smart work.
It willhelp toreduce thecomplexity.

Dont try to arrange the array.Keep it intact.
Try to find the lowest number then get the diffeerences
between them without rearranging the array.

Thanks for suggestion. But I just don't know another method on how I can check for the next number without disarranging the array. If you have suggestions how, I would appreciate it. Thanks though

Instead of sorting the array just to find the minimum, loop though the array continuously updating the 'min' variable. Something like:

``````int min = myArray;
for(int i = 1, limit = myArray.length; i < limit; ++i)
{
if(myArray[i] < min)
min = myArray[i];
}
System.out.println("The minimum is " + min);``````

To add to the previous suggestion by ~s.o.s~ of updating min variable -Initialize min variable with zero and do this min checking in the same loop where user provides inputs; Then have another loop to print the differences. This way you will acheive it in two loops instead of three.

> Initialize min variable with zero and do this min checking in the same loop where user provides inputs;
What happens when all the inputs are greater than 0? You end up getting zero as the minimum which is the wrong answer.

To get around that, you would have to use a modified version of the loop as:

``````int min = 0;
for(int i = 0; i < 5; ++i)
{
// accept input in 'num'
if(i == 0)
min =   num;
else if(num < min)
min = num;
}``````

Thanks everyone! I was able to make it work..thanks for the help!:)

create a program that the user allow to input a 5 integers, that looking the lowest value?

create a program that the user allow to input a 5 integers, that looking the lowest value?

Create your own thread and show effort. No one is going to do your work for you.

create a program that the user allow to input a 5 integers, that looking the lowest value?