I'm exercising opreators in C++ and I don't understand the output of the code bellow
int x = 21, z = 33, y = 43;
cout << (!(z < y&& x < z) || !(x = z - y)) << endl;
I wrote it with the thought to be true and I understand it as "it's not the case z is less than y and x is less than z (which is false) or it's not the case x is equal to the difference of z and y (which is true)" so I expected output 1 (=true) and I'm confused that's not the case. Can you explain me where I'm making a mistake?
edit: Thanks for the answers, it's funny how I made such trivial mistakes I actually read about.
The part that you misinterpreted:
!(x = z - y))
x = z - y is assignment. It yields -10 as result. -10 is not 0, hence negating it yields false.
Now, first part of the expression:
!(z < y&& x < z)
!(33 < 43 && 21 < 33)
!(true && true)
!(true)
false
Putting it together:
(false || false) == false
This = is an assignment operator. It assigns values.
This == is a comparison operator. It is used to compare two values.
int value = 5, value2 = 12;
if(value == value2)
{
// do something if value and value2 are EQUAL (which they are not)
}
See this link for more information on operators in C++.
Related
if(x < y)
x = x + 1
else
x = 3 * x
{x < 0}
my answer :
x+1<0 --> x<-1
x*3<0 --> x<0
so weakest condition is (x<-1) ∩ (x<0) = x<-1
However, the answer sheet says that x<0 is the answer. How can I get the weakest condition?
I'm working on an assignment that gives an integer N and tasks us to find all possible combinations of X, Y such that X + Y = N and Y = X with one digit removed. For example, 302 would have the following solutions:
251 + 51 = 302
275 + 27 = 302
276 + 26 = 302
281 + 21 = 302
301 + 01 = 302
My code to accomplish this can find all of the correct answers, but it runs too slowly for very large numbers (it takes roughly 8 seconds for the largest possible number, 10^9, when I would like for the entire algorithm of up to 100 of these cases to complete in under 3 seconds).
Here's some code describing my current solution:
//Only need to consider cases where x > y.
for(int x = n * 0.5; x <= n; x++)
{
//Only considers cases where y's rightmost digit could align with x.
int y = n - x,
y_rightmost = y % 10;
if(y_rightmost == x % 10 || y_rightmost == (x % 100) / 10)
{
//Determines the number of digits in x and y without division. places[] = {1, 10, 100, 1000, ... 1000000000}
int x_numDigits = 0,
y_numDigits = 0;
while(x >= places[x_numDigits])
{
if(y >= places[x_numDigits])
y_numDigits++;
x_numDigits++;
}
//y must have less digits than x to be a possible solution.
if(y_numDigits < x_numDigits)
{
if(func(x, y))
{
//x and y are a solution.
}
}
}
Where func is a function to determine if x and y only have a one digit difference. Here's my current method for calculating that:
bool func(int x, int y)
{
int diff = 0;
while(y > 0)
{
if(x % 10 != y % 10)
{
//If the rightmost digits do not match, move x to the left once and check again.
x /= 10;
diff++;
if(diff > 1)
return false;
}
else
{
//If they matched, both move to the next digit.
x /= 10;
y /= 10;
}
}
//If the last digit in x is the only difference or x is composed of 0's led by 1 number, then x, y is a solution.
if((x < 10 && diff == 0) || (x % 10 == 0))
return true;
else
return false;
}
This is the fastest solution that I've been able to find so far (other methods I tried included converting X and Y into strings and using a custom subsequence function, along with dividing X into a prefix and suffix without each digit from the right to the left and seeing if any of these summed to Y, but neither worked as quickly). However, it still doesn't scale as well as I need it to with larger numbers, and I'm struggling to think of any other ways to optimize the code or underlying mathematical reasoning. Any advice would be greatly appreciated.
Consider solving a simpler solution first:
Finding X and Y such that X + Y = N
In pseudo-code you steps should look like this:
loop through the array and with every given item do the next:
add this number to Set and check whether there is N - item
This will work as O(n) complexity for unique array.
So improve it to work with duplicated numbers by looping through an array first and adding counter of duplicates for every number. Use some kind of Dictionary for c++ or extend Set. And every time you find the necessary number check for counter.
After doing that you will just have to write this "digit check" function and apply it when finding the value in Set.
I've been searching the documentation and experimenting myself but I can't figure out whether or not you can set a variable using an IF statement. For instance
x = if y >= 1;
would set x equal to 1 if y is greater than or equal to 1 and 0 otherwise. Is this possible in SAS? Do you have to do
if y >= 1 then x = 1; else x = 0;
Almost there... just remove the if :
x = (y >= 1) ;
Remember, all evaluations equate to either true (1) or false (0), so you can simplify lots of code in this manner, especially with the addition of ifn and ifc.
x = (y >= 1) ;
z = (index(name,'Dave') > 0) ;
q = ifc(x and not z,'This','That') ;
Or mixing boolean & regular algebra :
points = ((product = 'SHOES') * 100 * sale_price) + ((product = 'HATS') * 200 * sale_price) ;
Before reading this question please consider that it is intended for use with the Z3 solver tool and it's c++ api (everything is redefined so it's not normal c++ syntax)
Can someone explain how do I mix boolean logic with integers (programing wise)?
Example:
y = (x > 10 and x < 100) //y hsould be true or false (boolean)
z = (y == true and k > 20 and k < 200)
m = (z or w) //suppose w takes true of false (boolean)
I tried with the examples given in the c++ file but I can't figure out how it works when mixing integer arithmetic and boolean.
Writing answer assuming you a beginner of c++.
May be you are looking for this.
bool y,z,m,w;
int x, k;
y = (x>10 && x<100);
z = (y == true && k > 20 && k < 200);
m = (z || w);
Let see what this line means:
y = (x>10 && x<100);
here if x is greater than 10 x>10 results true. In the same way if x is less than 100 x<100 results true. if both of them are true, the right side results true, which will be assigned to y.
|| means or.
I have a problem doing one of the questions from my programming lab.
The question was like "Given the variables x, y, and z, each associated with an int, write a fragment of code that assigns the smallest of these to min."
And my work area looks like
if x < y and x < z:
x = min
if y < x and y < z:
y = min
if z < x and z < y:
z = min
When I turned in, the feedback said:
Remarks:
⇒ Unexpected identifiers: and
More Hints:
⇒ Solutions with your approach don't usually use: <
although I tried may ways to figure it out but none of them worked.
Plz help.
Thx.
For the first reply, the system said:
Remarks:
⇒ Unexpected identifiers: and
More Hints:
⇒ Solutions with your approach don't usually use: <
Problems Detected:
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ min does not contain the correct value
⇒ y was modified
⇒ z was modified
For the second reply, the system said:
Remarks:
⇒ Unexpected identifiers: and, def, minist
⇒ You have to use the min variable .
⇒ You should use an assignment operator (=) in this exercise.
More Hints:
⇒ You almost certainly should be using: =
⇒ You almost certainly should be using: min
⇒ I haven't yet seen a correct solution that uses: , (comma)
I see three issues with the code as you've shown it in the question.
The first is that your indentation is a bit messed up. You probably want all your if statements to be at the same level, with the corresponding assignment statements indented under each one:
if something:
foo = bar
if something_else:
foo = baz
# etc
You could also use elif statements instead of the second and third if, since you never expect to have more than one of the conditions true at the same time.
The second issue is that you're doing your assignment statements backwards. If you want to assign the value in a variable x to a new variable named min, you should put min on the left side and x on the right:
min = x
This is part of the reason you're getting lots of confusing errors. Before your code runs, min is the name of a builtin function (which I suspect you'll learn about later in your class). When you did x = min you were replacing the old x value with a reference to the function.
The last thing is a logic issue. Your comparisons are all using the < operator to test if one value is smaller than the others. The issue is what happens if two of the values are equal. If x and y have the same value, both x < y and y < x will be False. That's not very good for your code, and so you probably want to use the <= operator instead. It tests if the left side is less than or equal to the right side. With this version, if you're using elifs, you can actually do away with the last condition and just use else, since if neither x or y is less than or equal to the other values, z must be the smallest value by process of elimination.
Anyway, here's the code with all three issues fixed:
if x <= y and x <= z:
min = x
elif y <= x and y <= z:
min = y
else: # no "z <= x and z <= y" check needed here, will always be true if reached
min = z
Maybe the tab problem.
You can try this:
if x < y and x < z:
x = min
if y < x and y < z:
y = min
if z < x and z < y:
z = min
I make a sample:
#-*- coding:utf-8 -*-
def minist(x,y,z):
if x < y and x < z:
return x
if y < x and y < z:
return y
if z < x and z < y:
return z
x = 1
y = 2
z = 3
test = -1
test = minist(x,y,z)
print(test)
This sample will print 1.