Thread: Float Values

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    Float Values

    Hey guys i know this is kind of a newbie question but Ive finished all my code. I just want to know how I can change it so that it only deals with float values. I mean so that my program will return the smaller of the two float values without conversion. any help would be great.

    Code:
    #include <stdio.h>
    
    int my_min(int, int);
    
    
    
    int main(void)
    
    {
    
        int number1, number2;
    
    
    
        printf("Enter a pair of integers (q to quit):\n");
    
        while (scanf("%d %d", &number1, &number2) == 2)
    
        {
    
            printf("The lesser of %d and %d is %d.\n",
    
                number1, number2, my_min(number1,number2));
    
            printf("Enter a pair of integers (q to quit):\n");
    
        }
    
        printf("Bye.\n");
    
    
    
        return 0;
    
    }
    
    
    
    int my_min(int x,int y)
    
    {
    
        int min;
    
    
    
        if (x < y)
    
            min = x;
    
        else
    
            min = y;
    
    
    
        return min;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    this was my teachers question too in case this can help.

    Devise a function my_min(x,y) that returns the smaller of two float values, and test the function with a driver. The driver should prompt the user for input and then display the value returned by the my_min() function.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What have you tried?

    Jim

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    well i tried declaring where i had the integers at the top as floats but then that just screwed up like my whole numbering system lol. So now im pretty much stuck..

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    How about declaring some of those variables as float instead of integer?

    The modulo for float is going to be %f. For example, %3.0f would reserve three spaces on one side of the decimal and zero spaces on the other with the letter f designating the reporting of a float.

    By making those kinds of changes I was able to get your program to compile and run with simple values.

    If you change the top, but don't change that function, then you will see an error in the logic of the program like, " The lesser of 2.000000 and 3.000000 is 0.000000." If you got the function and the main to work in float, you might see a reply like, "The lesser of 2.000000 and 3.000000 is 2.000000."

    You can do it.
    Last edited by agxphoto; 12-08-2010 at 11:48 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    so like:

    Code:
    #include <stdio.h>
    
    int my_min(int, int);
    
    
    
    int main(void)
    
    {
    
        flat number1, number2;
    
    
    
        printf("Enter a pair of integers (q to quit):\n");
    
        while (scanf("%0.0f %0.0f", &number1, &number2) == 2)
    
        {
    
            printf("The lesser of %0.0f and %0.0f is %0.0f.\n",
    
                number1, number2, my_min(number1,number2));
    
            printf("Enter a pair of integers (q to quit):\n");
    
        }
    
        printf("Bye.\n");
    
    
    
        return 0;
    
    }
    
    
    
    int my_min(int x,int y)
    
    {
    
        int min;
    
    
    
        if (x < y)
    
            min = x;
    
        else
    
            min = y;
    
    
    
        return min;
    
    }

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    That would get you the reporting of the numbers as float; but that didn't make the variables get declared as floats, did it?

    You've got a typo of flat in there. Float.

    That first time you declared a function: do you see yourself declaring that as float? When you're putting numbers into that function, are they going in there as float?

    Why do you have "int" and the word "integer" all over your program when what you want is a float in a lot of those parts of the program?
    Last edited by agxphoto; 12-09-2010 at 12:10 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    And all the other 'int' in your min function as well.

    %f should suffice.
    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.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    thanks alot guys finally got it working.......I know my newbie antics can ........ people off sometimes... but thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  3. Unable to calculate float values
    By CHurst in forum C Programming
    Replies: 2
    Last Post: 12-08-2005, 06:14 PM
  4. Why does it work in Visual Studio and Not Borland
    By MonteMan in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2002, 09:36 PM
  5. Need more eyes to find problem??
    By sailci in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 10:03 PM