9 Replies - 2322 Views - Last Post: 13 January 2010 - 09:36 AM Rate Topic: ***-- 2 Votes

#1 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Sorting Numbers

Post icon  Posted 13 January 2010 - 12:20 AM

I am stuck on this assignment my code is real close to the answer but i do not know how to fix it.

Lab Goal : This lab was designed to teach you more about sorting.

Lab Description : Send in a number and get back an array that contains all of the digits from the number in sorted order. 634152 would come back [1, 2, 3, 4, 5, 6]. You may not use Strings in this program.


You must use % and /. 456 % 10 = 6 456 / 10 = 45
You must use % and /. 102 % 10 = 2 102 / 10 = 10
You must use % and /. 9123 % 10 = 3 9123 / 10 = 912
You must use % and /. 9 % 10 = 9 9 / 10 = 0



Sample Data :
567891
901912468
864213507




Sample Output :
1 5 6 7 8 9
0 1 1 2 4 6 8 9 9
0 1 2 3 4 5 6 7 8

// A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;

public class NumberSort
{
	//instance variables and other methods not shown

	private static int getNumDigits(int number)
	{
		int count = 0;
		for(int i=10; i<=number; i=i*i)
		{
			count++;
			
		}
		
		return count;
	}
	
	public static int[] getSortedDigitArray(int number)
	{
		int [] a= new [count]
		for(int i=10; i<=100000; i=i*i)
		{
			a+={number/i%10};
			
		}
		
	}
}

// A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import static java.lang.System.*;

public class Lab15e
{
	public static void main(String args[])
	{
		//test case 1
		int[] one = NumberSort.getSortedDigitArray(567891);
		for(int item : one)
		{
			System.out.print(item + " ");
		}
		System.out.println();
		
		//test case 2	
		int[]two= NumberSort.getSortedDigitArray(901912468);
		for(int item : two )
		{
			out.print(item+ " ");
		}
		out.println();
		int[]three= NumberSort.getSortedDigitArray(864213507);
		for(int item : three)
		{
			out.print(item + " ");
		}	
			

	}
}

This post has been edited by hlln: 13 January 2010 - 12:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Numbers

#2 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Re: Sorting Numbers

Posted 13 January 2010 - 12:57 AM

I got everything fixed BUT when i run there is no output. WHY? SEE ATTACHED!!
Was This Post Helpful? 0
  • +
  • -

#3 0x00hex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 11-January 10

Re: Sorting Numbers

Posted 13 January 2010 - 03:29 AM

Seems you have forgotten to include "System." in your loops when printing out the numbers.
//Should be System.out.print(item + " ");
 out.print(item + " "); 


Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,757
  • Joined: 20-September 08

Re: Sorting Numbers

Posted 13 January 2010 - 04:25 AM

Make sure your NumberSort method is returning the array properly - sounds like it's empty
Was This Post Helpful? 0
  • +
  • -

#5 Simple_Condolences  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 138
  • Joined: 10-January 10

Re: Sorting Numbers

Posted 13 January 2010 - 06:32 AM

System.out.println(blah); instead of out.println(blah);

If that still doesn't fix it then you need to check your array and make sure that data is being stored though.. well, really, you'd know... you'd have an OutOfBoundsException at runtime... I dunnoooo!

- Zach
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,757
  • Joined: 20-September 08

Re: Sorting Numbers

Posted 13 January 2010 - 07:08 AM

Quote

If that still doesn't fix it then


How would it? If that were a problem, it would be a compile time problem. It isn't, since System. is imported static
Was This Post Helpful? 0
  • +
  • -

#7 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Re: Sorting Numbers

Posted 13 January 2010 - 09:15 AM

// A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;

public class NumberSort
{
	//instance variables and other methods not shown
	private static int getNumDigits(int number)
	{
		int count = 0;
		for(int i=number; i>=0; i/=10)
		{
		number=number/=10;
		count++;
		}
		return count;
	}
	
	public static int[] getSortedDigitArray(int number)
	{
		int [] a=new int [getNumDigits(number)];
		for(int i=number; i>=0; i/=10)
		{
			a[i]=number%=10;
			
		}
		Arrays.sort(a);
		return a;
	}
		
	}


// A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import static java.lang.System.*;

public class Lab15e
{
	public static void main(String args[])
	{
		//test case 1
		int[] one = NumberSort.getSortedDigitArray(567891);
		for(int item : one)
		{
			System.out.print(item + " ");
		}
		System.out.println();
		
		//test case 2	
		int[]two= NumberSort.getSortedDigitArray(901912468);
		for(int item : two )
		{
			out.print(item+ " ");
		}
		out.println();
		int[]three= NumberSort.getSortedDigitArray(864213507);
		for(int item : three)
		{
			out.print(item + " ");
		}		
	}
}


NO OUTPUT WHY?
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Sorting Numbers

Posted 13 January 2010 - 09:32 AM

you got an infinite loop in your getNumDigits method..try to find it ;)
Was This Post Helpful? 1
  • +
  • -

#9 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Re: Sorting Numbers

Posted 13 January 2010 - 09:34 AM

i can't find it all my numbers are going to stop at i>=0;
// A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;

public class NumberSort
{
	//instance variables and other methods not shown
	private static int []a;
	private static int getNumDigits(int number)
	{
		int count = 0;
		for(int i=number; i>=0; i/=10)
		{
		number/=10;
		count++;
		}
		return count;
	}
	
	public static int[] getSortedDigitArray(int number)
	{
		a=new int [getNumDigits(number)];
		for(int i=number; i>=0; i/=10)
		{
			a[i]=number%=10;
			
		}
		Arrays.sort(a);
		return a;
	}
		
	}



Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Sorting Numbers

Posted 13 January 2010 - 09:36 AM

anyway here's the fixed program

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;

public class NumberSort
{
	//instance variables and other methods not shown
	private static int getNumDigits(int number)
	{
		int count = 0;
		for(int i=number; i>0; i/=10)
		{
	
		count++;
		}
		return count;
	}
	
	public static int[] getSortedDigitArray(int number)
	{
		int [] a=new int [getNumDigits(number)];
		for(int i= 0; i < a.length; i++)
		{
			a[i]=number%10;
		number /= 10;
			
		}
		Arrays.sort(a);
		return a;
	}   
	}



look at it and try to understand the differences between my code and your code..
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1