Its a c++ operator called the ternary operator.
Basically the pseudo code is like this
(return statement or a variable you are assigning a value to ) (condition to test) ? ( if true return this value ) : (otherwise return this value )
so lets look at my example
The thing I am using the value with is the return statement.
the condition I am using n > 1 //should of been n > 0 I had a typo because x^0 == 1
if n is greater than 1 I am using recursion ( return the value of x * pow( x , n - 1 )
if n is less than or equal to 1 then return 1 //again should have been less than 1 aka 0
Basically it can be rewritten as either of these two:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
long long pow( int x , int n )
{
if( n > 0 )
return( x * pow( x , n - 1 );
return( 1 ); //if x is not greater than 1
}
//or
long long pow( int x , int n )
{
int increment = x;
if( n == 0 )
return( 1 );
for( int i = 1; i < n; ++i )
x *= increment;
return( x );
}
|
Then it should have been
first_digit = number / pow( 10 , power_ten - 1 )
since 1000 = 10^3 not 10^4 I had a brain fart.
Here is somethign to read up on:
http://www.cplusplus.com/articles/1AUq5Di1/
http://www.learncpp.com/cpp-tutorial/34-sizeof-comma-and-arithmetic-if-operators/
*edit also you may use the pow function in the math library if you want. I just didn't feel like it.
**edit
The power_ten in my code is really the amount of digits in the number.