Let's say there is a sequence of 4 numbers. All of these numbers repeat themselves every fourth time. The numbers are, in correct order: 2, 4, 8, 6. So for example: ID 1: 2 ID 2: 4 ID 3: 8 ID 4: 6 ID 5: 2 ID 6: 4 ID 7: 8 And so on. My question is, can i calculate and display one of the numbers that corresponds to that sequence ID (2, 4, 8 or 6) by only receiving the sequence ID as the input. I just need the mathematical equation for finding it, but i'll be equally happy if someone would give me this equation as a ideally, C++ script. Ill be happy for any answer. Thanks.
I tried to find a way to find the number using the remainder from the division of the ID entered by the user by 10, so for example:
if(IDint % 10 == 1) cout << 2;
But I quickly realised that not every ID ending by 1 will have the number 2, so I'm kind of lost at this point.
Thanks again.
You were on the right track using the remainder operator
%(modulo), however you used the wrong divisor. A divisor of 10 will repeat a sequence of 10 numbers, whereas what you need is 4 numbers. So the divisor should be 4 in your case.That's half of the equation. Then, it's a simple transformation to go from the 1-based sequence ID to a 0-based system for the modulo operation. To do that you simply subtract 1 from your sequence ID first. And if you're more comfortable dealing with 1-based values after wrapping, then you can add 1 again.
So your math should have been:
This is a lot of repeated code though. It would be cleaner to store your sequence in an array or vector, and then calculate a (zero-based) index for it.
Here's an example of selecting values from any repeating non-zero length sequence starting from a 1-based ID:
Output: