CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Sorting help

  1. #1
    Join Date
    Aug 2013
    Posts
    1

    Sorting help

    I'm trying to write a program that generates 100 random number, prints them, then sorts them, then prints out the sorted list with ten numbers in each line. I cannot figure out what is wrong with my code. Anyone have any ideas? A detailed explanation would be awesome as I am new to this. This is what I have so far.

    Code:
    public class Main {
    
    	public static void main(String[]Args)
    	{
    		int [] b;
    		b = new int[100];
    		loadArray(b);
    		printArray(b);
    		Sort(b); 
    		printArray(b);
    	}
    
    	public static void loadArray(int[] b) {
    		for(int i = 0; i < b.length; i++){
    			b[i]= (int)((Math.random())*100)+1;{
    				
    			}
    		}
    		
    	}
    	
    	public static void printArray(int[] b){
    		System.out.println();
    		for(int i = 0; i < b.length; i++){
    			System.out.print(b[i]);
    			if((i + 1)%10 == 0)
    				System.out.println();
    		}
    	}
    	
    	public static void Sort(int [] b)
    	{
    		int Temp = 0;
    		for (int i = 0; i < b.length-1; i++)
    		{
    			for (int j = i + 1; j < b.length; j++)
    			{
    				if (b[i]> b[j])
    					{
    					Temp = b[i];
    					b[i] = b[j];
    					b[j] = Temp;
    					}
    			}
    			}
    			
    		}
    	
    
    }
    And here is an example of what it prints out right now.

    42629035334048469325
    21775561975248917456
    9340882010067304331
    5542393117952289996
    3775521286392295798
    83166760847191818556
    696337363376546310037
    99637451574393191
    54155896825250881413
    4479358294746523

    1244455688
    9111213141516202123
    23252829293031313333
    35363737373739394040
    42434546474848505252
    52525454545556565757
    58586061626363676769
    71747475767779818283
    84858688909191919393
    9393969696979899100100

    As you can see all of the lines have more than 10 numbers in them except for the first line of the sorted list, which is also the only line that is sorted. Any help would be appreciated, thanks.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Sorting help

    Are you confusing numbers with digits? 10 is a number with 2 digits.
    Norm

  3. #3
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: Sorting help

    Your list is sorted it just doesn't look like it is because there is no spacing between numbers.

    Code:
    System.out.print(b[i]);
    /* change above line so that
    * 9111213141516202123
    * it will have spacing and be show as below
    * 9 11 12 13 14 15 16 20 21 23
    */
    System.out.print(b[i] + " ");
    You will see that the program your program is working as intended.

    Code:
    // When you initiate a local array you can initialize it right away and save a line of code.
    int [] b;
    b = new int[100];
    // Is the same as doing this int[] b = new int[100];
    
    public static void loadArray(int[] b) {
    	for(int i = 0; i < b.length; i++){
    		b[i]= (int)((Math.random())*100)+1;{ // <<< Here did you add the { by accident when posting the code?
    		// As it stands right now the code should throw an error. There is no need for it there.
    	}
    }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured