Calculating numbers with operators [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 7 years ago.
Improve this question
Can anyone explain to me how this code is being calculated using operators.
#include <stdio.h>
int main(void)
{
int x = 2, y , z;
x *= 3;
printf("%d\n", x);
x = x * (3 + 2);
printf("%d\n", x);
x *= y = z = 4;
printf("%d\n", x);
x *= y += z;
printf("%d %d %d\n", x, y, z);
return 0;
}
As a result, I got the following output:
6,
30,
120,
960, 8, 4,

The operator followed by an equal sign means that the operator is applied to the variable on the left and the variable on the right, followed by an assignment to the variable on the left. You also follow the precedence operations. I have expanded the meaning below.
x *= 3;
is the same as
x = x * 3; // 6 = 2*3
The parentheses shows order of precedence
x = x * (3+2) // x was 6 from the previous line
this is
x = x * 5 // which gives 30
next
x *= y = z = 4;
means
z = 4;
y = z; //which is 4
x = x *y; // which is 120 = 30 *4
next
x *= y += z;
means
y = y + z ; // y = 4 + 4 (8)
x = x * y; // x = 120 * 8 (960)

*= multiplies left operand by right operand and assigns result to left.
x *= y = z = 4;
Works right to left, assigning 4 to z and y then multiplying x by 4.
x *= y += z;
z is added to y and result is assigned to y, then x is multiplied by y and result is assigned to x.

x*=3 means x=x*3 => x=2*3=6
x*=(3+2) means x=x*(3+2) => x=6*5=30
x*=y=z=4 means z=4, then y=4, then x=x*y => x=30*4=120
x*=y+=z means y=y+z then x=x*y => y=4+4=8 then x=120*8=960.

The first two assignments are straightforward: multiplying x by 3 gives you 6, and then by (3+2) gives you 30.
The third assignment features a chain of assignments, with 4 assigned to y and z. Since the value of an assignment is always the value of its left side after the assignment, the final multiplication x *= y multiplies x by 4, giving you 120.
The last line is the trickiest, because it features two side effects. First, y += z is evaluated, producing 8. After that x is multiplied by 8, producing its final value of 960.
Note that in the last line x is multiplied by 8, which may or may not be the value of y at the time the multiplication is performed.

Related

Make 2 numbers reach their target numbers at the same time

I'm trying to code a mouse drag function in C. Basically, i want X to reach targetX, and Y to reach targetY at the same time. For example:
x = 0, y = 0;
targetX = 10, targetY = 20;
I want to make x reach targetX by the time y reaches targetY. I want it to loop until both x and y reach their target number, incrementing them by 1 each time to actually achieve that. So, at one point, x would be 9, and y would be 19, then it would increment them each by 1, causing them to both equal their target numbers, instead of x reaching targetX first, then y getting incremented solo (which is what my current code does). Also, i need it to be able to decrement (instead of increment) x or y (or both), as well.
Here is what i have now:
for (x; x < targetX || x > targetX;) {
x = (x < targetX) ? x + 1 : x - 1;
y = (y < targetY) ? y + 1 : y - 1;
mouse_move(x, y);
}
for (y; y < targetY || y > targetY;) {
y = (y < targetY) ? y + 1 : y - 1;
mouse_move(x, y);
}
It doesn't have to be in C, i'm just looking for a way to make x and y reach their target numbers at the same time.
Save the initial x.
Save the initial y.
delta x = target x - initial x
delta y = target y - initial y
num steps = max of the deltas (or whatever you want)
For step = 1 .. num steps,
fraction = step / num steps
x = initial x + ( fraction * delta x )
y = initial y + ( fraction * delta y )
Move the mouse

how to multiply values from table in c

I am new to programming and have been asked to create a table with 3 variables x, y and z.
To create x and y, I was asked to use a for loops and have does so. For z, I have to multiply the values of x and y but I'm not entirely sure how to work out z and how to place it in a table.
Please help. I have given an example of how my results should be.
What I've done so far:
int x, y, z;
for (x = 1; x <= 4; x++)
printf(" %d ", x);
for (y = 2; y <= 5; y++)
printf(" %d ", y);
return 0;
The data structure should be not complex
int matrix[3][5];
for(i=0; i<5;i++){
matrix[0][i]=i+1;
matrix[1][i]=i+2;
matrix[2][i]=matrix[0][i]*matrix[1][i];
}
You can change to char matrix to include your headers
You could see that course
https://www.edx.org/course/c-programming-pointers-and-memory-management
If the task is only to print a table, like the one posted, all you need is one loop:
#include <stdio.h>
int main(void)
{
// print the header of the table
puts("======================\n x y z = x * y\n----------------------");
for ( int x = 1; // initialize 'x' with the first value in the table
x <= 5; // the last value shown is 5. 'x < 6' would do the same
++x ) // increment the value after each row is printed
{
int y = x + 1; // 'y' goes from 2 to 6
int z = x * y; // 'z' is the product of 'x' and 'y'
// print each row of the table, assigning a width to each column,
// numbers are right justified
printf("%3d %3d %3d\n", x, y, z);
}
puts("======================");
return 0;
}
The output beeing
======================
x y z = x * y
----------------------
1 2 2
2 3 6
3 4 12
4 5 20
5 6 30
======================
int x[] = {1,2,3,4,5,.....} <-----for storing values of x
int y[] = {2,3,4,5,6,....} <------for storing values of y
Take another array for storing z values.
So now we have z[i]=x[i]*y[i] where i=0,1,2,........n also y[i]=x[i]+1
Use a loop to calculate and print the result.

Solving compound assignment expressions

Okay, I know the output for the expression (x *= y = z = 4;) is 40; but how exactly did we get 40? Can you please show me step by step.
I thought the precedence is from right to left so (2 * 4) = (z =4), I don't understand
#include <stdio.h>
#define PRINTX printf("%d\n",x)
int main (void){
int x = 2, y, z;
x *= 3 + 2;
PRINTX;
x *= y = z = 4;
PRINTX;
x = y == z;
PRINTX;
x == ( y = z );
PRINTX;
return 0;
}
No, the only way that assignment can be evaluated here is right to left.
First, note that x *= 99, for example, is shorthand for x = x * 99.
With that said,
x *= y = z = 4;
is equivalent to
z = 4;
y = z;
x *= y; // This is shorthand for x = x * 4
Consider what would happen if you tried to evaluate it the other way around:
// y is unininitialized
x *= y;
y = z;
z = 4;
It would fail.
So really,
// x = 2
int x = 2, y, z;
// x = x * (3 + 2) = x * 5 = 2 * 5 = 10
x *= 3 + 2;
// x = x * 4 = 10 * 4 = 40
x *= y = z = 4;
This can be rewritten as
int x, y, z;
x = 2; // x = 2
x = x * (3 + 2); // This is 2 * 5, so x = 10 after this
z = 4; // z = 4
y = z; // y = 4
x = x * y; // x = 10 * 4 = 40
And that's how you end up with 40.
All of the assignment operators have equal precedence, and right-to-left associativity (which affects what happens when multiple operators of equal precedence are present in an expression).
This means x *= y = z = 4 is equivalent to x *= (y = (z = 4)). z = 4 must be evaluated first (which assigns z to 4, and gives a result of 4). The assignment y = ... then gives the value y the value of 4, and also produces a result of 4. The assignment x *= ... then multiples x (which has a value 10) by 4, giving a result of 40.
(The reason x *= 3 + 2 gives x the value 10 is that addition has higher precedence than assignment, so x *= 3+2 is equivalent to x *= (3 + 2) rather than (x *= 3) + 2.)
If the assignment operators were instead left-to-right associative, x *= y = z = 4 would be equivalent to (((x *= y) = y) = z) = 4 which would not compile.
You have:
int x = 2, y, z;
x *= 3 + 2;
This is a shorthand for x = x * (3 + 2), which evaluates to 10 given that x starts at 2.
PRINTX;
x *= y = z = 4;
After this, y == z and both are set to 4; and x is 4 times its previous value of 10, hence 40.
PRINTX;
x = y == z;
This compares y and z; they're equal, so x is assigned 1 (comparisons always evaluate to 0 or 1).
PRINTX;
x == ( y = z );
This assigns z to y (leaving the value unchanged at 4); nominally, this is compared with x but the compiler can ignore the comparison. Therefore, x is unchanged and still 1.

Why does this program run the way it does?

#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int x, y, z;
x = ++a[1];
y = a[1]++;
z = a[x++];
printf("%d, %d, %d", x, y, z);
return 0;
}
"x" is printed as 3, but I would have expected it to return 2? In fact if I remove the "++" and set x equal to a[1], it returns as 2. It adds 1 to any value that is actually there. Am I missing something?
"x" is printed as 3, but I would have expected it to return 2?
x = ++a[1];
Here x = 2 because of pre-increment
The you have
z = a[x++];
x ++ = x + 1 = 2+1 = 3
Hence x=3
x = ++a[1];//Increment a[1] and then assign it to x
y = a[1]++;//assign a[1] to y and then increment a[1]
z = a[x++];//assign a[2] to z and then increment x
The ++ is called an increment operator. It increases the value by 1.
In you code you have
x = ++a[1];
++ before a variable is the Pre Increment Operator. It increments the value of a[1] before assigning it to x. So the value of a[1] and x becomes 3.
The other one
y = a[1]++;
++ after the variable is the Post Increment Operator. It assigns a[1] ( which has already become 3 ) to y and then increments the value of a[1] to 4.
This link will help you
http://www.c4learn.com/c-programming/c-increment-operator/#A_Pre_Increment_Operator
x=++a[1]; // Now x got the value 2.
In this line,
z = a[x++]; // x++ will be happen after the assigning of a[2] to z. So the value of x is incremented. So the x value is became 3.
It is post increment so the z got the value as 15. Refer this link.

Is `y = x = x + 1;` undefined behavior?

Is this code:
y = x = x + 1;
undefined behavior in C?
Answer to your question
No.
What will happen
This will happen:
int x = 1; /* ASSUME THIS IS SO */
y = x = x + 1;
/* Results: */
y == 2;
x == 2;
How it compiles
The same as:
x += 1;
y = x;
Why this is not undefined
Because you are not writing x in the same expression you read it. You just set it to itself + 1, then assign y to the value of x.
Your future
If you find the code confusing you can use parentheses for readability:
y = x = (x + 1);
No, your expression is properly defined. You probably were looking for y = x = x++;, which is not.
No. You only modify x once, and due to the right-associativity of = that assignment happens before the assignment to y. Even if it did happen after, there's still only one modification of x. Your statement is as legal as y = ++x.

Resources