Click here to Skip to main content
15,178,370 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Codeproject,

Assuming I have a row that follows like so:

0 1 2
3 4 5
6 7 8

And the number starts at 4, but I can only add or remove one, so saying;

-1 and -1 would equal to 0.
-1 and 0 would equal to 3.

Any ideas?
[no name] 8-Jul-13 15:59pm
Smells of homework. What have you tried?
Deviant Sapphire 8-Jul-13 16:00pm
Not really homework, this is for a project of mine but I do not have an idea. I thought about multiplying the number by three, but that didn't work out well.

1 solution

I think I know what you're trying to say here, so here's an attempt at solving this (if I'm way off let me know, your description is a little vague):

Well you could use a 2-dimensional array, representing your grid, then use your -1/0/+1 as a change in the indexes, but that's more than you really need.

Look at it this way, what changes do those numbers really result in? The first moves you left or right, no matter where you are, the results in adding the same to the number (e.g. you start at 4, the first number is +1, you move to 5, which is 4 + 1). So just add the first number to 4. The second number moves you up or down. These all result in a change of 3*[second number] (e.g. you;re at 3, the second number is -1, you add -1*3 to 3 and get: 3 + (-1 * 3) = 0).

So the simplest way is:

4 + [first number] + [second number] * 3
Deviant Sapphire 8-Jul-13 16:21pm
Thank you I really appreciate the help and this did solve it!
Tim Carmichael 8-Jul-13 16:44pm
Looking at the set of numbers and the offered solution, please keep in mind what the desired behaviour is if the starting point is already on a edge (ie: not 4).
If you start with 0, what do you want to happen if the first number is -1? Do you wrap to 2 or ignore the value?
Deviant Sapphire 9-Jul-13 21:04pm
No this is exactly what I meant, I just followed your logic and it's easier for me to figure out next problems. Thanks a lot! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900