Thread: Median of 3

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    Median of 3

    I'm a newbie in C language so I'm still trying to learn the basic concepts. I have a question about a program regarding the median value of 3 numbers. First I wrote the following piece of code:
    Code:
    #include <stdio.h>
    double min(double x, double y){
        return x<y ? x : y;
    }
    double med_aux(double x, double y, double z){
        return z<x ? x : min(z,y);
    }
    double med(double x, double y, double z){
        return x<y ? med_aux(x, y, z) : med_aux(y, x, z);
    }
    int main(){
        double n1, n2, n3;
        n1=5;
        n2=22;
        n3=1;
        printf("The median of the values is %lf");
        printf("%lf", med(n1, n2, n3));
        return 0;
    }
    And it seemed fine to me, but I was getting "The median of the values is 0.0000005.000000" as output, so I changed the printf a bit:
    Code:
    #include <stdio.h>
    double min(double x, double y){
        return x<y ? x : y;
    }
    double med_aux(double x, double y, double z){
        return z<x ? x : min(z,y);
    }
    double med(double x, double y, double z){
        return x<y ? med_aux(x, y, z) : med_aux(y, x, z);
    }
    int main(){
        double n1, n2, n3;
        n1=5;
        n2=22;
        n3=1;
        printf("The median of the values is %lf", med(n1, n2, n3));
        return 0;
    }
    And I got the desired output, which is "The median of the values is 5.000000". But still, I would like to know why it didn't work the first time, since it seemed correct. Also, if you could better explain the %d(decimal integer) and %f(floating point), when to use which, I would greatly appreciate it. I know these questions may seem trivial but I really want to understand properly. Thank you!!

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    You've been thrown into an exercise in writing functions before you understand printf. You need to be able to use printf() to get the results from your other exercises.

    printf() is a function in C. However it isn't an ordinary function, it's a variable arguments function. As a beginner, it's probably best to think of it as an output statement rather than a function.

    It takes a format string as its first argument. This tells it the types of subsequent arguments and, when you get more advanced, things like the number of decimal places to output. For now, you don't need to worry about that. What you need to know is that '%' is the escape character. "%" means 'treat the next character as a placeholder for an argument rather than a character to output. What you need to know at first is that %c means "output a character", "%d" means "output an integer" and "%f" means "output a floating point value", actually a double (if you pass a float it gets promoted to a double". %s means "output a string" and matches a char *.

    With these simple formatting commands, you can get data out of the program and onto the screen. You need to experiment a bit just printing out a few values.

    You must supply an argument to match the formatting character. If you don't do this, printf misinterprets the argument you supply ot grabs garbage if you don't supply anything, and you get strange results.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    If you turn the compiler warnings on, the errors will be pointed out at compiler time.

    For 'gcc' I use "-Wall -pedantic" as my default compile options - enable all warnings, and be pedantic (complain about even the smallest, trivial things).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hamster_nz View Post
    For 'gcc' I use "-Wall -pedantic" as my default compile options - enable all warnings, and be pedantic (complain about even the smallest, trivial things).
    High five, fellow pedant!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    114
    Quote Originally Posted by laserlight View Post
    High five, fellow pedant!
    Sorry to hijack this thread, but is `-pedantic` just useful if you're worried about porting? Are there any benefits if the code it staying put?

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by Yonut View Post
    Sorry to hijack this thread, but is `-pedantic` just useful if you're worried about porting? Are there any benefits if the code it staying put?
    It's mainly for porting. It means "reject anything that isn't strictly ANSI-compliant". However in most beginners' exercises, you won't be doing anything that is platform-specific. So -pedantic can act as a warning that you are going wrong.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating the Median
    By openwindow in forum C Programming
    Replies: 1
    Last Post: 11-20-2011, 07:15 AM
  2. Median Help!
    By tidbit in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2010, 09:55 PM
  3. Median
    By Neo1 in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2007, 04:00 PM
  4. Median
    By Suchy in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2006, 02:07 AM
  5. median
    By frank in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2001, 04:32 PM

Tags for this Thread