Can not understand working of a line [duplicate] - c++

This question already has answers here:
How do I use the conditional (ternary) operator?
(10 answers)
What does the question mark character ('?') mean in C++?
(8 answers)
Closed 6 years ago.
I took few help online to complete my task. And I found this code but I do not know the actual working as I have never used such syntax before in c++. The (?) Question mark and (:) colon.Can any one provide a simple general syntax code explaining same line?
x = (i-coins[j] >= 0)? table[i - coins[j]][j]: 0;

This means
if (i-coins[j] >= 0)
x = table[i - coins[j]][j];
else
x = 0;

This is called ternary operator, and it is used in place for a short if-else statement.
int factorial(int number) {
if (number < 1) {
return 1;
} else {
return number*(number-1);
}
}
The above function can be summed up using a ternary operator:
int factorial(int number) {
return (number < 1) ? 1 : number*(number-1);
}

Related

Why is my modulo operator not working properly? [duplicate]

This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
Closed 2 years ago.
Given the equation y^2=x^3+2*x+4 mod 7 i want to find all possible solutions with x and y being in the range of 0 to 6. I have written the following program:
for (int y=0;y<7;y++)
{
for (int x=0;x<7;x++)
{
if ((x^3+5*x+4)%7==(y^2)%7)
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
}
}
However, the output of this program is wrong. For example, the program prints out (4,1), however this does not satisfy the equation. How can i fix this?
I think the problem is that x^3 and y^2 are not power operation, it is xor operation indeed, so, you can use x*x*x and y*y instead:
for (int y=0;y<7;y++)
{
for (int x=0;x<7;x++)
{
if ((x*x*x+5*x+4)%7==(y*y)%7)
{
std::cout<<"("<<x<<","<<y<<")"<<std::endl;
}
}
}
}

What is the internal functioning of the following code? [duplicate]

This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]
(6 answers)
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
I am trying to understand how the following lines of code work in c++.
int main(){
int i;
i = 1 + (2,3,5,3,6);
cout<<i<<endl;
return 0;
}
Output: 7
Basically, the answer is the sum of 1 and the last integer in between the parentheses.
(2,3,5,3,6) turns out to be 6.
Hence 1 + 6 = 7
You can verify with a print statement
printf("\n%d\n", (2,3,5,3,6));
It will print 6 only.

Why can't I use return in my ternary expression but I can in a regular if-else statement? [duplicate]

This question already has answers here:
return statement in ternary operator c++
(5 answers)
Closed 3 years ago.
I am writing a simple factorial function to practice recursion within c++.
I wanted to write it in ternary format but it was not working so I wrote it out first to see if it worked the more traditional way.
Ternary Format:
num > 1 ? num * factorial(num-1) : return 1;
Traditional :
if (num > 1){
return num * factorial(num-1);
}
else {
return 1;
}
I am simply printing the result of factorial(5) which should display 120. While it works for the second it does not for the first. Why can I not use return in the ternary format? Or, am I using it wrong?
You can use factorial just fine in the conditional form, but you can't use return. return is a statement, whereas the conditional operator expects expressions. Conveniently, the conditional operator itself is an expression, which lets you do this.
return num > 1 ? num * factorial(num-1) : 1;
Note that we're returning the result of the whole expression, not just the num > 1 piece. If I wanted to be redundant with parentheses, I could do this, which is semantically equivalent.
return (num > 1 ? num * factorial(num-1) : 1);

what does this line of code do? player = (player % 2) ? 1 : 2; [duplicate]

This question already has answers here:
What does the question mark character ('?') mean in C++?
(8 answers)
Closed 7 years ago.
I can`t understand this line
why we use question mark "?" in it.
there are 2 player 1 and 2 .
player = (player % 2) ? 1 : 2;
This is a conditional if, and is the same as:
if(player % 2)
player = 1; // Odd
else
player = 2; // Even
Another way to do this without an if branch:
player = 2 - (player & 0x01);
The least significant bit is zero for even numbers.
It's the ternary operator.
This line of code will set player to 1 if player originally was odd, and to 2 if it was even.
It's the ternary operator. It takes this form:
boolean expression ? a : b;
Which translates to:
If this expression is true, then a else b
It's often used as the right-hand expression in assignment operators. In your case player is being assigned 1 or 2 based on whether they are even or odd.
It meaning that if the Condition is true so Player have the Value 1 else it have 2.
if(player % 2) {
player = 1;
} else {
player = 2;
}

C++ code printing strange values [duplicate]

This question already has answers here:
Math-like chaining of the comparison operator - as in, "if ( (5<j<=1) )" [duplicate]
(4 answers)
Closed 8 years ago.
i am having a very silly but strange problem. When i am trying to compile and run the following code my compiler is printing "ggl" but i think it shouldn't. It is strange that after doing so much programming i am stuck here. What exactly is the problem? Can someone please help me out? Thanks in advance !!
#include <iostream>
using namespace std;
int main() {
int t=8;
if(1<t<5){
cout<<"ggl";
}
//cout<<aa;
return 0;
}
This line doesn't do what you think it does
if(1<t<5)
You would have to say
if (1 < t && t < 5)
The first version says
if ((1 < t) < 5)
Which evaluates to
if (true < 5)
if (1 < 5)
Which is always true.
Your if condition effectively says if ((1 < t) < 5), which is always true, because (1 < t) is either 1 or 0 (1 < 8 evaluates to 1).
Since chained comparisons do not (usually) work in C++, you'll need to check the condition differently:
if (1 < t && t < 5) {
cout << "ggl";
}
This
if(1<t<5)
does not do what you think it does. It does not determine whether t is between 1 and 5. You want
if ((1<t) && (t<5))
What it actually does is take the value (1<t) (which will 1 if 1<t and 0 otherwise), then see if that value is less than 5, which it always will be.