8 Replies - 514 Views - Last Post: 19 February 2013 - 06:23 PM Rate Topic: -----

#1 Trent4296  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-February 13

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

Posted 19 February 2013 - 10:01 AM

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

Is This A Good Question/Topic? 0
  • +

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

#2 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

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]
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,883
  • Joined: 06-March 08

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

Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

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

Posted 19 February 2013 - 10:34 AM

View Postpbl, on 19 February 2013 - 12:28 PM, said:

I think you mean

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



Use the log base 10 of n.

log base 10 1 = 0
log base 10 10 = 1
log base 10 100 = 2

etc.
Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,822
  • Joined: 20-September 08

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

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

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

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.
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));


Was This Post Helpful? 1
  • +
  • -

#7 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

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/

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

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,822
  • Joined: 20-September 08

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.
Oops - yes ;)
Was This Post Helpful? 0
  • +
  • -

#9 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

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

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1