943,972 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4350
  • Java RSS
Apr 4th, 2005
0

i need help with arrays

Expand Post »
Can someone please help me with this program? it is supposed to ask user to input ten integers which are part of an array and then output that array and then output the numbers in ascending and descending order in an array. can you repair this program? thanks.
import java.io.*;

public class Elements
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));

public static void main (String[] args) throws IOException
{
int[] input = new int[10];

System.out.println("Enter ten numbers each on a different line: ");

for(int i = 0; i < 10; i++)
{
input[i] = Integer.parseInt(keyboard.readLine());
}
System.out.print("Your numbers are: ");
for (int i = 0; i < 9; i++)
{
System.out.print(input[i] + ", ");
}
System.out.println(input[9] + ".");

int sort;
int unsort;
int Largest = -1;
int i;

for(sort = 0; sort < 10; sort++)
{
for(unsort = 0; unsort < 10; unsort++)
{
if(input[unsort] > Largest)
{
Largest = input[unsort];
i = unsort;
}
}
int[] NewAry = new int[10];
i = 0;
NewAry[sort] = Largest;
input[i] = -1;
Largest = -1;
}
System.out.println("Your numbers in descending order are: " + NewAry[sort]);
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bluesmiley is offline Offline
2 posts
since Apr 2005
May 17th, 2005
0

Re: i need help with arrays

Everything looks good except for the sorting code:

for(sort = 0; sort < 10; sort++)
{
for(unsort = 0; unsort < 10; unsort++)
{
if(input[unsort] > Largest)
{
Largest = input[unsort];
i = unsort;
}
}

It seems to me that you are trying to use Selection Sort. Selection Sort works by finding the max and putting it at the end. Finding the second max and putting it and the second last slot and so on. Since you must write it in descending and ascending it is easier to use bubble sort (you'll see why). The following code sorts ascending.

boolean sorted=false;

//while not sorted repeat
while(!sorted)
{
//assume its sorted
sorted = true;

//make a pass thru the array
for(int i =0; i < 9; i++)
{
//get two consecutive slots
int first = input[i];
int second = input[i+1];

//if out of order, swap
if(first > second)
{
input[i+1] = first;
input[i]= second;

//KNOW ITS NOT SORTED!! so mark it as not sorted
sorted = false;
}
}
}

To sort descending, swap > with <. THATS IT!

For more programming help, visit www.NeedProgrammingHelp.com or email me at NeedProgrammingHelp@hotmail.com

Alex.
NPH
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
NPH is offline Offline
55 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Search Method help needed!!!
Next Thread in Java Forum Timeline: two Java GUI problems - please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC