The question is
int i = 6, x = 11;
switch(i % 3 ? 0 : 1)
{
case 0: x /=2; break;
case 1: x +=3;
case 2: x *=4;
}
cout << "x = " << x;
So the answer i should be getting is x=5, however, from the answer script it said it should be x= 56.
Why is that?
Both statements:
x +=3;
and
x *=4;
are executed. This is why (see here for conditional operator reference and here for the switch statement).
In the expression
i % 3 ? 0 : 1
i % 3 is the condition. Being an integer value, it's considered false if equal to zero, true otherwise. In your case i has value 6, thus i % 3 is 0, thus the condition is false and the value after the : is taken to be evaluated in the switch statement.
The value after the : is 1, so the code jumps to case 1 and executes x += 3. Now x is 11+3=14.
But there is no break statement before case 2, so the execution simply goes on with x *= 4 and x becomes 14*4=56.
Related
I have the following code:
if (x % 2 == 0) ^ (y % 2 == 1) ^ (z % 2 == 0) {
grid_box.weights[x][y][x] = 1.0;
assert_eq!(grid_box.weights[x][y][x], 1.0);
} else {
grid_box.weights[x][y][z] = 0.0;
assert_eq!(grid_box.weights[x][y][x], 0.0); // Assertion failed, 1.0 != 0.0
}
However, the second assertion fails even though I assigned the grid position to zero in the very previous line. Further experimentation shows that the previous value in the grid seems to make a difference for what the new value should be, even though assignment should override the value entirely.
You have a typo, you assign to grid_box.weights[x][y][z] and read from grid_box.weights[x][y][x]. Replace the z with x.
Where should an element be located in the array so that the run time of the Binary search algorithm is O(log n)?
The first or last element will give the worst case complexity in binary search as you'll have to do maximum no of comparisons.
Example:
1 2 3 4 5 6 7 8 9
Here searching for 1 will give you the worst case, with the result coming in 4th pass.
1 2 3 4 5 6 7 8
In this case, searching for 8 will give the worst case, with the result coming in 4 passes.
Note that in the second case searching for 1 (the first element) can be done in just 3 passes. (compare 1 & 4, compare 1 & 2 and finally 1)
So, if no. of elements are even, the last element gives the worst case.
This is assuming all arrays are 0 indexed. This happens due to considering the mid as float of (start + end) /2.
// Java implementation of iterative Binary Search
class BinarySearch
{
// Returns index of x if it is present in arr[],
// else return -1
int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r)
{
int m = l + (r-l)/2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return -1;
}
// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = {2, 3, 4, 10, 40};
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at " +
"index " + result);
}
}
Time Complexity:
The time complexity of Binary Search can be written as
T(n) = T(n/2) + c
The above recurrence can be solved either using Recurrence T ree method or Master method. It falls in case II of Master Method and solution of the recurrence is Theta(Logn).
Auxiliary Space: O(1) in case of iterative implementation. In case of recursive implementation, O(Logn) recursion call stack space.
Question: What is y after the following switch statement is executed? Rewrite the code using an if statement.
y = 3; x = 3;
switch (x + 3)
{
case 6: y = 1;
default: y += 1;
}
I'm at the beginning of the C++ hike. I don't know what do with this. It's not working in C++ Visual Studio 2013. I've put it in as is and nothing happens.
I use:
y = 3; x = 3;
switch (x + 3)
{
case 6: y = 1;
default: y += 1;
}
return 0;
}
And nothing happens. I have both the answers but I have no clue how to get them...
y is 2
if (x + 3 == 6)
y = 1;
y += 1;
I'm strictly supposed to be using
#include <iostream>
using namespace std;
int main()
{
return 0;
}
switch basics in C/C++:
switch expression is evaluated once, at the beginning. Then the 1st matching case is executed
after the case clause, the execution falls through to the next clause unless there's a break. Since this typically isn't what was indended, fall-throughs are normally documented with a comment or (if multiple cases refer to the same code) by stacking them together (case 2: case 3: <code>).
The following Groovy code prints a range of numbers from 1 to 5.
(1..5).each {println it}
However, when I forget to add the parenthesis, and do this:
1..5.each { println it}
It prints only 5
Why is this legal Groovy syntax? I would expect this to either behave as the (1..5) version or to throw an exception saying that I have forgotten the parenthesis.
5.each has priority over 1..5 in the Groovy parser. It works because it is doing something like this:
ret = 5.each { println it }
range = 1..ret
assert range == [1, 2, 3, 4, 5]
The return of each is the collection itself
The .-Operator has a higher precedence in groovy than .. Source:
Operator Overloading
The precedence heirarchy of the operators, some of which we haven't looked at yet, is, from highest to lowest:
$(scope escape)
new ()(parentheses)
[](subscripting) ()(method call) {}(closable block) [](list/map)
. ?. *. (dots)
~ ! $ ()(cast type)
**(power)
++(pre/post) --(pre/post) +(unary) -(unary)
* / %
+(binary) -(binary)
<< >> >>> .. ..<
< <= > >= instanceof in as
== != <=>
&
^
|
&&
||
?:
= **= *= /= %= += -= <<= >>= >>>= &= ^= |=
Following is an example program in BASIC. Can someone tell me what this function returns if the marked condition is not true? I have to port the program to C++ and need to understand it. I have no BASIC knowledge - please bear with simple question.
FUNCTION CheckPoss (u)
tot = tot + 1
f = 0
SELECT CASE u
CASE 2
f = f + CheckIntersection(1, 3, 2, 1) 'A
CASE 3
f = f + CheckIntersection(2, 3, 3, 1) 'B
END SELECT
IF f = 0 THEN <============== This condition if true,
CheckPoss = 1 <============== then return value is 1
IF u = 9 THEN
PrintSolution
END IF
END IF
END FUNCTION
This is a good example of bad programming. First some unknown global variable is changed in this function. "tot = tot + 1"! Second line "F" is another unknown global variable is assigned "0". Or is this the only place this variable is used? In that case it is a variant implicitly declared here. Use a dim to declare it. It is legal in basic to do this. Globals should be passed as arguments to the function like this:
function CheckPoss(u as integer, tot as integer) as integer
dim f as integer
f=0
It is all about good practice so the input is clear and output is clear and all variable assignments should be through arguments passed to the function.
The return type is not declared either. Is this visual basic? or is it some older basic? Anyway the return type is a variant in case of visual basic. Older basic would be an integer type.
The output from this function will mostly likely be a zero if the condition is not met! That should also be clear in the code and it is not clear as it is, and I understand why you ask. I am amazed this piece of code comes from a working program.
Good luck with your project!
I don't know exactly that this function do.
On VB.net, the function follow the structure:
Public function CheckPoss(Byval u as integer)
... ' Just commands
return u ' Or other variable
end function
If not exist the 'return' command, the return of function will be 'null' character.
On C, The function will be:
int CheckPoss(int u){
tot++; // Increment tot variable (need be declared)
int f = 0;
switch(u){
case 2:
f += CheckIntersection(1, 3, 2, 1); // A
break;
case 3:
f += CheckIntersection(2, 3, 3, 1); // B
break;
}
if (f == 0){
if (u == 9){
PrintSolution();
}
return 1;
}
}
The return command need be the last command of this function. At case f != 0, the function must return trash (some value or character).
My suggestion is:
int CheckPoss(int u){
tot++; // I think that this must count how times you call this function
int f;
if(u == 2){
f = CheckIntersection(1, 3, 2, 1); // A
}else if(u == 3){
f = CheckIntersection(2, 3, 3, 1); // B
}else{
f = 1; // Case else
}
if (f == 0){
if (u == 9)
PrintSolution();
return 1;
}
}