How to get second half of a number? - c++

If n is a given number, how to get the first half of a number and second half?
Ex: 9456, what I need is the first half is 94 and the second half is 56.
I can get the first half of a number using (int)(n / pow(10, (digits / 2))) this formula, but am not able to get the second half. Can anyone help here?
Thank you.

I think you can use the simple process for picking up each digit using n%100 and then n/=10 ,then store the first half of numbers in a variable and the other half in another.

first convert number to string , then use .length() to obtain max pow of 10 lets say = p.
then take phalf = p/2 if it is even or (p + 1)/2 , if it is odd.
then do divide and modulo with pow( 10, phalf ), divide will give upper first half and modulo woill give lower second half.

To get the MSB (most significant digit), you have to use / operator.
and to get the LSB (least significant digit), you have to use % operator.
In your case :
second_half = n % pow(10, digits / 2)
Make sure to round the outcome of the pow function since, it return type is double, which may lead to precision error.

Related

Can someone explain this bit wise operation

I am trying to understand what is happening in this bitwise operation that is used to set the size of a char array but I do not fully understand it.
unsigned long maxParams = 2;// or some other value, I just chose 2 arbitrarily
unsigned char m_uParamBitArray[(((maxParams) + ((8)-1)) & ~((8)-1))/8];
What size is this array set to based on the value of maxParams?
This counts the number of 8-bit chars needed to fit maxParams bits.
+7 is to round up to the next multiple of 8.
0 -> 7 will round to 0 char
1 -> 8 will round to 1 char
After division by 8. Which is done by /8
The bitwise and is to discard the last three bits before dividing by 8.
When maxParams is not divisible by eight, the first part of the formula formula rounds it up to the next multiple of eight; otherwise, it leaves the number unchanged. The second part of the formula divides the result by eight, which it can always do without getting a remainder.
In general, if you want to round up to N without going over it, you can add N-1 before the division:
(x + (N - 1)) / N
You can safely drop the & ~((8)-1) part of the expression:
(maxParams + (8-1))/8

How to find the second to last digit of a six digit number?

For example, if the number is 431678 I need to find the 7 and 8.
To find the 8 I divide the number (with %) by 10. I don't know how to find the 7. I assume I use division possibly by 10000? How can I get my program to print 7(second to last digit)? It's obviously a simple program so I'm looking for the simple arithmetic.
const unsigned x = 431678;
std::cout << ((x / 10) % 10);

A program that determines the nth positive root of the p

I want to count the nth positive root of p for example we have n=2 and p=16 the answer is 4 because
4^2 = 16. I want to do this for huge numbers (1 <= n <= 200, 1 <= p < 10^101). I don't know how should I do it as fast as possible.
Example:
n=2 p=16 Answer 4
n=7 p=4357186184021382204544 Answer 1234
There are arbitrary precision math packages out there, if you have to come up with your own algorithm.
But you might try this: Get p into a double any way you can (a double can handle 10^101.) Then use math.h::pow(p, 1.0/n), and that answer will be close to the right integer (round it?). But this will fail if p is more than 15 digits, and n is too small, e.g., p = 10^100, n=2 gives a 50 digit answer, which is too big an integer for double to represent exactly.
Get 101 digit p into double: cut the number (string) into 10 digit chunks, multiply each by 10 to the appropriate power, and add them up.
Try Newton's method as described here:
http://en.wikipedia.org/wiki/Nth_root_algorithm
Take log of p, divide by n, and take the anti-log:
nthRoot(p, n) := Math.Power(10, Math.Log(p) / n)
Not sure whether you're specifically dealing with integers or what but that is the psuedo-code for it.

How to efficiently find the largest integer closest to the mean of two integers in increments of 100,000?

Let's say I am given integers x and y (satisfying x <= y with ones digit of 0 so they are, in particular, divisible by two). Then I know that their average avg = ((x+y) / 2) is an integer as well. I would like to find this midpoint rounded up to a resolution of 100. In other words if my two inputs are 75200 and 75300 then the avg is 75250 and rounded up to the nearest 100 (but without exceeding or equaling the bigger number) forces the answer to be 75200.
How can I implement this logic without first dividing everything by 100 and using the following floating point arithmetic:
x + std::floor((y - x) * .5 * 100 + .5)*0.01
In other words, how can I do the above without floating point values but obtain the same behavior at the resolution of 100 instead of 0.01?
To compute the average you can do
avg = (x + y) / 2
(BTW, integer addition and division by 2 are very cheap operations even on small microcontrollers.)
To round this to the nearest multiple of 100 (corresponding to your floating-point example) you can do
result = ((avg + 50) / 100) * 100
as integer division rounds down to the nearest integer. By changing the 50 to 0 you can always round down, while changing it to 99 always rounds up.
Edit: Note that this method for rounding doesn't work for negative numbers. Since integer division rounds towards zero, in that case you'll need to subtract the 50, subtract 99 to always round down and subtract 0 to always round up.
Your problematic example requires strong conditions:
the difference between x and y needs to be not greater than 100
y % 100 must be 0
So for most cases, a simple rounded average is perfect for you:
avg100 = avg - (avg % 100) + 100
The tricky part is fixing the remaining error without a condition - if you want to avoid conditions, or slow operations.
For this, the best way is to use a multiplication, and split the expression into two:
avg100 = avg - (avg % 100)
avg100 += 100 * !!(y - avg100)
For most cases, y is greater than avg100. For this case, the !! operator will return 1. In the rare case when they equal, it will return a 0, and it won't change the value.
(I don't know if the compiler will really generate a code without conditions for the '!!' operator, but I don't have a batter idea, and if it is possible, I think it will. If not, this code is still short and easy to understand.)
Also, you can calculate the average using the following expression:
avg = y - (y-x)/2
Or even change the division into bit shift for optimization.
This won't require for both of the numbers to be even, just to be the same parity.

Need help with C++ Loops Exercise

From Cay Horstmann's "C++ For Everyone"
Chapter 4: Loops
Write a program that adds up the sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17)
I don't know how to make the computer "see" the numbers like separate them
n % 10 gets the value of the one's digit. You can figure it out from there right?
Here's a hint. C++ has the modulus operator %. It will produce the remainder when two numbers are divided together. So if I wanted to know the last digit in a number which was greater than 10 I would modulus 10 and get the result
int lastDigit = number % 10;
The last digit of a base-10 integer i is equal to i % 10. (For reference, % is the modulus operator; it basically returns the remainder from dividing the left number by the right.)
So, now you have the last digit. Once you do, add it to a running total you're keeping, divide i by 10 (effectively shifting the digits down by one place), or in your case 100 (two places), and start back at the beginning. Repeat until i == 0.
People here rather not provide you with the answer to your exercise, but to provide you with hints so that you can find the answer on your own and more importantly understand it.
To start, the following arithmetic operations will help you:
loop:
right_most_digit = n % 10
n = n / 10
end_loop