Thread: how to improve

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    how to improve

    anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?

    Code:
    if (i=0)
    {
                    mpg=miles/gallons;
    }
    else 
    {
    	mpg += (miles/gallons)/2;
    }
    i++;

  2. #2
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    I think you want if (i==0) instead of if(i=0)

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559

    Re: how to improve

    Originally posted by blight2c
    anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?

    Code:
    if (i=0)
    {
                    mpg=miles/gallons;
    }
    else 
    {
    	mpg += (miles/gallons)/2;
    }
    i++;
    Code:
       mpg += (miles/gallons)/2;
       if (i == 0)
          mpg = miles/gallons;
       i++
    or
    Code:
       i == 0 ? mpg = miles/gallons : mpg += (miles/gallons)/2;
       i++;
    Last edited by salvelinus; 04-02-2002 at 09:54 AM.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559

    Re: how to improve

    Originally posted by blight2c
    anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?

    Code:
    if (i=0)
    {
                    mpg=miles/gallons;
    }
    Also, in the above statement, mpg will never be cumulative; anytime the assignment in this if statement executes, mpg will be set to the calculated value; the calculated value won't be added to mpg.
    The else part, however you code it, will be cumulative.

  5. #5
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    Also, in the above statement, mpg will never be cumulative; anytime the assignment in this if statement executes, mpg will be set to the calculated value; the calculated value won't be added to mpg.
    right, i should have been more clear. the if is only for the first time around when there have been no previos mpg's to calc. the second time around (all this is in a while loop, btw) and thereafter it should always flow into the else. i just found it cumbersome and inefficent. i was wondering if there was a more clever algorithm. thanks for the help

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    In that case, do the first calculation before you enter the loop. Subsequent calculations can be done in the loop. You shouldn't need to check i then, won't need an if.
    Based on the code you posted, maybe other stuff in your program would still require it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What do I need to improve on for the real world?
    By vhunterd in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-08-2007, 07:49 PM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. Replies: 6
    Last Post: 06-09-2006, 12:44 AM
  4. can someone double check and maybe improve my code
    By tommy69 in forum C Programming
    Replies: 23
    Last Post: 04-21-2004, 02:04 PM