Thread: int ...float ...

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    16

    int ...float ...

    hi, i am very confused about when and were to put float and int.
    i know that float is for numbers with decimals (3.14...etc) but when i am writing code i get always confused.

    This programm just does some easy calculations, when i give it x,y,z values.
    it works with int's but i cant figure out what to change so that it also computes numbers like 1.5 etc..


    Code:
    int
    sin1(int x)
    {
    int a;
    a=pow(x,3);
    return a;
    }
    
    
    int
    sin2(int x,int y,int z)
    {
    int b;
    b=(x*y*z);
    return b;
    }
    
    
    sin1();
    sin2();
    
    
    int main()
    {
    int x,y,z,c;
    scanf("%d%d%d",&x,&y,&z);
    c=(sin1(x)+sin2(x,y,z));
    printf("f(x)=(x^3)+(x*y*z)= %d",c);
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Be sure you apply indentation to your code:

    Code:
    int sin1(int x)
    {
        int a;
    
        a=pow(x,3);
    
        return a;
    }
    
    int sin2(int x,int y,int z)
    {
        int b;
    
        b=(x*y*z);
    
        return b;
    }
    
    sin1();
    sin2();
    
    int main()
    {
        int x,y,z,c;
    
        scanf("%d%d%d",&x,&y,&z);
        c=(sin1(x)+sin2(x,y,z));
        printf("f(x)=(x^3)+(x*y*z)= %d",c);
    }
    • Be sure to include headers
    • There are two function calls (lines 19 and 20) outside of any function
    • You should have prompts in your program so the user knows what to enter


    Quote Originally Posted by Nantia Atzo View Post
    i know that float is for numbers with decimals (3.14...etc) but when i am writing code i get always confused.
    Use ints when you are working with whole numbers.
    Use floats/doubles when you are working with real numbers.

    For instance, in the code you provided, is there any reason for ints to be used anywhere?

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    16
    Matticus,
    the two function calls are there because my teacher in university sais we have to do so , outside the main...i am not really sure about the reason but i am searching for it.
    for my problem here, i changed every int with float , and every %d to %f and now it works
    thank you for your help.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the two function calls are there because my teacher in university sais we have to do so
    Your teacher is using an old book then.

    sin1();
    This is an old-style (pre ANSI/ISO C, more commonly known as K&R C)
    It neither tells you the return type (which is assumed to be int), nor does it say how many (if any) parameters it takes, or what types those parameters are.

    int sin1(int x);
    This is a new-style function prototype.
    You can tell because it always has a return type, and always lists the parameters (and their types).

    The existence of a prototype after it's definition is valid, if somewhat moot point.
    Function prototypes really come into their own when you put the prototypes into header files, and functions into different source files.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How i can convert int->float float->int safely?
    By Vanhapolle in forum C Programming
    Replies: 2
    Last Post: 11-04-2014, 08:22 PM
  2. Replies: 1
    Last Post: 02-25-2013, 08:08 AM
  3. (float) or (float*) wrt sizeOf()
    By jakemott in forum C Programming
    Replies: 2
    Last Post: 07-19-2009, 08:13 PM
  4. Replies: 8
    Last Post: 07-08-2005, 09:12 AM
  5. Unresolved external 'convert(float, float)'
    By Ipsec Espah in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2003, 10:08 AM

Tags for this Thread