Thread: Cast double to int correctly(5 lines of code)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Cast double to int correctly(5 lines of code)

    Code:
    #define CENTS_PER_DOLLAR 100 
    
    int avg_min_cost;
     avg_min_cost=(wkday_min+night_min+wkend_min))/(CENTS_PER_DOLLAR);
    
      
    printf("Average minute cost: $ %.2lf\n",(double)avg_min_cost);
    I'm not sure in the above code whether or not i casted the int to doubles correctly.

    Here are the specifications:
    "Note that since you are dividing by 100, and not 100.0
    You will have to cast your int variable to doubles in your printf statements before dividing by CENTS_PER_DOLLAR"

    ps: when dividing by 100.0 the program works fine. Dividing by the 100, the numbers are off from the goal.
    However, i am required to divide by 100... what lol?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nope.

    Your instructions state to cast your int variable to doubles BEFORE dividing by CENTS_PER_DOLLAR in the printf statement.

    You're not dividing in the right statement, and casting the var AFTER you've done integer division will not give you anything other than a whole number.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by Adak View Post
    Nope.

    Your instructions state to cast your int variable to doubles BEFORE dividing by CENTS_PER_DOLLAR in the printf statement.

    You're not dividing in the right statement, and casting the var AFTER you've done integer division will not give you anything other than a whole number.
    So if i do not cast my int variable into a double, and divide by 100, my result would be an integer, correct?

    But if i go ahead and cast the variable, then when i divide by CENTS_PER_DOLLAR (100), it is equivalent to dividing by 100.0?

    Thanks for clarifying this.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's all about what happens when you perform a binary operation on two different types, e.g addition.
    One of the operands changes type first to make them the same, and the one that changes is pretty much the one that represents the least values. Look up the type promotion rules.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. helpp me , why my code dont work correctly
    By technoaymen in forum C Programming
    Replies: 4
    Last Post: 02-16-2012, 04:02 PM
  2. Clarification: Type cast double to int
    By strokebow in forum C Programming
    Replies: 1
    Last Post: 05-10-2011, 07:59 AM
  3. how to i correct this code to read the file correctly?
    By funit49 in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2008, 07:56 PM
  4. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  5. Am I understanding this C code correctly ???
    By AntonsonDJ in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 07:19 PM