Results 1 to 11 of 11
Thread: Help Please!!!
- 03-30-2011, 01:48 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 19
- Rep Power
- 0
Help Please!!!
My program must generate random numbers between -999 and 999 and store them into an array.
After that use the insertion sort to sort what stored in the array.
Assuming number of elements are 5.
The random number generator method is working fine; also; the insertion sort method is working fine, but the problem is the result of the sorting isn't printed correctly. I couldn't figure the problem out. Can anybody help me?
The result of the code:PHP Code:public class Driver { final static int COL = 5; public static void main(String[] args) { int[] array = new int[COL]; int nums = 0; fillArray(array,nums); System.out.println(); insertionSort(array); } public static void fillArray(int[] newArray, int num) { Random nums = new Random(); for(int i = 1; i <= COL; i++) { newArray[num] = nums.nextInt(1998)-999; System.out.print(newArray[num] + " | "); } } public static void insertionSort(int[] newArray) { for (int i = 0; i < COL; i++) { for (int j = i; j > 0; j--) { if (newArray[j-1] > newArray[j]) { int temp = newArray[j]; newArray[j] = newArray[j-1]; newArray[j-1] = temp; } } } for (int i = 0; i < COL; i++) System.out.print(newArray[i] + " | "); } }
296 | 657 | 234 | -666 | 944 |
0 | 0 | 0 | 0 | 944 |
Process completed.
- 03-30-2011, 02:09 AM #2
I'm not sure where the first line ouf output is coming from.
In the fillArray method you insert the numbers at the index of "num". since its initial value is zero and it never changes you simply overwrite the value in the array and never insert values into other slots
0 0 0 0 0
generate 123
123 0 0 0 0
generate -234
-234 0 0 0 0
generate 753
753 0 0 0 0
etc
- 03-30-2011, 02:11 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
One problem here is that you started your fillArray() for loop at 1, ignoring that arrays in Java start at the index 0. So, newArray[0], since it was never filled out, is 0 by default.
Also, heck you're not even changing the index, you're just using "num" over and over again! Have it as newArray[i] and have the for loop look like this:
Java Code:for(int i=0; i<newArray.length, i++) newArray[i] = blablabla;
Last edited by Solarsonic; 03-30-2011 at 02:16 AM.
- 03-30-2011, 02:13 AM #4
- 03-30-2011, 02:18 AM #5
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-30-2011, 02:24 AM #6
Member
- Join Date
- Nov 2009
- Posts
- 19
- Rep Power
- 0
Hi, Thank you very much for your reply. ^_^
before I create this code (above), I made another one without the "fillArray" method that generate the random numbers and store them in the array:
This is the result of this code:PHP Code:import java.util.*; public class InSort { static int N = 5; public static void main(String[] args) { // generate random input int[] data = new int[N]; for (int i = 0; i < N; i++) data[i] = (int)(5*Math.random()); System.out.println("Before:"); for (int i = 0; i < N; i++) System.out.print(data[i] + " | "); insertionSort(data); } public static void insertionSort(int newData[]) { for (int i = 0; i < N; i++) { for (int j = i; j > 0; j--) { if (newData[j-1] > newData[j]) { int swap = newData[j]; newData[j] = newData[j-1]; newData[j-1] = swap; } } } System.out.println(); System.out.println("After:"); for (int i = 0; i < N; i++) System.out.print(newData[i] + " | "); } }
It prints the correct result of the sorting. The problem is that I have to make the fillArray() method.Before:
1 | 4 | 4 | 3 | 0 |
After:
0 | 1 | 3 | 4 | 4 |
Process completed.
What do you think?
- 03-30-2011, 02:27 AM #7
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-30-2011, 02:28 AM #8
Why get rid of the fillArray method?
Why are you printing the array in the insertionSort method? The task of that method is to sort the array not to print it.
Why duplicate code? Create a printArray method and call it everytime you want to print the array instead of writing another for loop.
- 03-30-2011, 02:30 AM #9
Member
- Join Date
- Nov 2009
- Posts
- 19
- Rep Power
- 0
Thank you both,
I just modified the code and now it works fine.
Best Regards,
- 03-30-2011, 02:36 AM #10
Member
- Join Date
- Nov 2009
- Posts
- 19
- Rep Power
- 0
Hi, Junky
I have steps that I have to follow, if it depends on me I will write the code in the main method, but unfortunately it doesn't.
About printing the array, I just want to print it to make sure that the code is fine and working correctly. ;P
Thanks,
- 03-30-2011, 04:07 AM #11


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks