Why Compiler change value of x to 1? [duplicate] - c++

This question already has answers here:
Operator precedence Confusion
(2 answers)
Operator associativity, precedence
(8 answers)
Closed last month.
Is that for 1 return to True value and 0 return to False value?
The output is 111
#include <iostream>
using namespace std;
int main()
{
int x = 8, y = 10;
if (x = 2 && y > x)
cout << y + 1;
cout << x;
}

As the && operator has a higher operator precedence than the = operator, the condition of the = operator is being treated as assigning a boolean value to x as-if you had written:
x = (2 && (y > x))
Not as:
(x = 2) && (y > x)
For better experiences, use parenthesis explicitly:
if ((x = 2) && (y > x))

Related

What n&1 and n>>=1 mean? [duplicate]

This question already has answers here:
What is (x & 1) and (x >>= 1)?
(5 answers)
Closed 2 years ago.
I was doing this exercise : https://www.hackerrank.com/challenges/30-binary-numbers/problem and I found this code, but I didn't understand what the condition with n&1 and n>>=1 do here.
//C++ program to convert a decimal
// number to binary number
#include <iostream>
using namespace std;
int main()
{
int n,count=0,max=0;
cin >> n;
while(n)
{
if (n&1)
count++;
else
count = 0;
if (max < count)
max = count;
n>>=1;
}
cout << max;
return 0;
}
if (n&1)
checks whether n is odd by doing a bitwise and.
n>>=1;
shifts the bits of n to the right by one bit.
The & is a bitwise-AND operator and evaluates the expression in either true or false (when the expression is conditional), it's pretty much similar to x % 2, i.e. this condition:
if (n & 1) {
//...
}
// is equal to
if (n % 2) {
// ...
}
OTOH, n >>= 1 shifts right n by a bit.

Multiplication by recursion - how are values added?

I am trying to understand what is happening in my recursion step return x + times(x, y-1).
Specifically times(x, y-1) Since there is no equation in the function what is happening in the recursion? I don't see how the values are added.
#include <iostream>
using namespace std;
int times(int x, int y)
{
if (x == 0 || y == 0)
return 0;
else if (y == 1)
return x;
else
return x + times(x, y - 1);
}
int main()
{
int x, y;
cout << "Enter two numbers to be multiplied seperated by a space: ";
cin >> x >> y;
cout << "The product is " << times(x, y) << endl;
return 0;
}
The best way to understand recursion is to write all the stack trace on paper.
Let's take an example of times(3, 2):
Call 1: times(3, 2) -> returns 6
Call 2: 3 + times(3, 1) -> returns (3 + 3), that is 6
Call 3: times(3, 1) -> returns 3
So, the final answer is 6.
x + times(x, y-1): It's a way of representing multiplication using addition. For example:
1) 3 * 2 = (3 + 3)
2) 4 * 3 = (4 + 4 + 4 + 4)
#dxiv mentioned it perfectly, a * n = a + a * (n - 1).

C++ for with multiple control statements using comma operator

How does comma operator if it is used in a 'for loop' for writing multiple control statements?
i tried
#include <iostream>
using namespace std;
int main() {
for (int x = 0, y = 0; x < 3, y < 4; ++x, ++y) {
cout << x << " " << y << endl;
}
return 0;
}
and it seems like only the last expression is evaluated. Ty
This is how comma operator works. Its 1st operand x < 3 is evaluated, then the result is discarded; then the 2nd operand y < 4 is evaluated and the value is returned as the return value of the comma operator. x < 3 has not any effects here.
You might want to use operator&& or operator|| for this case, e.g. x < 3 && y < 4 or x < 3 || y < 4 based on your intent.

How does c++ evaluate assignment operators (if statement)

Consider the following
int main() {
int a = 8;
int b = 10;
while (true) {
if (a /= 2 && b < 12) {
b++;
std::cout << b << std::endl;
}
else break;
}
return 0;
}
Now c++ is not my main language, but how does c++ evaluate this if statement?
In this case, when b>=12, the compiler throws the "division by zero" exception, but why?
Now if i wrap the states in parentheses i do not get the exception.
if( (a /= 2) && (b < 12))
Does this have something to do with how c++ evaluates the statements?
If evaluation is not the problem:
I am aware of that
a = (a/2 && b<12)
would not hold either.
P Λ Q does not hold for P Λ ¬Q but the state of P should not be affected? Why is it P gets blamed instead of ¬Q?
if (a /= 2 && b < 12)
is the same as:
if (a /= (2 && b < 12))
so:
2 is evaluated, which is converted to true in the context of an operand to &&. This does not trigger short-circuit evaluation so we continue...
b < 12, which in the case you're talking about is false
So 2 && b < 12 evaluates to false overall
a /= 2 && b < 12 is therefore equivalent to a /= false here, which is equivalent to a /= 0.

string concatenation in c++

Previously, I was using append function to concatenate strings.
However, since doing so requires multiple lines of unnecessary codes, I wanted to try out '+' operator instead. Unfortunately, it didn't go well...
bool Grid::is_available(int x, int y) const
{
if (x < 0 || x >= dim[1] || y < 0 || y >= dim[0])
throw std::invalid_argument("is_available(" + x + ", " + y + "): Invalid coordinate input.");
return occupancy[x][y] == AVAILABLE;
}
The error that I got was "'+': cannot add two pointers" with the code C2110.
All the solutions for this problem said to concatenate one on each line.
Are there actually no way to concatenate multiple strings in C++ in one line? I had no problem with this in C# before.
You can use std::to_string() to convert your integers:
bool Grid::is_available(int x, int y) const
{
if (x < 0 || x >= dim[1] || y < 0 || y >= dim[0])
throw std::invalid_argument(
"is_available(" + std::to_string(x) + ", "
+ std::to_string(y) + "): Invalid coordinate input.");
return occupancy[x][y] == AVAILABLE;
}