• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How could I put input into an array from a single line of numbers?

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How could I put for example 1001 in int num1[] where each position would be a number from the input? for example:
num1[0] = 1
num1[1] = 0
num1[2] = 0
num1[3] = 1
 
Saloon Keeper
Posts: 13397
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assuming you want to do this for an arbitrary non-negative integer, and not just 1001.

First, how would you get the least significant digit of an integer? If you had to get the digit 4 from the number 1234, how would you do it?
 
Rancher
Posts: 344
11
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hannah

If you want to find the least significant digit in value X in base N then just divide X by N and take the remainder. So if you have decimal value 1234 and want to extract the least signifcant digit, you just calculate 1234 % 10 = 4. Then you want to 'remove' the 4 so you can get the next digit, and to do that you just divide by 10 ie. 1234 / 10 = 123 (using integer division).

And repeat, until you run out of digits ie. X is 0.

Alternatively, just convert your number into a string and extract the digits from that. Use sprintf(buf, "%d", X) to get the decimal representation of X in buf, "1234", so buf[0] = '1', buf[1] = '2' etc. Then convert the character values to numbers by subtracting '0' ie. '2' - '0' = 2.

(To be safe you should use snprintf() - look it up.)

Cheers
John

Edit: sorry Stephan, didn't see your post until after posting mine.
 
Hanna Roberts
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok that makes sense, thank you for the help
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic