The comparison statement [duplicate] - c++

This question already has answers here:
Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?
(5 answers)
Closed 2 years ago.
#include <iostream>
#include<string>
using namespace std;
int main() {
int a=30,b=1, c=5, i=10;
i=b<a<c;
cout<<i<<endl;
return 0;
}
Hello
I have question about this code.
when I run the code, I get the result which is 1, but I am not sure the relationship about i=b<a<c;
and why I get the result
Thank you.

int a=30,b=1, c=5, i=10;
i=b<a<c;
i=b<a<c
is interpreted as:
compute (b<a)
compare the result to see if it is < c
place that result into the variable i
So this is what happens:
b<a is evaluated as 1<30, which is true, so the result is 1 .
next this 1 result is compared to c, which is evaluated as 1< 5, which is also true, so that result is also 1
this final 1 result is set as the value in the variable i.

Related

why does the output change when i change z -1 to z--? [duplicate]

This question already has answers here:
Pre vs Post Increment
(3 answers)
Closed 8 months ago.
I have the following c++ program:
#include <iostream>
using namespace std;
//looping through arrays backwards
int main() {
int a[3] {1, 2, 3};
int x = sizeof(a), y = sizeof(int), z = x / y;
for(int i = z - 1; i >= 0; i--) {
cout << a[i] << " ";
}
return 0;
}
And it outputs 3 2 1. But if I change the first parameter in the for loop to int i = z--;, it outpus 2 3 2 1 and I don't understand why. Aren't z - 1 and z-- supposed to be the same thing? Could someone please explain why? Also, I'm a begginer in C++ and I'm learning via the W3Schools tutorial about it. Thanks!
The expression z-- evaluates to z, then - as a side effect - z is decremented (scheduled according to scheduling rules). This means, you're essentially saying int i = z in your loop (and then decrement z, but it's not used anymore) - therefore, your code has UB. The 2 printed is purely coincidental, anything might be printed or anything could happen in your code. If you'd like to use --, use it as prefix, i. e., int i = --z.

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;
}
}
}
}

How to fix rand()/RAND_MAX in a method that always produces 0.0000000? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Integer division always zero [duplicate]
(1 answer)
Random number c++ in some range [duplicate]
(6 answers)
Closed 3 years ago.
I need to produces numbers between 0 and a max (seen in code as assetMax). In the code, the rand()/RAND_MAX always produces 0 and I cannot seem to figure out why. I use the rand() function immediately before it to produce values in a range and it works completely fine. However, here it does not.
I have tried to switch the order of the variables, create the random number in a separate double before multiplying the two, and the header.
void cPortfolio::randomize(cProblem &portfolioProblem) {
int assetCount = 6 * rand() / RAND_MAX + (portfolioProblem.assetMax-8); //this line works as expected
int test;
for (int i = 0; i < assetCount; i++) {
double num = rand() / RAND_MAX; //this always produces 0.0000
int test = num * (portfolioProblem.assetNum); } `} //cannot format these correctly please ignore the brackets

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.

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.