Results 1 to 11 of 11

Thread: Help Please!!!

  1. #1
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default 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?

    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] + " | ");
    	}
    }
    The result of the code:

    296 | 657 | 234 | -666 | 944 |
    0 | 0 | 0 | 0 | 944 |
    Process completed.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by alihht View Post
    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?

    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] + " | ");
    		}[CODE][/CODE]
    	}
    	
    	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] + " | ");
    	}
    }
    The result of the code:
    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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Solarsonic View Post
    Your 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.
    Actually I have already explained what the problem is.

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    Actually I have already explained what the problem is.
    You just beat me to the post. I then realized what you pointed out and I wanted to do a major /facepalm :p

  6. #6
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    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:

    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] + " | ");
       }
    }
    This is the result of this code:

    Before:
    1 | 4 | 4 | 3 | 0 |
    After:
    0 | 1 | 3 | 4 | 4 |
    Process completed.
    It prints the correct result of the sorting. The problem is that I have to make the fillArray() method.

    What do you think?

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by alihht View Post
    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:

    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] + " | ");
       }
    }
    This is the result of this code:



    It prints the correct result of the sorting. The problem is that I have to make the fillArray() method.

    What do you think?
    I explained how to do the fillArray() method in my post above.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  9. #9
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    Thank you both,
    I just modified the code and now it works fine.

    Best Regards,

  10. #10
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    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,

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I'm sure none of your steps say "Do not use methods". In fact the opposite would be true. If I was marking assignments someone who used methods would get an higher mark than someone who stuck all their code in the main method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •