Someone please explain me the logic behind this opration [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
#include<stdio.h>
int main(){
int a=10,b=3,c=2,d=4,result;
result = a+a*-b/c%d+c*d;
printf("%d",result);
}
How is this program giving 15 as the output.. I did not understand the logic behind the operation.. Can someone please tell me how calculation is done?

int a = 10, b = 3, c = 2, d = 4, result;
result=a+a*-b/c%d+c*d; // original line, with no spaces added
result = a + (a * (-b) / c % d) + (c * d);
result = 10 + (10 * -3 / 2 % 4) + (2 * 4);
result = 10 + (-30 / 2 % 4) + 8;
result = 10 + (-15 % 4) + 8;
result = 10 + (-3) + 8;
result = 15;

Note that the *, / and % operators have higher precedence than + and that those first three operators have equal precedence and left-to-right associativity. Note also that the unary minus operator (as in -b) has higher precedence than multiplication.
So, adding parentheses to highlight the operators' bindings and order-of-evaluation, and a couple of lines to keep track of the intermediate results, we see the following:
#include<stdio.h>
int main(){
int a=10, b=3, c=2, d=4, result;
result = a + ( ( ( (a*(-b)) ) / c ) % d ) + (c*d);
// ^-30 ^-15 ^-3 ^ 8
// ^ a + -3 = 7 ^ 7 + 8 = 15
printf("%d",result);
}

The relevant code:
int a=10,b=3,c=2,d=4,result;
result=a+a*-b/c%d+c*d;
Now processing this as a compiler would, following the rules of operator precedence:
// Unary minus
result = 10 + 10 * (-3) / 2 % 4 + 2 * 4;
// Multiplication, division, and remainder, with left-to-right associativity.
result = 10 + (-30) / 2 % 4 + 8;
result = 10 + (-15) % 4 + 8;
result = 10 + (-3) + 8;
// Addition and subtraction, with left-to-right associativity.
result = 15;
Note that according to the C99 specification, a == (a / b) * b + a % b must hold for the remainder operation with a negative number.

Related

Not enough input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
INPUT
Integers (0 ≤ Integer < 10^1,000,000)
OUTPUT
Find input mod 3 and input mod 11
EXAMPLE
main(){
int num;//problem here i try long long not pass because it not enough.
scanf("%d",&num);
printf("%d ",num%3);
printf("%d",num%11);
}
"Normal types in C can usually only store up to 64 bits, so you'll have to store big numbers in an array, for example, and write mathematical operations yourself. But you shouldn't reinvent the wheel here - you could try the GNU Multiple Precision Arithmetic Library for this purpose."
-AndiDog
Source:Store and work with Big numbers in C
Take advantage that mod 3 and mod 11 can easily be chained one digit at a time.
#include<stdio.h>
#include<ctype.h>
void mod3_11(void) {
int mod3 = 0;
int mod11 = 0;
int ch;
while (isdigit(ch = fgetc(stdin))) {
int digit = ch - '0';
mod3 = (digit + mod3)%3;
mod11 = (digit - mod11 + 11)%11;
}
printf("mod 3 = %d\n", mod3);
printf("mod 11 = %d\n", mod11);
fflush(stdout);
}
int main(void) {
mod3_11();
return 0;
}
This works as each successive digit is processed, code is taking the previous "mod" * 10
// math
mod_n <-- (10*mod_n + digit)%n
mod3 <-- (mod3*10 + digit) % 3
mod3 <-- (mod3*1 + mod3*3*3 + digit) % 3
mod3 <-- (mod3 + 0 + digit) % 3 (mod3*3*3 % 3 is 0)
mod3 <-- (mod3 + digit) % 3
mod11 <-- (mod11*10 + digit) % 11
mod11 <-- (mod11*11 - mod11*1 + digit) % 11
mod11 <-- (mod11*11 - mod11 + digit + 11) % 11 (add 11 to avoid negative numbers)
mod11 <-- (-mod11 + digit + 11) % 11
Why add 11? What's the difference between “mod” and “remainder”?

Not understanding the calculation

I have following C program, and I'm not understanding the output of the following program.
#include <stdio.h>
int main()
{
int a,b, *p1, *p2, x,y,z;
a=12;
b=4;
p1=&a;
p2=&b;
x=*p1 * *p2-6;
y=4* - *p2 / *p1+10;
printf("y=%d", y);
return 0;
}
The output of the program is 9. But what is the meaning of 4*?
What is the meaning of 4*
The * there is the multiplication operator. Only one operand, 4, is shown in that extract. The full multiplication is:
4* - *p2
which is more clearly written as
4 * -(*p2)
Write out the expression, substituting the values. We can ignore x since it is not used. Which leaves us:
y= 4* - *p2 / *p1+10;
And *p2 is b which is 4. And *p1 is a which is 12. So the expression is:
y = 4 * -4 / 12 + 10;
And this evaluates as:
y = ((4 * -4) / 12) + 10;
Which is
y = (-16 / 12) + 10;
Which is
y = -1 + 10;
The spacing in this line might be causing confusion:
y=4* - *p2 / *p1+10;
This is equivalent to:
y = 4 * (-*p2) / *p1 + 10;
but the spacing makes it look like a subtraction.
The code
4* - *p2
means
4 * (-*p2)
So * means simple multiplication here.
Get familiar with the C precedence and associativity table:
The statements will be evaluated like this (parentheses added for clarity):
x = ((*p1) * (*p2)) - 6;
x = ((12) * (4)) - 6
x = (48) - 6
x = 42
y = (4 * (-(*p2)) / (*p1)) + 10;
y = (4 * (-4) / (12)) + 10
y = (-16 / 12) + 10
y = -1 + 10
y = 9

Please explain the logic behind this program that uses recursion to calculate a^b (a raised to power b) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
It's really embarrassing!!I just fail to understand the working of the following little program that uses recursion to calculate the powers of a number "a" ("a" raised to a power "b").Kindly explain the logic used behind this function.I don't understand the use of the "x*x" parameter,the n/2 parameter and the "n modulo 2" part.Please dissect it for me.
#include<stdio.h>
int foo(int,int);
int main() {
int a,b;
printf("Enter number a and its power b\n");
scanf("%d%d",&a,&b);
printf("a raised to b is %d", foo(a,b));
return 0;
}
int foo ( int x , int n) {
int val=1;
if(n>0) {
if (n%2 == 1)
val = val *x;
val = val * foo(x*x , n/2);
}
return val;
}
The idea behind this recursion is, that ab = (a2)b/2, and ab = a(a2)(b-1)/2.
Depending on whether b is odd or even (thats the n%2 == 1 part), you choose one of these formulas to ensure b/2 or (b-1)/2 is still an integer. Note here, that the n/2 in your code actually is (n-1)/2 for odd n, since integer division rounds down automatically.
This recursion terminates since the exponent grows smaller with each step.
This makes use of the fact that a power such as x^23 can be rewritten as x^16 * x^4 * x^2 * x^1
Now computing x^16 is fairly easy, because it's just (((x^2)^2)^2)^2 which is only 4 multiplications instead of computing x * x * x * ... 16 times ... * x.
Now notice that while computing x^16 you've also run across x^4 and x^2 which you needed to compute your number. So in the end, you've computed x^23 in only 7 multiplications instead of 22.
Now where the n % 2 and n / 2 come in to the picture is in deciding if the power of 2 is in n (in our example, is 8 in the binary representation of 23? no).
So you just iterate through the bits of n. You square x every time, and if there's a 1 in the current n bit you're looking at, you multiply the squared number into your result.
Update:
The trick to writing a number out this way is to look at n in binary. 23 is 101112, or we can write out the place values 23 = 1*16 + 0*8 + 1*4 + 1*2 + 1*1.
This means x^23 = x^(16 + 4 + 2 + 1) and thanks to the exponential laws, = x^16 * x^4 * x^2 * x^1 which is what we started with.
As another quick example: take x^44. We write it in binary as 1011002 so we can say
44 = 1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 0*1 = 32 + 8 + 4
so
x^44 = x^(32 + 8 + 4) = x^32 * x^8 * x^4
we then calculate the following
1: x^2 = (x)^2 (from the x we are given)
2: x^4 = (x^2)^2 (x^2 from step 1)
3: x^8 = (x^4)^2 (x^4 from step 2)
4: x^16 = (x^8)^2 (x^8 from step 3)
5: x^32 = (x^16)^2 (x^16 from step 4)
6: x^44 = (x^32) * (x^8) * (x^4) (using results of steps 2, 3, and 5)
As you stated, foo works recursively. Why don't you go through it step by step? Assume a==2 and b==3, you get
1st move
int foo ( int x , int n) // x == 2, n==3
{
int val=1;
if(n>0) // n == 3, true!
{
if (n%2 == 1) //true!
val = val *x; // val = 1 * 2;
val = val * foo(x*x , n/2); // next step
}
return val;
}
2nd move
int foo ( int x , int n) // x == 4, n==1
{
int val=1;
if(n>0) // n == 1, true!
{
if (n%2 == 1) //true
val = val *x; val = 1 * 4;
val = val * foo(x*x , n/2); // next step -> 4 * ...
}
return val;
}
In the 2nd step you return 4 which yields in the first step
val = val * foo(x*x , n/2); // 2 * 4 in the first step and this equals 8

C bit operation puzzle

/*
* ezThreeFourths - multiplies by 3/4 rounding toward 0,
* Should exactly duplicate effect of C expression (x*3/4),
* including overflow behavior.
* Examples: ezThreeFourths(11) = 8
* ezThreeFourths(-9) = -6
* ezThreeFourths(1073741824) = -268435456 (overflow)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 3
*/
int ezThreeFourths(int x) {
int z = x+x+x;
int sign_z = z>>31;
return ((z>>2)&(~sign_z)) + (((z>>2)+1)&sign_z);
}
I tried to solve this puzzle but
ERROR: Test ezThreeFourths(-2147483648[0x80000000]) failed...
...Gives -536870911[0xe0000001]. Should be -536870912[0xe0000000]
compiled with gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51)
What's wrong with this solution?
Here's what I did:
#include <stdio.h>
#include <limits.h>
int ThreeFourths(int x)
{
int x3 = x + x + x;
return (x3 >= 0) ? (x3 >> 2) : -(int)((UINT_MAX - x3 + 1) >> 2);
}
int testData[] =
{
0,
1,
-1,
2,
-2,
3,
-3,
4,
-4,
5,
-5,
-9,
11,
INT_MAX / 2 + 1,
INT_MIN
};
int main(void)
{
int i;
for (i = 0; i < sizeof(testData)/sizeof(testData[0]); i++)
{
printf(" %d * 3 / 4 = %d\n",
testData[i], testData[i] * 3 / 4);
printf("ThreeFourths(%d) = %d\n",
testData[i], ThreeFourths(testData[i]));
}
return 0;
}
Output:
0 * 3 / 4 = 0
ThreeFourths(0) = 0
1 * 3 / 4 = 0
ThreeFourths(1) = 0
-1 * 3 / 4 = 0
ThreeFourths(-1) = 0
2 * 3 / 4 = 1
ThreeFourths(2) = 1
-2 * 3 / 4 = -1
ThreeFourths(-2) = -1
3 * 3 / 4 = 2
ThreeFourths(3) = 2
-3 * 3 / 4 = -2
ThreeFourths(-3) = -2
4 * 3 / 4 = 3
ThreeFourths(4) = 3
-4 * 3 / 4 = -3
ThreeFourths(-4) = -3
5 * 3 / 4 = 3
ThreeFourths(5) = 3
-5 * 3 / 4 = -3
ThreeFourths(-5) = -3
-9 * 3 / 4 = -6
ThreeFourths(-9) = -6
11 * 3 / 4 = 8
ThreeFourths(11) = 8
1073741824 * 3 / 4 = -268435456
ThreeFourths(1073741824) = -268435456
-2147483648 * 3 / 4 = -536870912
ThreeFourths(-2147483648) = -536870912
The reason why I didn't use right shifts on negative integers is simple. The result of these shifts is implementation-defined (per the C standard) and is not guaranteed to be the same as from a right shift with sign extension that we might be expecting because of it being the most common implementation.
I wrote (UINT_MAX - x3 + 1) instead of simply -x3 because it can result a signed overflow (when x3 = INT_MIN that is a minus power of 2), which has undefined behavior (per the C standard, again). And even if this undefined behavior is known to be harmless, simple negation could still fail to produce a positive number (because of the asymmetry in the 2's complement representation of signed integers).
x + x + x can still produce signed overflow just as x * 3 can. So, this is the same undefined behavior.
Btw, since signed overflows result in UB, it should not even be legally asked from you to achieve them, let alone have specific expectations about the results when UB occurs.
int ezThreeFourths(int x) {
int z = x+x+x;
int sign_z = z>>31;
return ((z>>2)&(~sign_z)) + (((z>>2)+1)&sign_z);
}
Works with non-negative numbers. Also you shouldn't lie about code "you" wrote. Considering the exact code was written in "2008-01-26"
Works fine for me using Embarcadero C++ 6.43:
// x = 2147483647
int ezThreeFourths(int x)
{
int z = x+x+x;
// z = 2147483645 (6442450941[0x17FFFFFFD] truncated to 32-bits!)
int sign_z = z>>31;
// sign_z = (2147483645 >> 31) = 0
return ((z>>2)&(~sign_z)) + (((z>>2)+1)&sign_z);
// = ((2147483645 >> 2) & (~0)) + (((2147483645 >> 2) + 1) & 0)
// = (536870911 & 0xFFFFFFFF) + ((536870911+1) & 0)
// = (536870911 & 0xFFFFFFFF) + (536870912 & 0)
// = (536870911 & 0xFFFFFFFF) + 0
// = (536870911 & 0xFFFFFFFF)
// = 536870911
}
Your approach for making negative numbers round towards zero is not working correctly in the case of input values that divide evenly by 4. 0x80000000 is one such example, but it's perhaps easier to see the problem if you try with a small value.
For example: ezThreeFourths(-8) = -5 [ should be -6 ]

How to get the 0's of digits when iterating through int's in C

I am trying to iterate through some number as 02100021, this is a routing number which needs to get validated if it is a proper routing number. Therefore I am using the ABA routing number validation check as 3*(d1+d4+d7) + 7*(d2+d5+d8) + (d3+d6+d9) where mod 10 = 0
Since I have the number in integer type, my first question is how can I iterate through such number with 0's in it, or if there is some easier way of iterating through mode and multiplying it with such number.
Thanks
If I understand correctly, 7 * (d2, d5, d8) should be 7 * (d2 + d5 + d8).
Zeros modulo 10 do not count.
bool correct(long x) {
long a = x / 10 + x / 10000 + x / 10000000L;
long b = x / 100 + x / 100000L + x / 100000000L;
long c = x / 1000 + x / 1000000L + x / 1000000000L;
int aba_checksum = (int)((3 * a + 7 * b + c) % 10);
return aba_checksum == 0;
}
Corrected for d[i] = digit * 10 ^ (9 - i)
bool correct(long x) {
long a = x / 100000000L + x / 100000L + x / 100;
long b = x / 10000000L + x / 10000L + x / 10;
long c = x / 1000000L + x / 1000L + x;
int aba_checksum = (int)((3 * a + 7 * b + c) % 10);
return aba_checksum == 0;
}
Modulo arithmetic can be done at the end.
To prevent overflow (to negative numbers) one might do it earlier, above even 7 * b does not overflow.

Resources