| | |
i need help with arrays
![]() |
•
•
Join Date: Apr 2005
Posts: 2
Reputation:
Solved Threads: 0
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]);
}
}
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]);
}
}
•
•
Join Date: May 2005
Posts: 55
Reputation:
Solved Threads: 1
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.
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.
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Search Method help needed!!!
- Next Thread: two Java GUI problems - please help
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api applet application arc arguments array arrays automation banking binary bluetooth byte chat chatprogramusingobjects class client code color compare component count database design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong problem producer program programming project projectideas read recursion reference replaysolutions rim scanner server set size sms sort sql string swing terminal threads transforms tree ui unicode validation web windows





