Thread: C Newby - help with type double

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Unhappy C Newby - help with type double

    Hello -

    I am very new to C programming and am tying to complete a homework assignment, but I can't get the values to return correctly and have no idea what to change.

    I thought that I should replace the %d with %f, but that still doesn't return the correct results.

    Any guidance is appreciated!

    This is the exercise:
    Devise a function called min(x,y) that returns the smaller of two double values. Test the function with a simple driver.

    This is the code I have:
    Code:
    #include <stdio.h>
    
    double imin(double, double);
    
    
    double main(void)        
    
    {
    
          double num1, num2;                                                            
     
            printf("Enter a pair of numbers (q to quit):\n");
            while (scanf("%d %d", &num1, &num2)==2);
        {
            printf("The smallest number of %f and %f is %f. \n",
                    num1, num2, imin(num1, num2));
    
        }
    
            printf("you're done!\n");
            return 0;
    
        }
        
    
            double imin(double x, double y)
            {
                return (x < y) ? x : y;
            }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to change scanf too! scanf requires %lf to read a number into a double variable.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    OMG! thank you so much. *sigh* I have been trying to figure this out for about 2 hours.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just a thing here.
    Main returns int, not double.
    You need to make another function that you can recursively call if anything, because main must return int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elysia View Post
    You need to make another function that you can recursively call if anything, because main must return int.
    What?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wait, indentation screwed up the code.
    There is another function, but main should still return int and not double. You don't need to return the return value from imin.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM