C++ Modulus Operator Understanding - c++

I am just starting out programming and reading thru C++ Programming Principles and Practice. I am currently doing the Chapter 3 exercises and do not understand why this code I wrote works. Please help explain.
#include "std_lib_facilities.h"
int main() {
cout<<"Hello, User\n""Please enter a number (Followed by the 'Enter' key):";
int number=0;
cin>> number;
if (number%2) {
cout<<"Your number is an odd number!";
} else {
cout<<"Your number is an even number\n";
}
return 0;
}

When number is odd, number%2 is 1.
if (number%2) {
is equivalent to
if (1) {
Hence, you get the output from the line
cout<<"Your number is an odd number!";
When number is even, number%2 is 0.
if (number%2) {
is equivalent to
if (0) {
Hence, you get the output from the line
cout<<"Your number is an even number\n";

The modulus operator simply determines the remainder of the corresponding division problem. For instance, 2 % 2 returns 0 as 2 / 2 is 1 with a remainder of 0.
In your code, any even number entered will return a 0 as all even numbers are, by definition, divisible by 2 (meaning <any even number> % 2 == 0)
Likewise, any odd number entered will return 1 (for instance, 7 % 2 == 1 as 7 / 2 has a remainder of 1).
In c++, like in many programming languages, numeral values can be treated as booleans such that 0 relates to false while other numbers (depending on the language) relate to true (1 is, as far as I know, universally true no matter the programming language).
In other words, an odd number input would evaluate number % 2 to 1, meaning true. So if (number % 2), we know that the input number is odd. Otherwise, number % 2 must be false, meaning 0, which means that the input number is even.

"if" statements works on boolean values. Let's remember that boolean values are represented by "false" and "true", but in reality, it's all about the binary set of Z2 containing {0, 1}. "false" represents "0" and "true" represents "1" (or some people in electronics interpret them as "off/on")
So, yeah, behind the curtains, "if" statements are looking for 0 or 1. The modulus operator returns the rest of a / b. When you input any number and divide it by 2, you are gonna get a rest of 0 or 1 being it pair or an odd number.
So that's why it works, you will always get a result of 0 or 1 which are false and true by doing that operation that way.

think of modulus in terms of this:
while (integer A is bigger than integer B,)
A = A - B;
return A
for example, 9%2 -> 9-2=7-2=5-2=3-2=1
9%2=1;
the statement if (number%2) is what is called a boolean comparison (true false). Another way to write this statement identically is if(number%2 != 0) or if(number%2 != false) since false and zero are equivocal. You're taking the return value of the modulus operator function (a template object we will assume is an integer) and inserting it into an if statement that executes if the input does not equal zero. If statements execute if the input is -5,9999999,1-- anything but zero. So, if (2) would be true. if(-5) would also be true. if(0) would be false. if(5%2) would be 1 = true. if(4%2) would be if(0) = false. If it is true, the code in the body of the if statement is executed.

Related

Has number unique digits?

I am currently trying to solve a problem set on codeforce where I need to check if an positive integer number has unique digits. My solutions includes a while loop and two for loops, which is quite a lot of for such an easy task.
I found a more elegant solution but I don't fully understand how the code works. I have commented it with my remarks. Could someone explain to me the second 2) and fifth 5) part?
int unique(long long int number){
/* 1) create array/list with 10 elements, the first element seen[0]
* is equal to zero */
char seen[10] = {0};
/* 2) what is the meaning of while(some random integer number)? I thought
* that the argument must be a statement that is either true or false. */
while (number) {
int digit = number % 10; // 3) get the last digit of the number
number /= 10; // 4) removes last digit of the number
/* 5) Could someone explain to me what seen[digit]++ does. And when its
* true or false? */
if (seen[digit]++)
return 0; /* not unique */
}
return 1; /* unique */
}
Of course I tried to figure out the fifth part on my own but
#include <iostream>
using namespace std;
int main(){
char seen[10] = {0};
cout << seen[7]++ << endl;
}
print outs nothing.
I'll go by parts:
2 ) The implicit conversion between a numeric type and bool returns false if the number is zero and true otherwise. You could read while(number) like while(number != 0)
5 ) This works the same way: seen[digit]++ is an expression with the same value as seen[digit] but that then increments its value (check how post-increment works). Therefore, the first time that digit is seen, seen[digit]++ has the value 0 (so the first time the condition is not met) and increments its value to 1 (so the second time the condition will be met, making the function return).
while(number) means the cycle will repeat until number is not zero. Non-zero number is equal to true
seen[digit]++ does following:
it return current value of seen[digit]. For the first time it will be zero - as no number met.
after returning current value - it increase value by one. So for the first call it will return 0 and the seen[digit] will become 1.
So for the second call it will return 1 - that mean this number already met, so it is not unique.
Q.1 what is the meaning of while(some random integer number)? I thought that the argument must be a statement that is either true or false.
=> Yes you are right while condition checks for true and false. In case of integer, 0 is treated as false and rest of the integers as true. So, whenever number become 0, while loop will break.
Q.2 Could someone explain to me what seen[digit]++ does. And when its true or false?
=> seen is declared as an array of size 10 and initialized all entries as 0. So initially every entry of array seen is zero i.e. seen[0] = 0, seen[1] = 0, seen[2] = 1... seen[9] = 0. Now when we find digit and perform seen[digit]++ it will increase value by 1 every time.
Ok so:
Every number not equal to 0 is true and equal to 0 is false. For example 1 2 and 3 are true, but 0 is false. So while (number) will iterate as long as number != 0
seen[digit]++ first returns the value, then increments itself by one after returning the value.
The condition if(number) is same as if(number != 0).
Point 2: After we have processed the last digit in the number, the value of number/10 will be 0 (as the last digit belongs to 0-9) and there we end our loop.
Point 5: The increment number will increment the value in the array and return the old value. If the value is incremented to 2, then it means that the digit is not unique and increment operation returns us 1 and the if condition is satisfied.
In C++ 0 evaluates to false and any other number evaluates to true. That "random number" is actually modified inside the loop with number /= 10. Division of integer numbers in C++ is special in the sense that it does not yield fractions so 51/10 = 5 and 5/10 = 0. At some point number equals 0 and the loop ends.
seen[digit]++ is a commonly used trick. You lookup the table seen at position digit return the current value and increment the value by 1. So if you would modify your example code like this:
#include <iostream>
using namespace std;
int main(){
int seen[10] = {0};
cout << seen[7]++ << endl;
cout << seen[7] << endl;
}
Your console output should be:
0
1
There is also ++seen[digit] which would first increment and then return the value so you would get:
1
1

C++ random bitwise behavior

INTENTION -> A program that adds even numbers only within a range
Strange behavior -> the logical statement is incorrect for adding even numbers, instead the logical statement is correct for adding odd numbers, but the sum of even numbers is the result. As we all know, num & 1 returns true if num is odd, and false if num is even.
Question -> Check my logical statement in the code. Why is the increment of n inline with the logical operator inverting the return value and summing even numbers (which is intended, but unexpected for this logical statement)?
//program to print sum of even numbers
#include <iostream>
int main(){
int n = {0}, result = {0};
while(n < 99) result += n++ & 1 ? n : 0;
std::cout << result << '\n';
}
I was experiencing some random behavior when attempting a simple coding implementation of a bitwise odd number detection. num & 1
This is returning the desired and appropriate value of even nums between 0 and 100, when I'm not using any negation. such as ~num & 1 . Can someone explain to me why the negation isn't necessary, and why its returning even values? Is there some extra behavior to the n++ happening?
NOTE: I understand the syntax is obtuse, that was sort of my intention, to be as obtuse as possible for the sake of experimenting with the language a bit. I'm not asking for a style critique.
Examples
https://repl.it/KG8Z/1 < this should print odd, but what is the unintended side-effect of the ++ operator that is making it print even numbers?
https://repl.it/KG8Z/0 < c++
https://repl.it/KGId/3 < python
I was expecting to need to use the bitwise not operator in c++ to get the desired result, but the results are the same despite the absence of a logical not. How to explain this odd behavior?
When
n++ & 1
executes, lets expect n is odd number at the moment, condition is true.
Post incrementation is applied and n is even when
result += n
executes. Thats why its counting even numbers instead of odd. Modify code to
while(n < 99) result += !(n++ & 1) ? n : 0;
and it will count odd one's.
[expr.cond]/1:
Conditional expressions group right-to-left. The first expression is
contextually converted to bool. It is evaluated and if it is true, the
result of the conditional expression is the value of the second
expression, otherwise that of the third expression. Only one of the
second and third expressions is evaluated. Every value computation and
side effect associated with the first expression is sequenced before
every value computation and side effect associated with the second or
third expression.
Thus, n is "selected" on odd ns in the condition, but at that point, it's already incremented (even).
n & ~1 is not the same as !(n & 1).

Having trouble understanding a portion of code (bit operation)

I can't understand how to count number of 1's in binary representation.
I have my code, and I hope someone can explain it for me.
Code:
int count (int x)
{
int nr=0;
while(x != 0)
{
nr+=x%2;
x/=2;
}
return nr;
}
Why while ? For example if i have 1011, it wouldn't stop at 0?
Why nr += x%2 ?
Why x/=2 ?!
First:
nr += x % 2;
Imagine x in binary:
...1001101
The Modulo operator returns the remainder from a / b.
Now the last bit of x is either a 0, in which case 2 will always go into x with 0 remainder, or a 1, in which case it returns a 1.
As you can see x % 2 will return (if the last bit is a one) a one, thus incrementing nr by one, or not, in which case nr is unchanged.
x /= 2;
This divides x by two, and because it is a integer, drops the remainder. What this means is is the binary was
....10
It will find out how many times 2 would go into it, in this case 1. It effectively drops the last digit of the binary number because in base 2 (binary) the number of times 2 goes into a number is just the same as 'shifting' everything down a space (This is a poor explanation, please ask if you need elaboration). This effectively 'iterates' through the binary number, allowing the line about to check the next bit.
This will iterate until the binary is just 1 and then half that, drop the remainder and x will equal 0,
while (x != 0)
in which case exit the loop, you have checked every bit.
Also:
'count`is possibly not the most descriptive name for a function, consider naming it something more descriptive of its purpose.
nr will always be a integer greater or equal to zero, so you should probably have the return type unsigned int
int count (int x)
{
int nr=0;
while(x != 0)
{
nr+=x%2;
x/=2;
}
return nr;
}
This program basically gives the numbers of set bits in a given integer.
For instance, lets start with the example integer 11 ( binary representation - 1011).
First flow will enter the while loop and check for the number, if it is equal to zero.
while(11 != 0)
Since 11 is not equal to zero it enter the while loop and nr is assigned the value 1 (11%2 = 1).nr += 11%2;
Then it executes the second line inside the loop (x = x/2). This line of code assigns the value 5 (11/2 = 5 ) to x.
Once done with the body of the while loop, it then again checks if x ie 5 is equal to zero.
while( 5 != 0).
Since it is not the case,the flow goes inside the while loop for the second time and nr is assigned the value 2 ( 1+ 5%2).
After that the value of x is divided by 2 (x/2, 5/2 = 2 )and it assigns 2 to x.
Similarly in the next loop, while (2 != 0 ), nr adds (2 + 2%2), since 2%2 is 0, value of nr remains 2 and value of x is decreased to 1 (2/2) in the next line.
1 is not eqaul to 0 so it enters the while loop for the third time.
In the third execution of the while loop nr value is increased to 3 (2 + 1%2).
After that value of x is reduced to 0 ( x = 1/2 which is 0).
Since it fails the check (while x != 0), the flow comes out of the loop.
At the end the value of nr (Which is the number of bits set in a given integer) is returned to the calling function.
Best way to understand the flow of a program is executing the program through a debugger. I strongly suggest you to execute the program once through a debugger.It will help you to understand the flow completely.

Lua if A != (X or Y or Z)?

So I have this function that takes in an integer. But It doesn't work and I suspect that the if statement is not valid, I could not find anything on google regarding the issue, maybe my googling skills just suck.
if mynumber != (0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8) then
print("Please choose an integer number between 1-8")
end
Thanks for any help!!
Correct. That is not how you test things like that. You cannot test multiple values that way.
or requires expressions on either side and evaluates to a single expression. So (0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8) evaluates to 0 and your final expression is just if mynumber != 0 then.
To test multiple values like that you need to use or around multiple comparison expressions.
if (mynumber ~= 0) or (mynumber ~= 1) or (mynumber ~= 2) ... then (also notice ~= is the not-equal operator not !=).
Also be sure to note YuHao's answer about the logic in this line and how to test for this correctly.
Others have pointed the major problems you have, i.e, 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 evaluates as 0, the rest is ignored because of short-circuit. You need to test the number with these numbers one by one.
However, there's one last trap. The condition
if mynumber ~= 0 or mynumber ~= 1 then
is always true, because a number is either not equal to 0, in which case mynumber ~= 0 is true; or it is equal to 0, in which case mynumber ~= 1 is true.
The correct logic should be:
if mynumber ~= 0 and mynumber ~= 1 then
Etan's answer explains the behaviour as observed in lua. I'd suggest writing a custom FindIn function for searching:
function FindIn( tInput, Value )
for _ in pairs( tInput ) do
if Value == tInput[_] then return true end
end
return false
end
if FindIn( {1,2,3,4,5,6,7,8}, mynumber ) then
-- ...
end
try this:
In Lua You check if two items are NOT EQUAL by "~=" instead of "!=",
If You compare two items in if statement, then always remember that items should return booleans, so: instead of mynumber != (0 or 1 or...) try something like (mynumber ~= 0) or (mynumber ~= 1) ...
You can do it simple with .... (mynumber have to be integer variable)
if mynumber<0 or mynumber>8 then
print("Please choose an integer number between 1-8")
end

Is it prime? TI-BASIC

Hi Im trying to translate this code to TI-BASIC. Im having problems with how to change for loop into while loop and also with incrementing a number in TI-BASIC.
#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
You can efficiently use a While loop in this situation:
Input "NUMBER: ",A
1->B
3->I
√(A->D
If not(fPart(A/2
DelVar BWhile I<=D and B
fPart(A/I->B
I+2->I
End
If not(B
Disp "NOT
Disp "PRIME
In TI-Basic a While loop works as you would expect and you can have conditions for it.
Incrementing a number is as simple as saying
X+i->X
Where 'i' is the incrementer.
To change a For loop into a While loop, you'll have to set up the While loop to constantly check to see if the number and increment have passed the upper bound while increasing the increment each run through.
If you wanted to mimic i++ or ++i in TI-Basic (Using a While loop), all you would have to change would be the arrangement of the code. Please note that TI-Basic For statements always operates under ++i.
Example (i++):
0->X
While X<10
Disp X
X+1->X
End
This will display (With each number on a new line)
0 1 2 3 4 5 6 7 8 9
Example (++i):
0->X
While X<10
X+1->X
Disp X
End
This will display (With each number on a new line)
1 2 3 4 5 6 7 8 9 10
Let it be noted that TI-Basic For statements are much much faster than While loops when it comes to incrementing and should almost always be considered superior for the task.
Integrating Timtech's idea to skip even numbers effectively halves the required time to check the primality of the number with the addition of only a few extra lines.
I expanded the idea to skip multiples of two and multiples of three.
Input "Number:",X:abs(X->X
0
If not(fPart(X/2)) or not(fPart(X/3:Return
For(B,5,sqrt(X),6)
If not(fPart(X/B)) or not(fPart((X+2)/B:Return
End
1
Test Number: 1003001
Time Required: ~4 Seconds (So much better than 15 :D)
Size: 65 Bytes
I dont see why you would want to use a while loop as ti-basic has for loops:
0->F
Input "ENTER NUMBER:",N
For(I,2,int(N/2
If N/I=int(N/I
Then
int(N/2->I
1->F
End
End
If F
Then
Disp "NUMBER IS PRIME
Else
Disp "NUMBER IS NOT PRIME
End
N/I=int(N/I is a way to check for a number's remainder (another way of saying N%I==0 but ti basic does not have modulus). Another trick here is setting I to its maximum bound (int(N/2) as a sort of "break" like other languages would have