how to make this 10 lines more elegant?

This is a discussion on how to make this 10 lines more elegant? within the C Programming forums, part of the General Programming Boards category; hi, this code help me to find some "missing value" in message sent from a slider in my user interface. ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    how to make this 10 lines more elegant?

    hi, this code help me to find some "missing value" in message sent from a slider in my user interface. (iphone app)
    This code works fine, but i this isn't very elegance...someone has some advices?

    thanks
    Code:
    /* i receive "actualValue" from slider on my interface
     * this interface doesn't send every value, but the values sents depends on users
     *  speed, but i want to receive update on every value that is multiple of STEP.
     *  so, when i receive an update, i "miss" some value.
     */
     //if acutualValue is multiple of step
    if (! (actualValue % step)) {
    			//and difference between previsukyevalue > step
    			if ( abs(actualValue - previusKeyValue) > step) {
    				int i;
    				if (actualValue > previusKeyValue) {
    					/* i'm looking for values between previuskeyvalye + 1 and actualvalue that are multiple of step.
    					 * i'm going from low value to higth
    					 */
    					for (i = previusKeyValue + 1; i < actualValue ; i++) {
    						if (!(i % step)) {
    							print("you skipped %d",i);
    						}
    					}
    				}
    				else {
    					/* the same as previus code, but whene i go from higt value to low value.
    					for (i = previusKeyValue - 1; i > actualValue ; i--) {
    						if (!(i % step)) {
    							print("you skipped %d",i);
    						}
    					}
    					
    				}
    			}
    			print("you found %d",actualValue);
    			previusKeyValue = actualValue;
    		}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, is this a slider, or a guessing game, or what? This will miss some multiples, if that's what you're asking (i.e. if step is 5 and you go from 78 to 82, this code won't ever notice that you missed 80). I think what you want is this:
    Code:
    int orig = previusKeyValue/step; //I can't believe you're making me misspell previous
    int now = actualValue/step;
    if (orig != now) {
        int low = orig < now ? orig : now; //this is cheap but so what
        int high = orig < now ? now : orig;
        for (int i = low+1; i <= high; i++) {  //this is unusual but matches the situation
            printf("You skipped %d", i*step);
        }
    }
    I'm skipping the bit where actualValue % step == 0 because you are too.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( prev != actual )
    {
        printf( "You missed %d steps.\n", (prev - actual) / step );    
    }
    You mean like that?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I make this work with lines of any length
    By play_fetch in forum C Programming
    Replies: 12
    Last Post: 03-11-2011, 06:06 PM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 09:55 AM
  3. Replies: 7
    Last Post: 07-31-2005, 12:28 AM
  4. How do I make new lines? by MEGA N00b
    By Moron Slayer in forum C++ Programming
    Replies: 2
    Last Post: 12-18-2003, 05:58 PM
  5. The Elegant Universe
    By DISGUISED in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-21-2002, 01:54 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21