Instead of
cardNumber = j+1;
deck[i][j] = cardNumber;
theDeck[k] = cardNumber;
is it valid to just say
deck[i][j] = theDeck[k] = cardNumber;
to assign cardNumber to both deck and theDeck at the same time??
Yes, it is. The assignment operator returns a value.
Yes, it's an expression and its value is the right side of the assignment. Note that this comes also from the associativity of = (right-to-left), which makes this:
x = y = z
Equivalent to:
x = (y = z)
But not:
(x = y) = z /* wouldn't work */
So you can go even further and write:
theDeck[k] = deck[i][j] = cardNumber = j+1;
Yes it is; it is like this:
deck[i][j] = (theDeck[k] = cardNumber);
Related
I have a really simple problem but can't get it done.
I want to produce a linspaced sequence between two values of an anonymous function. If I do it with values its no problem and looks like this
n = 5;
left = 1;
right = 3;
y = zeros(n, 1);
x = linspace(left, right, n)';
q = zeros(2*n, 1);
q(1:2:end) = x
q(2:2:end) = y
But the same thing is not possible with anonymous functions as boundaries. My attempt looks like this but I would really appreciate a better solution
n = 5;
left = #(t) 0.5 * t;
right = #(t) 2 * t^2 + 5;
diff = #(t) right(t) - left(t);
q = #(t) [];
for i = 1:n
q = #(t) [q(t) i*diff(t)/n 0*t];
end
q(0.5)
I hope you can help me, thanks in advance!
Thanks to #Adiel I made the following answer
function [Q] = reference_configuration(left, right, n, t)
l = left(t);
r = right(t);
diff = r - l;
x = linspace(0, diff, n);
y = zeros(1, n);
q = zeros(1, 2*n);
q(1:2:end) = x;
q(2:2:end) = y;
end
Maybe it helps someone. Thank you!
I have just come across some code with the line:
n /= 10;
I assumed it was a typo and removed the / to make it n = 10, but the program no longer works.
Never seen this sort of operator before, anybody know?
The /= is a shorthand operator.
a /= b
is equivalent to
c = a/b;
a = c;
The /= is one of the shorthand operators.
A shorthand operator is a concise way to express something that is already available in a programming language.
They are:
+= (E.g.: x += 4; is equivalent to x = x + 4;)
-= (E.g.: x -= 4; is equivalent to x = x - 4;)
*= (E.g.: x *= 4; is equivalent to x = x * 4;)
/= (E.g.: x *= 4; is equivalent to x = x / 4;)
%= (E.g.: x %= 4; is equivalent to x = x % 4;)
n /= 10 is taking the value of n, dividing it by 10 and reassigning that value to n.
It's just shorthand for n = (n / 10) just like n++ is n = n + 1.
It is the same as
n = n/10 ;
You can use this form also with other operators (+, -, %,...).
The case n+=1 (n-=1) can also be written as n++ (increase n by 1) or ++n.
It simply means n = n/10. The same form can be used with other operators too.
I am desperately looking for a way to easily calculate the number of all possible execution paths in a C function.
For example, for the following function I would expect to get a result of 3 (if there is a chance based on the values that 'i' gets to enter any of the 'if' statements)
void test(void)
{
if (i>0)
x = x + 1;
else if (i>10)
x = x + 2;
else
x = x + 3;
}
Use comma operator as
int test(void)
{
int ways = 0;
if (++ways, i>0)
x = x + 1;
else if (++ways, i>10)
x = x + 2;
else
{
x = x + 3;
++ways;
}
return ways;
}
This is an interview question.
We have only two constructs
loop(a) means loop for a times.
increment(a) increments a.
Thus to implement a+b one could write
loop(a) {inc(b)}
return b;
The question is how to implement a-b.
How about;
a = 10
b = 8
result = 0
loop(b) {
last = 0
times = 0;
loop(a) {
last = times
times = inc(times)
}
result = a = last
}
result is 2
Js eg;
var a = 10;
var b = 8;
var result;
for (var _b = 0; _b < b; _b++) {
var last = 0, times = 0, loopa = 0;
for (var _a = 0; _a < a; _a++) {
last = times;
times = inc(times);
}
result = a = last;
}
function inc(i) {
return i + 1;
}
print(result) // 2
I think if break from loop is allowed, a-b can be done in this way:
c=0;
loop(a) {
if (a==b) break;
inc(c);
inc(b);
}
return c;
Ofcourse assuming a>b.
depends if this Numeric architecture is known:
you can take advantage of the "Two Compliment" mechanism of the x86/x64 architecture,
for example, if the signed numbering scheme is cyclic like.
f(0 < x < 32768) = x
f(32769 < x < 65535) = x - 65536
Then you can use:
dec(a)
{
loop(65535 [= 2^16-1]) { inc(a) }
}
.
solving the riddel as
(a-b)
{
loop(b) { dec(a) }
}
Depending on the Signed scheme the addition Constant can change, same for short, long, large integer types.
Hope this is good :)
Best of luck.
We're a looking for x, so that a-b = x. In other words a = b+x
Pseudocode
int x = 0
WHILE (x <= a) do {
if (b+x == a) BREAK // satisfies a-b = x
x++
}
RESET B
INC B
LOOP A
{
INC D
LOOP B
{
RESET D
}
}
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.