what is -> operator? [closed] - if-statement

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
oops, I am not talking about Object Operator, some call it arrow, some call it thingy...
today, while studying DISCRETE STRUCTURES, teacher told us,
if p then q , this is a conditional statement and its written as p -> q ( p implies q),
my question was, what this sign is called, teacher says its if and then sign, then say, its implies sign, but I dont feel it right...
can somebody tell me what this sign is called?? can somebody explain it ? as I was caught in the sign only, I wasnt even able to listen what the teacher was telling about this conditional statement...( teacher sent me out of class saying YOU ARE ASKING FOOLISH QUESTIONSS :( )
One Request..I dont know where to Put this question...as discrete structures relates to programming, so putting my question right here, Forgive me for this if I am at wrong place ( no down-voting , rather please shift this question to approperiate place )

Wikipedia titles it the Material Conditional operator, though I've usually called it the implication operator. In my discrete structures class, we generally read it as either "if p then q" or "p implies q".
For completeness sake, here's the truth table:
p | q | p -> q
--------------
T | T | T
T | F | F
F | T | T
F | F | T

Maybe you could call it simply the arrow sign. We used to say it like "p arrow q".

you can understand it by this way,
p -> q // p derives q. You can reach to q if you are given p.
// q is obtainable from p.

Related

How Gcd works in this code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I tried solving a question on hackerearth and i was not able to solve it so i saw editorial.They gave only code without explanation.Can u expain logic behind why gcd used here?
Question:
Scooby and all of his friends have gathered for a party. There are N friends present. Scooby is really happy to see all of his friends in one place and is excited to greet them.
All N friends are seated in a circle, and are numbered from 0 to N-1. Scooby is initially sitting beside the Ath friend. After greeting one friend, he goes clockwise to the Bth next friend, sits next to him and greets him. He repeats this till he returns to the Ath friend.
In his excitement, it is possible that Scooby misses out on greeting some friends. Your job is to find the number of friends (including A) that Scooby will have greeted before reaching back to A.
Solution given:
int main()
{
int T;
cin>>T;
while(T--)
{
long long N,A,B;
cin>>A>>B>>N;
long long g=gcd(B,N);
cout<<N/g<<endl;
}
return 0;
}
To explain the solution of the above problem I will first show that the answer is - LCM(B,N)/B and then show you how this is equal to N/GCD(B,N).
First Part-
Now assume that when it again reaches A after following the above mentioned steps he would have greeted f friends.(Note no two friends greeted through the above mentioned procedure can be same). Moreover, assume that when he reached A he would have made r rounds of the circle.
Now we can say that -
f * B = r * N = C.
Let this be equal to some constant C. Clearly C is some multiple of B and N moreover, it is the Lowest Common Multiple(LCM) of B and N(as we want to give answer as soon as it reaches for the first time).
So f = LCM(B,N)/B. Note f is the number of friends he greeted so it is the required answer.
Second Part-
For two positive integers a and b with their GCD and LCM g and l respectively, we have the following relation - a*b = g*l.
From the above relation we can say that -
LCM(B,N)*GCD(B,N) = B*N
=> LCM(B,N)/B = N/GCD(B,N)
So finally we have our answer = LCM(B,N)/B = N/GCD(B,N).

Performance when using sequence of operations in a single line in cpp [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Recently I have started looking into C++ from the basics and got to know (to my surprise) that I can give series of expressions in a single line separated by commas in some cases as below
//it'll execute all the expressions mentioned after condition seperated by comma
for(int i=0;condition;++i,++x,cout<<"in for loop"<<endl,z = z*2);
(x>y)? ++z,z1 = z*2, cout<<"printing statement"<<endl:cout<<"condition failed"<<endl,z = z/2;
Here, I have a confusion after this is working. Is it safe to code in that way or is there any problem coding in such a way?
Please clarify!!!
Correct me if i'm wrong anywhere, I'm just curious to know why most of the programmers don't use this way (I haven't seen such kind of lines anywhere)
The comma operator , evaluates each of its operands in sequence. In standardese, there is a sequence point between the evaluation of the left operand and the right operand.
In a expression which contains a comma operator, the value of the left operand is discarded and the expression takes on the value of the right operand. In both of the examples above, the comma operator is used in a void context, so none of the values are used.
So a statement like this where the value of the comma operator is not used:
exp1, exp2, exp3, exp4;
Is equivalent to the following sequence of statements:
exp1; exp2; exp3; exp4;
The first example is equivalent to the following:
for(int i=0;condition;) {
++i;
++x;
cout<<"in for loop"<<endl;
z = z*2;
}
And the second example:
if (x>y) {
++z;
z1 = z*2;
cout<<"printing statement"<<endl;
} else {
cout<<"condition failed"<<endl;
z = z/2;
}
Note that this is considerably more readable that the one-line versions. It's also easier to debug. Since debuggers typically step through code a line at a time, it breaks up the flow and is more granular.
Not indenting and spacing your code is not less costly regarding performance. It is unreadable, confusing and a pain to understand for you and for anyone who'd have to work with it.
Lot of people will prefer a well-syntaxed, beautifully and efficiently-indented code than a top-performance one. You can modify, debug and refract a code which might not work but has the advantage to be understandable.
On the other hand, very few codes remain unchanged and stay unread. There will always be a time when someone, may be you, will have to read it again and if it looks like the one if your OP, it will be very time costly to do.
It is allowed. In my opinion and i say without a reference that in general other programmers do not find your 'for' loop very readable. Sometimes in a for loop you want to do other things then just for (int i = 0; i < 10; ++i){"do something"}For example increment 'i' in every loop with two. Reading code should be like reading a text. If you are reading a book you do not want it to be unnecessary difficult.
Your other question was about the safety of the statement. The biggest problem with the code is that you might get confused about what you are doing exactly. Bugs are caused by human errors (computers are deterministic and are executing machine code which ultimately has been written by a human) and the question about safety mainly depends on how you define it.
To give you some tips. When i just started programming C++ i looked a lot on CPP reference. I will give you a link where you can read about the syntax and what is allowed/possible. On this website there are quite a lot of examples on all kinds of statements. They will in general not put 5 or 6 operations within in a single line. If there are more variables that you want to change then you might want to do that in the scope of the for loop so it will be more readable instead of inside the for loop.
http://en.cppreference.com/w/cpp/language/for

the value of a float variable is going to the wrong if condition [closed]

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 6 years ago.
Improve this question
the value of a float variable is going to the wrong if condition.. doesnt matter if thats a 0 or a -1.. it is just going for the condidtion when variable is to +1
Your assigned value in condition, not check it. First you use == instead of =
There is a difference between = and ==. In your if statement you want to check the value, hence you should use ==.
if(slope == 1)
{
/*...*/
}
You need to use == instead of =.
In c++, assignment operator (=) returns the value equal to the assigned value (this allows writing something like a = b = c). That's why slope = 1 is equal to 1, which, when converted to bool, equals true, and so you end up entering the if section.
The following lines in your code are invalid:
if (slope = 0)
if (slope = +1)
if (slope = -1)
This is because you use the assignment operator = instead of the equality operator ==. As a result of this, your if-statements are not making the desired comparison between the slope and the values +1, 0, or -1.
If we have to compare 2 values, 2 variables, or a variable to a value in an if-statement, then we use the equality operator == to compare them over the =. There are a few exceptions to this; see the following page:
Variable assignment in “if” condition.
Just a side note, I would like to point out that you should use some more whitespace in your code; it helps make it more readable. Also, try posting the code itself in your question instead of posting a snapshot of it.
Good luck!

What does |= signify in C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I was going through the source code of Intel's deep learning framework Caffe, when I came across |=. I've never seen that before in any code. In fact, I found it twice in the code. Line 188:
need_backward |= blob_need_backward_[blob_id];
and line 254:
need_backward |= param_need_backward;
I realize that they both are housed in a for loop which might signify some kind of relation. I'm just assuming.
Thats the 'bitwise OR assignment' compund assignment operator.
x |= y;
is equivalent to:
x = x | y;
There are a number of similar operators: +=, -=, *=, etc.
See: operator_assignment
|= is a compound assignment.
<var> |= <expr> means <var> = <var> | <expr>
It is the bitwise OR equivalent of += for incrementing. You can do this with most mathematical operators in C++.
| is bitwise OR, so you're reassigning a variable to its OR'd result.
it is shorthand for
need_backward = need_backward | param_need_backward;
you are performing bitwise or operation

In Haskell, how to construct lists and make substitution between it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In Haskell, I am going to construct 2 list of chars; one is the 26 chars in the alphabet by the original(A,B,C,D,....Z), all in uppercase. And the other one is the same list but the order of letters is changed, like(B,H,A,I......S). And now I am also going to make a substitution between these two lists, such as when the input is B then returns H, C returns A.
Can anyone could help me out of this?
List literals in Haskell use square brackets [], first of all.
Second of all, I don't know the wording of your assignment, but free of constraint, I would use an association list instead of two lists. An association list is a list of pairs of the form [(a,b)]. The key operation on an association list lookup, defined in Data.List. Look at the type signature and see if you figure out what it does.
If you start with two lists, you can zip them up.
import Data.List
import Data.Maybe
codec = zip "ABCDEF..." "BHAI..." -- String ~ [Char]
encode = map $ flip lookup codec
plainText = "The secret fox"
encodedText = encode plainText
Notice that this gives you a list of [Maybe Char]. I leave it to you to figure out how to extract the chars, since it's actually a design choice. (Do you want to just omit characters that don't show up in the codex, or insert a '!' or something so that the user knows data has been lost? Lots of options here. Notice how the Maybe monad is forcing you to explicitly handle this case).
Assume that the modified alphabet is:
"ORXBMDTCIGJYKAVLSWFNUQEHZP"
The code should look like:
import Data.List
import Data.Maybe
alphabet = ['A'..'Z']
mapping = zip alphabet "ORXBMDTCIGJYKAVLSWFNUQEHZP"
according letter = (snd . fromJust . (find (\t -> f t letter))) mapping
where
f (x, y) letter = x == letter
main = print $ according 'H'
mapping would create the correspondence between alphabet and a modified version. And according would return letter from a modified, based on the original one.