C/C++ What does the modulus operator mean in a C++ if statement?

  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    C++ If statement
AI Thread Summary
The discussion centers on understanding an if statement that utilizes the modulus operator. The expression checks if a specific element in the 'randoms' array, determined by the calculation (x * 3) + y, is odd. The modulus operator (%) returns the remainder of a division, so when used with 2, it indicates whether the number is odd (true) or even (false). If the element is odd, the code within the if statement will execute.
FallArk
Messages
127
Reaction score
0
I have run into an if statement as shown below:

if (randoms[(x * 3) + y] % 2)

I don't really understand what this means.
Thank you for helping!
 
Technology news on Phys.org
The [m]%[/m] symbol is the modulus operator, and so the expression [m]n % 2[/m] will be true if n is odd (there is a remainder when dividing by 2) and false if n is even (there is no remainder when dividing by 2).

So, the statement:

[m]if (randoms[(x * 3) + y] % 2)[/m]

Is looking to see if the [m]((x*3) + y)[/m]th element of the [m]randoms[/m] array is odd, and if so, execute some code.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top