Please explain why the first snippet of code does not result in -1.
string s = "abc";
int amount = -1;
cout << "amount to shift before modulus: " << amount<<endl;
amount %= s.size();
cout << "mod " <<s.size()<<endl;
cout << "amount to shift after modulus: " << amount<<endl;
Output:
amount to shift before modulus: -1
mod 3
amount to shift after modulus: 0
string s = "abc";
int sSize = s.size();
int amount = -1;
cout << "amount to shift before modulus: " << amount<<endl;
amount %= sSize;
cout << "mod " <<sSize<<endl;
cout << "amount to shift after modulus: " << amount<<endl;
Output:
amount to shift before modulus: -1
mod 3
amount to shift after modulus: -1
I was completing a coding challenge when I ran across this behavior and I don't understand what is going on. This behavior does not occur if the amount is a positive number such as 1.