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

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.

Related

The comparison statement [duplicate]

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.

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

C++ Random doubles from -1 to 1 [duplicate]

This question already has answers here:
generate random double numbers in c++
(10 answers)
Closed 4 years ago.
I need to randomly generate numbers (using random()) from -1 to 1 that are doubles. For example the output would be something like:
1,
-0.3324,
0.7821,
0.9823,
-0.111
etc... this is what I was trying to do walk_Length = (double)rand()%2 - 1;
You might get away with something like
double walk_Length = static_cast<double>(rand()) / RAND_MAX * 2 - 1;

Can not understand working of a line [duplicate]

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