Results 1 to 4 of 4

Thread: Algorithm problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    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?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    jim mcnamara
    Guest
    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]++; 
       }
    }

  4. #4
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    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...
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width