Hi
Let's say I have 4 digit number and I want to extract each digit and assign to array elements. So instead of having int rrrr = 1998 I have a int rrrr[3]={1, 9, 9, 8}.
Is there any simple way of doing this?
If you are using C++, you could put it into a stringstream, then read out each character individually and turn it into a integer. Otherwise, you would have to use the '%' and '/' operators to get each part.
Here is an algorithm. Every line of the algorithm should translate to exactly 1 line of C++ code.
0. Make an array of at least 10 ints (32-bit int has at most 10 digits)
1. Make a counter of the number of used elements in the array.
2. Prompt user to enter a number
3. Read in the number into a variable named "num"
4. Repeat the following steps until there are no more digits in num:
a. Get the ones digit from num into a temporary variable
b. Store the temporary value into the first unused element in the array
c. Increment the number of used elements in the array
d. Modify num so that it contains all but the ones digit.