Put int in int array

Hello,

I have a problem putting an integer number in an integer array.

For example,

if the user enter 5454 and we have int b[4] How can we put
5 in b[0]
4 in b[1]
5 in b[2]
4 in b[3]

note : I am not allowed to use any function


thanks in advance
A way of "cheating" is to read the individual characters. Store the data in a char array, delete some number to get the desired integer (I think it was 48?), and then just use assignment.

-Albatross
I could think of a way to do this with a vector.. But that would probably be complicating it way too much..
Use modulo operator to break apart the digits and then write each digit to the array. It sounds like you just need a fixed array of 4 and the user enters a 4 digit number but how dynamic is it? What if the user enteres 1203523335521? Do you need to make a bigger array by determining the number of digits?
You could ask how many digits will be inputted ahead of time and calculate 10^(that number of digits) - 1 and check if what's inputted is larger than that iff you wish to use kempofighter's method.

Or, you could input the data in to a string and convert it into a char array and use mine.

-Albatross
Thank you guys.

But I am not allowed to ask how many digits the user will enter.

I am asked to have a dynamic array which its size will depond on the user .

So, How can I do it?

The reason I ask is to do large integer arthimatics.
Tip: Divide several times by 10. See what happens.
Hopefully you are able to use std::deque so that you can grow the array as needed. It shouldn't be too difficult to figure out how to use that tool once you figure out how to separate each digit from the number.
Last edited on
Topic archived. No new replies allowed.