tags:

views:

23

answers:

2

Example: I have a scale between 1 and 7. When I get a value like 8, I want it to be wrapped on that scale so it's converted to 1. More examples:

  • 1 results in 1
  • 5 results in 5
  • 7 results in 7
  • 8 results in 1
  • 9 results in 2
  • 10 results in 3
  • 11 results in 4
  • 12 results in 5
  • 13 results in 6
  • 14 results in 7
  • 15 results in 1
  • 16 results in 2

and so on.

Is there a method or useful C-function to do that? Something tells me I just need a modulo. It's 42°C in my room. My brain is like soap.

+2  A: 

Try ((number - 1) % 7) + 1.

Will A
+4  A: 

int b = ((a-1) % 7) + 1;

Check using Excel, of all things!

Yes it's HOT today.. arrgh!

Kieren Johnstone
Too darned hot, although % 7 it's only 5°C.
Will A