how to use more that logical operator in c++? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My code is trying to determine if a triangle with the following side lengths (x, y, z) is an isosceles triangle.
It doesn't work, my code is :
if(x == y && y != z || y == y && y != z || x == z && z != y){
cout << "isosceles triangle" << endl;
}

first, you need to separe each case with ( ):
if((x == y && y != z) || (y == y && y != z) || (x == z && z != y) ){
so , you can have one case or another case or another case.
second:
y == y && y != z
is wrong (mathematically, not programmatically) this makes no sense for validating an isosceles triangle. the correct would be
y == z && x != z
the final result is
if((x == y && y != z) || (y == z && x != z) || (x == z && z != y) ){

I suggest using parenthesis:
if ((x == y && y != z) || (y != z) || (x == z && z!= y))
{
//...
}
I removed the expression y == y because it is always true.

Related

Simplifying a code snippet - vector rotation in a circle

Given the following code pattern, wherein I am trying to state a vector direction in increments of 45 degrees over the integers int x and int y, inside of a circle positioned at the origin
// x == 0 && y == 0 case is already taken cared of
if(x > 1) {
if(y == 0) {
// horizontal right
m_rotation = 0;
}else if(y < 1) {
// diagonal down right
m_rotation = 315;
} else if(y > 1) {
// diagonal up right
m_rotation = 45;
}
} else if(x == 0) {
if(y < 1) {
// vertical down
m_rotation = 270;
} else if(y > 1) {
// vertical up
m_rotation = 90;
}
} else if(x < 1){
if(y == 0) {
// horizontal left
m_rotation = 180;
}else if(y < 1) {
// diagonal down left
m_rotation = 225;
} else if(y > 1) {
// diagonal up left
m_rotation = 135;
}
}
I am looking for an elegant way to make this compact. I know there's the spaceship operator <=>, but I need to restrict myself to C++17.
Things I have tried
Nesting ternary operators with m_rotation = x > 1? (y < 1? (y == 0? 0: 315): 45): (x == 0? (y < 1? 270: 90): (y < 1? (y == 0? 180: 225): 135));, but this looks weird
I tried putting the x == 0 case inside x < 1 case and transform the later into else, but that does not simplify enough the code
Using absolute values to compare x and y, but I quickly get lost
Nothing else really, I don't know what else to try
Something like
constexpr int rotation[3][3] = {
{225, 180, 135},
{270, 0, 90},
{315, 0, 45},
};
if (x != 0 || y != 0) // if (!(x == 0 && y == 0))
m_rotation = rotation[1 + sign(x)][1 + sign(y)];
There is a closed form:
// standard sign functions
int xs = x < 0 ? -1 : x > 0;
int ys = y < 0 ? -1 : y > 0;
return 180 - 45 * (xs + 2) * ys + 90 * (xs * xs + xs) * (ys * ys - 1);
or shorter
return 180 * (x < 0 || y) - 45 * (xs + 2) * ys;

How to make a loop to determine if 2 numbers belong in a given range

I am having problems making a loop which stops when both x and y are in the range/interval [0,1] in c++.
double x;
double y;
while(condition)
{
if(x < 0)
{
x = -x;
}
else
{
x = 2 - x;
}
if(y < 0)
{
y = -y;
}
else
{
y = 2 - y;
}
}
This method with 2 loops works:
while((x < 0) || (x > 1)) {do sth}
while((y < 0) || (y > 1)) {do sth}
This doesn't work:
while(!((x >= 0) && (x <= 1)) && !((y >= 0) && (y <= 1))) {do sth}
And this doesn't work either:
while(((x < 0) || (x > 1)) && ((y < 0) || (y > 1))) {do sth}
This makes an infinite loop (in my case):
while(((x < 0) || (x > 1)) || ((y < 0) || (y > 1))) {do sth}
Note: {do sth} changes x and y if needed so they will eventually go in that interval (same as in the first block of code).
Note 2: By doesn't work I mean it never goes in the loop when x is in the interval and y < 0 (and some other cases).
while ( !( (x>=0 && x<=1) && (y>=0 && y<=1) ) ) should be the combined conditional check.
I'd go for a dedicated function with a speaking name: so you can still understand your code in a couple of weeks :-), e.g.
auto check_outside_interval_0_1 = [] (double const a) {
return a < 0.0 or 1.0 < a;
};
while( check_outside_interval_0_1(x) or
check_outside_interval_0_1(y) ) {
// ... do your things here
}

How to have an if() split by && and not ||

I'm trying to do something like this in c++
If x is equal to either a or b or c
and y is equal to either d or e or f
and z is equal to either g or h or i, it would turn true and execute the code
I am a bit lost in this
if(x==a||x==b||x==c && y==d||y==e||y==f && z==g||z==h||z==i){
// Do x
}
Just use && and ||, with parentheses to make the grouping clear.
if ((x == 'a' || x == 'b' || x == 'c')
&& (y == 'd' || y == 'e' || y == 'f')
&& (z == 'g' || z == 'h' || z == 'i')) {
// execute code
}
If you look at C++ operator precedence you'll find that && has higher precedence than ||.
That means that your if statement
if(x==a||x==b||x==c && y==d||y==e||y==f && z==g||z==h||z==i)
is the same as
if (x == a || x == b || (x == c && y == d) ||
y == e || (y == f && z == g) ||
z == h || z == i)
By using parentheses, you can change it to work the way you want:
if ((x == a || x == b || x == c) &&
(y == d || y == e || y == f) &&
(z == g || z == h || z == i))
It is a straightforward translation from what you said to C++ code:
if ((x == a || x == b || x == c) &&
(y == d || y == e || y == f) &&
(z == g || z == h || z == i))
{
}
Most real programs don't have conditionals this complex. Split up your logic into logical tests.
From the previous answers, the important point is that in C++, you missed that the AND operator && has a higher operator precendence than the OR operator ||.
This can be compared to how the multiplicative operators *, /, % of numbers have higher precedence than additive operators + and - in algebra.
Regroup the parathetheses so you have these three requirements of what x, y, and z have to be, and the statement is true when all three are correct. (Technically, this is called a product of sums form, as the if statement requires the truth of all three smaller statements (the AND), where each of the smaller statement uses only OR statements.).
if((x==a||x==b||x==c) && (y==d||y==e||y==f) && (z==g||z==h||z==i)){
// Do x
}
Your original code would be equivalent to
if(x==a || x==b || (x==c && y==d) || y==e || (y==f && z==g) || z==h || z==i){
// Do x
}
For larger if-statements, you can also use newlines but of course that will take up more lines.
if((x==a||x==b||x==c)
&& (y==d||y==e||y==f)
&& (z==g||z==h||z==i)
&& ... other conditions)){
// Do x
}
If you do not want such cumbersome and long if-statement notation, you can consider breaking into logical tests (the if-condition is the "product" of the logical tests), namely
bool a1 = (x==a||x==b||x==c);
bool a2 = (y==d||y==e||y==f);
bool a3 = (z==g||z==h||z==i);
bool a4 ...
if (a1 && a2 && a3 && a4 && ..) {
// Do x
}
Try using parentheses to separate your questions:
if( (condA || condB || condC) && (condD || condE || condF) && (...)) and so on.
(condA = condition A)
Going a little overboard here (but just a bit):
if (element_of(x, {a, b, c}
&& element_of(y, {d, e, f})
&& element_of(z, {g, h, i}))
{
foo();
}
with element_of defined as:
template <class T>
auto element_of(const T& element, std::initializer_list<T> list) -> bool
{
return std::find(list.begin(), list.end(), element) != list.end();
}
Or a even a little bit more overboard (this is just for fun, I don't recommend it):
if (Elem{x}.element_of({a, b, c})
&& Elem{y}.element_of({d, e, f})
&& Elem{z}.element_of({g, h, i}))
{
foo();
}
with Elem:
template <class T> struct Elem
{
T element;
auto element_of(std::initializer_list<T> list) -> bool
{
return std::find(list.begin(), list.end(), element) != list.end();
}
};
template <class T> Elem(T) -> Elem<T>;

What is equivalent to "!((x > y) && (y <= 0))"

What is the equivalent to the condition:
!((x > y) && (y <= 0))
Is it:
!(x > y) && !(y <= 0)
(x <= y) || (y > 0)
(x < y) || (y > 0)
After initializing the variables to make them into y = -3 and x = -3 I was able to isolate only the second one to display the same results as the first. Does this mean that this is the only one equivalent to it?
How exactly do you use truth tables or the "De Morgan's Law"?
I believe its the second one : (x <= y) || (y>0)

(C++) Random numbers are equal, but program says they aren't

I am a beginner, and I created a slot machine simulator. The wheel is spun and x, y, and z are set to random numbers. I have an if statement that checks to see if x == y == z. When the program runs, and the numbers are in fact equal, it runs my code that says they are not equal. Why does this happen?
For example, the cout statement will say 2 -- 2 -- 2, then it goes to my if statement for when they are not equal, and I get the "you lose" code.
I should add, this does not happen every time. Sometimes it properly executes the if statement for when they are equal. It is very odd.
srand(time(0));
int x = (rand() % 2) + 1;
int y = (rand() % 2) + 1;
int z = (rand() % 2) + 1;
std::cout << "The spin results are... " << x << " -- " << y << " -- " << z << std::endl << std::endl;
if( x == y == z)
{
std::cout << "You win!\n\n";
playerCoins = playerCoins + (coinsBet * 2);
}
else
{
std::cout << "You lose!\n\n";
}
x == y may result in 0 or 1, depending on their true or false value. Then 0 or 1 is compared with z, this why the given result is false.
The correct method is to check if x equals z and y equals z, which of course also means that x equal y. (x == z) && (y == z)
x == y == z does not do what you think it does. Use x == y && y == z instead.
The expression if (x == y == z) is evaluated to false.
Pretend x, y, and z all hold the value 2:
Because (x == y) is then true / 1. And z is holding the value 2, the if statement will check:
if ((x == y) == z)
// 1 == 2
Which becomes:
if (1 == 2) {} // false
You can fix that by doing this:
if ((x == y) && (y == z)) { /* ... */ }
You should use Logical operators.
the expression, x == y == z is wrong.
it should be done this way.
if((x == y) && (y == z)) { //or... if ((x == y) == z)
//your code goes here...
}
More information about logic operators, Here.