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

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

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.

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

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

Finding the square of a number without multiplication [duplicate]

This question already has answers here:
Making a square() function without x*x in C++
(7 answers)
Closed 4 years ago.
I'm a beginner in programming and trying to learn C++ by the book Programming principles and practice using C++. In some parts of the book there are little exercises that you can try to do, one of this exercises is about calculating the square of a number, here is what my book says :
Implement square() without using the multiply operator, that is, do the x * x by repetead addition (start a variable result to 0 and add x to it x times).
I've already found a solution for this program but my first tentative was something like this :
#include <iostream>
int main()
{
int a = 0;
std::cout << "Enter an integer value : ";
std::cin >> a;
while (a < a * a)
{
a += a;
std::cout << a << "\n";
}
}
I know this code is wrong but I can't understand the output of the progam, if I enter 5 the program prints 10 20 30 40 50 until 8000, why the for loop doesn't stop when a is greater than its square ? I'm just curious to undersant why
Using multiplication when trying to avoid multiplication seems broken. What about this:
int r = 0;
for (int n = 0; n < a; ++n) {
r += a;
}
why the for loop doesn't stop when a is greater than its square ?
Because it never is. If you compare the graph of y=x^2 against the graph of y=x, you will see that the only time y=x is above, is when 0 < x < 1. That's never the case for integers1. Now, since we're talking about computers with limited storage here, there is a thing called overflow, which will cause a very large number to become a very small number. However, signed integer overflow is undefined behavior in C++. So once your loop gets to the point where overflow would happen, you cannot rely on the results.
1. Note that your loop is not set to stop just when a is greater than its square, but when it is greater than or equal to its square. So, your loop will actually stop if a is 0 or 1.

How to quickly determine how many digits in an int? [duplicate]

This question already has answers here:
Finding the length of an integer in C
(29 answers)
Closed 9 years ago.
I know I can do:
n = floor(log10(i)) + 1;
Or I can do a quick loop:
while(i) {
n++;
i/=10;
}
Is there any better way than a complicated math operation, or a loop to achieve the goal? For example: if i = 1234, then n = 4.
The shortest way I know of (not computationally, just in terms of typing) is to call snprintf(3):
int n = snprintf(NULL, 0, "%d", i);
convert it to a string (itoa) and count the number of characters? (might be not the best performance-wise though)