-
Algorithm problem
I have been fighting to make a clean algorithm see if you can put me on a better path....
A point moves horizontally across the screen starting at an x value of 0 and ending at a x value of 800. As it moves across the screen it crosses say 8 different regions.
So region
1 goes from 0-100
2 goes from 100-200
3 goes from 200-300
4 goes from 300-400
5 goes from 400-500
6 goes from 500-600
7 goes from 600-700
8 goes from 700-800
ok? ok now there is an integer array representing each of these regions int state [8] that is intilized to 0. As the point moves from region 1-2 the first element in the array becomes 1, as it exit 2 and goes to 3 the second element in the array becomes 1 and the first element becomes 2. As it exits 3 and goes to 4, the third element becomes a 1, the second element becomes a 2 and the first becomes a 3. You get the picture?
so it will look like this...
Section 1 2 3 4 5 6 7 8
Value 1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
I have taken a crack at this but I keep coming up with a big mess of variables and for loops, anyone wanna take a psedo code/ c code crack at this?
-
well what do you need this for?
you could put the values in a stack as you pass each 100'th x (when state!=state=x/100)
-
try:
Code:
int state[9];
int i,j;
for(i=0;i<9;i+)state[i]=0;
if (x>0){
if(x%100 == 0){
j=x/100;
for(i=j;i>0;i--) state[i]++;
}
}
-
jim, you dont have to say i = 0 to 9, 0 to 8 will get the job done. 0 to 9 is NINE numbers. But your code is perfect.
also, the for loop that increments the states needs to be:
Code:
for(i=j;i>-1;i--) state[i]++;
because then it will go from j to 0 and not from j to 1.
Have fun coding...