Win a copy of Fixing your Scrum this week in the Agile forum!
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:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Rob Spoor
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Junilu Lacar
• Tim Cooke
Saloon Keepers:
• Tim Holloway
• Piet Souris
• Stephan van Hulst
• Tim Moores
• Carey Brown
Bartenders:
• Frits Walraven
• Himai Minh

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

Ranch Hand
Posts: 158
• Number of slices to send:
Optional 'thank-you' note:
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: 13174
285
• Number of slices to send:
Optional 'thank-you' note:
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: 321
11
• 2
• Number of slices to send:
Optional 'thank-you' note:
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: 158
• Number of slices to send:
Optional 'thank-you' note:
Oh ok that makes sense, thank you for the help

 When I was younger I felt like a man trapped inside a womanâ€™s body. Then I was born. My twin is a tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising