i need help with arrays

Reply

Join Date: Apr 2005
Posts: 2
Reputation: bluesmiley is an unknown quantity at this point 
Solved Threads: 0
bluesmiley bluesmiley is offline Offline
Newbie Poster

i need help with arrays

 
0
  #1
Apr 4th, 2005
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]);
}
}
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 55
Reputation: NPH is an unknown quantity at this point 
Solved Threads: 1
NPH NPH is offline Offline
Junior Poster in Training

Re: i need help with arrays

 
0
  #2
May 17th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC