Thread: Using the (int) cast

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    Using the (int) cast

    Hello,

    I solved some exercises on the textbook called A First Book of ANSI C by myself and really happy about that. I have a major problem with one exercise. It says i have to use the (int) cast, devise a method to round the values in the variables a and b to the nearest hundredth (penny value) before they are added.

    I have no idea what the question means!!! Any advice? I read a different book i brought from Korea to solve it in my own way but I dont know its right or not.

    I tried to find solutions for the textbook but cant find it.
    Question
    Code:
        double a, b, c;
        a = 1.674;
        b = 1.322;
        printf("\n%4.2f",a);
        printf("\n%4.2f",b);
        printf("\n----");
        c = a + b;
        printf("\n%4.2f\n",c);
    My code:
    Code:
    #include<stdio.h>
    #include <math.h>
    int main()
    {
        double a, b, c;
        a = 1.674;
        b = 1.322;
        double  round (double x);
        a = round(a*100)/100;
        b = round(b*100)/100;
        printf("\n%4.2f",a);
        printf("\n%4.2f",b);
        printf("\n----");
        c = a + b;
        printf("\n%4.2f\n",c);
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Say you had
    double a = 12.3456;

    a = a * 100;
    becomes 1234.56

    a = (int)a;
    becomes 1234.00
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    I got it
    Thank you very much!!

    Code:
    #include<stdio.h>
    main()
    {
        double a, b, c;
        a = 1.674;
        b = 1.322;
        
        printf("\n%4.2f",a);
        printf("\n%4.2f",b);
        
        a = a * 100;
        b = b * 100;
        printf("\n----");
        
        //round the values in the variables a and b to the nearest hundredth
        a = (int) a;
        b = (int) b;
        
        //add the variables a and b
        c = a + b;
        c = c / 100;
        
        //provide the required result
        printf("\n%4.2f\n",c);
        
        return 0;
    
    }
    Quote Originally Posted by Salem View Post
    Say you had
    double a = 12.3456;

    a = a * 100;
    becomes 1234.56

    a = (int)a;
    becomes 1234.00

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM