import java.util.Random;
public class Project6
{
public static void main (String [] args)
{
Hero();
}
public static void Hero ()
{
int count = 0;
Random RNum = new Random ();
int[] array = new int[1000];
for(int i = 0; i < array.length; i++)
{for(int j= i+1; j <array.length; j++)
{array[i] = RNum.nextInt(1000);
System.out.println(array[i]);
if (array[i] >= 500)
count++;
if (array[i] == array[j])
System.out.print("The following numbers were duplicates" +j);
}
}
System.out.println("The array had " + count + " elements over 500");
}
}
Sorting Array in Ascending order
Page 1 of 19 Replies - 3753 Views - Last Post: 06 May 2009 - 07:26 PM
#1
Sorting Array in Ascending order
Posted 05 May 2009 - 07:46 PM
How would I print the following array in ascending order?
Replies To: Sorting Array in Ascending order
#2
Re: Sorting Array in Ascending order
Posted 05 May 2009 - 08:51 PM
My book talks about making one a min value and one a max value in a nested loop but it doesn't make much sense to me in the problem I have mainly because I don't know where to put it. Will I make that statement outside my first loop brackets?
#3
Re: Sorting Array in Ascending order
Posted 05 May 2009 - 08:53 PM
sorted ascending order or ascending based on index values?
#4
Re: Sorting Array in Ascending order
Posted 05 May 2009 - 08:56 PM
#5
Re: Sorting Array in Ascending order
Posted 05 May 2009 - 09:20 PM
sort the ascending based on index values
This is what I have and it doesn't work I get an error
1 error found:
File: C:\Documents and Settings\Tim\Desktop\javaprograms\Projects\Project6.java [line: 32]
Error: C:\Documents and Settings\Tim\Desktop\javaprograms\Projects\Project6.java:32: ')' expected
This is what I have and it doesn't work I get an error
import java.util.Random;
public class Project6
{
public static void main (String [] args)
{
Hero();
}
public static void Hero ()
{
int count = 0;
Random RNum = new Random ();
int[] array = new int[1000];
for(int i = 0; i < array.length; i++)
{for(int j= i+1; j <array.length; j++)
{array[i] = RNum.nextInt(1000);
System.out.println(array[i]);
if (array[i] >= 500)
count++;
}
}
System.out.println(Arrays.sort(array);
System.out.println("The array had " + count + " elements over 500");
}
}
1 error found:
File: C:\Documents and Settings\Tim\Desktop\javaprograms\Projects\Project6.java [line: 32]
Error: C:\Documents and Settings\Tim\Desktop\javaprograms\Projects\Project6.java:32: ')' expected
This post has been edited by tnachbauer: 05 May 2009 - 09:23 PM
#6
Re: Sorting Array in Ascending order
Posted 05 May 2009 - 09:21 PM
That's paradoxical. The array is already ordered in sequential index values. If you need it sorted, see pbl's post above and then iterate starting at [0] going to [size-1]
#7
Re: Sorting Array in Ascending order
Posted 05 May 2009 - 09:31 PM
KYA, on 5 May, 2009 - 08:21 PM, said:
That's paradoxical. The array is already ordered in sequential index values. If you need it sorted, see pbl's post above and then iterate starting at [0] going to [size-1]
Good point... at least now I know that you read my topics
It was in another post I think
int[] array = new int[1000];
int[] nb = new int[1000];
for(int i = 0; i < 1000; i++) {
array[i] = rand.nextInt(1000);
nb[array[i]]++;
}
So now
nb[0] = number of 0 in array
nb[1] = number of 1 in array
....
nb[999] = number of 999 in array
it is almost as sorted. To create a "real" sorted array 2 choices:
1) Arrays.sort(array);
2)
int k = 0;
int[] sorted = new int[1000]; // new sorted array
for(int i = 0; i < 1000; i++) { // pass through all number 0 to 999
for(j = 0; j < nb[i]; j++) { // number I had of that number
sorted[k++] = i; // store it in new slot in sorted array
}
}
#8
Re: Sorting Array in Ascending order
Posted 06 May 2009 - 11:54 AM
Ok so now I want to print array[i] outside the loop correct? But whenever I do that it tells me it can't find variable i? so what would I be printing?
import java.util.Random;
public class Project6
{
public static void main (String [] args)
{
Hero();
}
public static void Hero ()
{
int k = 0;
int count = 0;
Random RNum = new Random ();
int[] array = new int[1000];
int[] nb = new int[1000];
for(int i = 0; i < array.length; i++)
{array[i] = RNum.nextInt(1000);
nb[array[i]]++;
System.out.println(array[i]);
if (array[i] >= 500)
count++;
}
int[] sorted = new int[1000];
for(int i = 0; i < 1000; i++)
{for(int j = 0; j < nb[i]; j++)
{ sorted[k++] = i;
}
}
System.out.println("The array had " + count + " elements over 500");
}
}
This post has been edited by tnachbauer: 06 May 2009 - 11:56 AM
#10
Re: Sorting Array in Ascending order
Posted 06 May 2009 - 07:26 PM
I am impressed
Random() does a very good job when you look at the output
Random() does a very good job when you look at the output
import java.util.Random;
public class project6
{
public static void main (String [] args)
{
Hero();
}
public static void Hero ()
{
int k = 0;
int count = 0;
Random RNum = new Random ();
int[] array = new int[1000];
int[] nb = new int[1000];
for(int i = 0; i < array.length; i++) {
array[i] = RNum.nextInt(1000);
nb[array[i]]++;
System.out.println(array[i]);
if (array[i] >= 500)
count++;
}
int[] sorted = new int[1000];
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < nb[i]; j++) {
System.out.println("[" + k + "] = " + i);
sorted[k++] = i;
}
}
System.out.println("The array had " + count + " elements over 500");
}
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|