Hi
Someone can help me with a code that i can determine the positions of the digits of an integer.
For example 456
Digit 4 is in position 0
Digit 5 is in position 1
Digit 6 is in position 2
Given an unsigned integer, you can examine the digits one by one in reverse order with code like:
1 2 3 4 5
while (num) {
unsigned digit = num % 10;
// Do Something with digit
num /= 10;
}
With this code, you could find the position from the back. If you also keep track of the length of the number (the number of digits), then you could get the position from the front with a little math.
Another possibility that's much less efficient is to convert the number to a string using ostringstream and then examine the string for the position. One could certainly argue that efficiency doesn't matter since either will run very fast on a modern PC.
Can you clarify some odd cases? Consider the number 456764:
Is digit 4 in position 1? position 6? Both?
If I ask for the position of 8, what should it return?
#include <iostream>
#include <vector>
#include <algorithm>
usingnamespace std;
//======================================================================
vector<int> digitise( unsignedlonglong N )
{
vector<int> result;
while ( N )
{
result.push_back( N % 10 );
N /= 10;
}
reverse( result.begin(), result.end() );
return result;
}
//======================================================================
template <typename T> ostream &operator << ( ostream &strm, const vector<T> &V )
{
for ( T element : V ) strm << element << " ";
return strm;
}
//======================================================================
int main()
{
unsignedlonglong N;
cout << "Input a number: "; cin >> N;
vector<int> digits = digitise( N );
for ( int d = 0; d < 10; d++ )
{
vector<int> pos;
for ( int p = 0; p < digits.size(); p++ ) if ( digits[p] == d ) pos.push_back( p );
if ( pos.size() != 0 ) cout << "Digit " << d << " is at position(s) " << pos << '\n';
}
}
//======================================================================
Input a number: 123454321
Digit 1 is at position(s) 0 8
Digit 2 is at position(s) 1 7
Digit 3 is at position(s) 2 6
Digit 4 is at position(s) 3 5
Digit 5 is at position(s) 4
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
usingnamespace std;
//======================================================================
int length( unsignedlonglong N ) { return N < 10 ? 1 : 1 + length( N / 10 ); } // find the digit length of a number
//======================================================================
template <typename T> ostream &operator << ( ostream &strm, const vector<T> &V ) // output a vector of general type
{
for ( T element : V ) strm << element << " ";
return strm;
}
//======================================================================
int main()
{
unsignedlonglong N;
cout << "Input a number: "; cin >> N;
int len = length( N );
map<int,vector<int>> pos; // positions for each digit
for ( int p = len - 1; N; p--, N /= 10 ) pos[N%10].push_back( p ); // update table using digits of N
for ( auto &e : pos ) // output
{
reverse( e.second.begin(), e.second.end() ); // needed because pushed on "from the back" of N
cout << "Digit " << e.first << " is at position(s) " << e.second << '\n';
}
}
//======================================================================
Invoque it like getDigitOf(456, 1) to get '5'.
If you want a numerical result version, just modify that a bit like this :
1 2 3 4 5 6
#include <string>
int getDigitValueOf(intconst number, intconst position)
{
auto numberString = std::to_string(number);
return numberString.at(position) - '0';
}
And you're good to go :o)
I used the to_string function that converts my integer into a string (of characters).
That's the reason why, when I read an element, I obtain a "char"acter :oP~
Also note that this is the very most simple part... I didn't take care of the "number" before I read the char at that position --> numberString.at(position).
You may want to take care of that and put a little test on "position" to conform the function to what you want and need.