Lab Description : Java 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.

//I realy need help//

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

Files Needed ::

NumberSort.java

Lab15e.java

## 8 Replies - 1037 Views - Last Post: 19 February 2013 - 06:23 PM

### #1

# enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 10:01 AM

##
**Replies To:** enter a number and return an array that contains all of the digits

### #2

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 10:07 AM

Use the fact that the reversal digit by digit evaluation of an integer requires

digit[0]=int_number % 10

Then to get the next digit

int_number=(int_number-digit[0])/ 10

all the way upto the digit[n]=0

then you'll have the digits in reverse order from digit[0]...digit[n-1]

digit[0]=int_number % 10

Then to get the next digit

int_number=(int_number-digit[0])/ 10

all the way upto the digit[n]=0

then you'll have the digits in reverse order from digit[0]...digit[n-1]

### #3

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 10:28 AM

I think you mean

digit[0] = num % 10;

num /= 10;

/>

The tricky part is to determine the size of the int[] array

without using String

That should do it but sure there is a better way

digit[0] = num % 10;

num /= 10;

/>

The tricky part is to determine the size of the int[] array

without using String

That should do it but sure there is a better way

int num = 213546; int size = 0; int numCopy = num; while(numCopy != 0) { ++size; numCopy /= 10; } int[] digit = new int[size]; for(int i = 0; i < size; ++i) { digit[i] = num % 10; num /= 10; } Arrays.sort(digit);

This post has been edited by **pbl**: 19 February 2013 - 12:24 PM

Reason for edit:: Yes = not - thanks to burakarltr

### #4

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 10:34 AM

pbl, on 19 February 2013 - 12:28 PM, said:

I think you mean

digit[0] - num % 10;

num /= 10;

/>

digit[0] - num % 10;

num /= 10;

/>

Or perhaps

digit[0] = num % 10; num /= 10;

Quote

The tricky part is to determine the size of the int[] array

without using String

without using String

Use the log base 10 of n.

log base 10 1 = 0

log base 10 10 = 1

log base 10 100 = 2

etc.

### #5

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 11:36 AM

Quote

The tricky part is to determine the size of the int[] array

without using String

That should do it but sure there is a better way

without using String

That should do it but sure there is a better way

You could always do

int size = (int)Math.ceil(Math.log10(num)); size = (size == 0)? 1 : size;

Sorry - only just now noticed jon's comment

This post has been edited by **g00se**: 19 February 2013 - 11:43 AM

Reason for edit:: Clarification

### #6

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 11:51 AM

It's a rare day when I get to catch someone out on a math detail.

ceil is going to fail on powers of 10.

gives 1 for 10, 2 for 100, etc. Better to do

ceil is going to fail on powers of 10.

int size = (int)Math.ceil(Math.log10(num));

gives 1 for 10, 2 for 100, etc. Better to do

int size = 1+ (int)(Math.log10(num));

### #7

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 04:23 PM

How about these for some more references?:

http://stackoverflow...seperate-digits

http://stackoverflow...f-an-int-number

http://www.coderanch...-Digits-Integer

or our very own DIC archive: http://www.dreaminco...ber-into-parts/

http://stackoverflow...seperate-digits

http://stackoverflow...f-an-int-number

http://www.coderanch...-Digits-Integer

or our very own DIC archive: http://www.dreaminco...ber-into-parts/

This post has been edited by **k3y**: 19 February 2013 - 04:25 PM

### #8

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 04:31 PM

Quote

ceil is going to fail on powers of 10.

### #9

## Re: enter a number and return an array that contains all of the digits

Posted 19 February 2013 - 06:23 PM

You Can Find an Exemplary Implementation here .

Spoiler

This post has been edited by **jon.kiparsky**: 19 February 2013 - 07:05 PM

Page 1 of 1