help plz in simple program

Page 1 of 1

4 Replies - 643 Views - Last Post: 29 September 2009 - 01:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=128638&amp;s=1f125594d5b6da6a8f1f996f8f74c7be&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 EEstar

Reputation: 0
• Posts: 15
• Joined: 06-May 09

help plz in simple program

Posted 28 September 2009 - 12:20 PM

if we have enterd three integers, let's say 1, 2, 3

how can i get the ordered form of these numbers based on their values

i tried some thing like this, but it didn't work: assuming x=3, y =2, z= 1

if(x>y && y>z)
system.out.println(x , y, z);
??????????

Also, i need to the get the square root of the averge which i already got:

i tried something like this :

double rt = Math.sqrt(average); ?????????????
can i anyone help me with this plz????

Is This A Good Question/Topic? 0

Replies To: help plz in simple program

#2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

Re: help plz in simple program

Posted 28 September 2009 - 12:48 PM

Feed your input values into an array, then sort it. You can write your own sorting routine or you can use the Arrays.sort() method. Let me demonstrate:

Scanner scan = new Scanner(System.in);
int[] input = new int[3];
double average = 0.0;
System.out.println("Enter three integers separated by spaces. Press enter when you are finished");
for(int i = 0; i < input.length && scan.hasNextInt(); i++){
input[i] = scan.nextInt();
average += input[i];
}
average /= input.length; //divide the average by the number of elements

Arrays.sort(input); //remember to import java.util.Arrays to access this method

From here, you can write a for-loop to print each individual element in the input array, much in the same way that I got the input. And your use of the sqrt() method is correct.

#3 EEstar

Reputation: 0
• Posts: 15
• Joined: 06-May 09

Re: help plz in simple program

Posted 28 September 2009 - 01:19 PM

thanks but still i need to get the non-decreasing order of the three integers ..

i don't know if i should use if statement to compare the integers or not...

any other ideas guys ..

macosxnerd101, on 28 Sep, 2009 - 11:48 AM, said:

Feed your input values into an array, then sort it. You can write your own sorting routine or you can use the Arrays.sort() method. Let me demonstrate:

Scanner scan = new Scanner(System.in);
int[] input = new int[3];
double average = 0.0;
System.out.println("Enter three integers separated by spaces. Press enter when you are finished");
for(int i = 0; i < input.length && scan.hasNextInt(); i++){
input[i] = scan.nextInt();
average += input[i];
}
average /= input.length; //divide the average by the number of elements

Arrays.sort(input); //remember to import java.util.Arrays to access this method

From here, you can write a for-loop to print each individual element in the input array, much in the same way that I got the input. And your use of the sqrt() method is correct.

#4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

Re: help plz in simple program

Posted 28 September 2009 - 01:28 PM

The Arrays.sort() method sorts the elements in ascending order. So to print the smallest, you need to access the input[0] element. Likewise for the largest element (input[2]), leaving the middle sized element at input[1]. You can use a loop to print them or access them individually.

#5 AntonWebsters

• D.I.C Regular

Reputation: 89
• Posts: 428
• Joined: 15-August 09

Re: help plz in simple program

Posted 29 September 2009 - 01:46 AM

Are you trying to sort the numbers? You can use Bubble sort or any kinds of sort...or you can take Mr. Macosxnerd101's advice, put the numbers into an array, and then use Arrays.sort() method to sort it. And I don't see anything wrong with your attempt to get the square root of the average of the three values.