Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #153916

Re: max of 3

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: max of 3
Date 2020-08-22 16:41 -0700
Organization None to speak of
Message-ID <87o8n25jui.fsf@nosuchdomain.example.com> (permalink)
References <da872028-3be4-4d30-8617-3a3c74c9e97fn@googlegroups.com> <87d03j57lg.fsf@bsb.me.uk> <Zn60H.1387423$rLg.1259802@fx49.ams4> <4e0bd5c4-6cf2-4ed2-993b-9df9801296ean@googlegroups.com> <875z9a701d.fsf@nosuchdomain.example.com>

Show all headers | View raw


Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> axel porin <porinaxel@gmail.com> writes:
>> Le samedi 22 août 2020 à 12:25:07 UTC+2, Bart a écrit :
>>> On 22/08/2020 10:54, Ben Bacarisse wrote:
>>> > axel porin <pori...@gmail.com> writes: 
>>> 
>>> >> int main(){ 
>>> >> int a=2,b=-4,c=6; 
>>> >> printf("%d ",max3(a,b,c)); 
>>> > 
>>> > I'm curious. Why not write 
>>> > 
>>> > printf("%d\n", max3(2, -4, 6)); 
>>> > 
>>> > ?
>>> I'm surprised at this advice. Presumably the intention is to eventually 
>>> have a, b and c populated by other means (eg. from the user), but values 
>>> are currently hardcoded. 
>>> 
>>> And maybe the values will be printed out first, or as part of the output: 
>>> 
>>> printf("Max3(%d, %d, %d) = %d\n", a,b,c, max3(a,b,c)); 
>>> 
>>> This all becomes harder with your suggestion.
>> what should i write to get the warnings
>
> It depends on what compiler and programming environment you're using,
> and you haven't told us that.
>
> Following this thread, you're still posting code that isn't
> indented properly.  If you're on a Unix/Linux-like system, read
> the documentation for the "indent" command; I suggest "indent -kr".
> Other systems are likely to have code formatters (perhaps even your
> text editor can do this).
>
> Here's your original program from the top of this thread:
>
> int max3(int a,int b,int c){
>     if(a<b)
>     if(b<c)
>     return c;
>     else
>     return b;
> }
>
> int main(){
>     int a=2,b=-4,c=6;
>    printf("%d ",max3(a,b,c));
> }
>
> Note that within the body of max3, every line is at the same indentation
> level, even though there are several logical levels.
>
> Here's my second draft of your program, with no semantic changes other
> than adding the required "#include <stdio.h>".  (If your actual program
> doesn't have that and you didn't get a warning for the call to printf,
> you also need to crank up the diagnostics on your compiler.  If you did
> have "#include <stdio.h>", please include it when you post.)
>
> #include <stdio.h>
>
> int max3(int a, int b, int c) {
>     if (a < b)
>         if (b < c)
>             return c;
>         else
>             return b;
> }
>
> int main(void) {
>     int a = 2, b = -4, c = 6;
>     printf("%d ", max3(a, b, c));
> }
>
> The indentation makes it much more obvious that you don't do anything if
> the first (a < b) condition is false.  And knowing that a < b doesn't
> tell you whether b or c is the maximum value.
>
> Some of the changes I've made are a matter of personal taste.  The
> language doesn't require white space in most contexts, but I like to add
> a certain amount to make the code clearer.  Others prefer or or less
> white space.  I like spaces around most operators ("a < b" rather than
> "a<b").  I also always put a space after each comma, and after "if"
> (primarily so that "if (condition)" doesn't look like a function call).
>
> "int main()" is better written as "int main(void)".  This is a minor
> point, and your compiler probably doesn't care.  (I could expound on
> this point at some length, but it's not relevant for now.)
>
> My personal preference is to (almost) *always* use curly braces for
> control statements (if, for, while, etc.).  It makes the code easier to
> read (at least for me), and can prevent errors when you later add more
> statements.  (It's a habit I picked up from Perl, which requires it.)
>
> Here's the program again with curly braces added:
>
> #include <stdio.h>
>
> int max3(int a, int b, int c) {
>     if (a < b) {
>         if (b < c) {
>             return c;
>         }
>         else {
>             return b;
>         }
>     }
> }
>
> int main(void) {
>     int a = 2, b = -4, c = 6;
>     printf("%d ", max3(a, b, c));
> }
>
> Using curly braces *and* correct indentation is a belt-and-suspenders
> approach.  It's not strictly necessary, but it means that an error in
> either the indentation or in the braces is likely to stand out.  It's
> for the benefit of the human reader, not for the compiler.  This version
> is still incorrect.
>
> Note that some programmers prefer to put the opening "{" on a line by
> itself, aligned with the closing "}".  Both styles are valid.
> Personally, I prefer putting the "{" on the end of the line.
>
> Now let's fix the code so it actually works.  I've taken the liberty of
> pretty much rewriting max3() from scratch.
>
> #include <stdio.h>
>
> int max3(int a, int b, int c) {
>     if (a >= b && a >= c) {
>         return a;
>     }
>     else if (b >= c) {
>         return b;
>     }
>     else {
>         return c;
>     }
> }
>
> int main(void) {
>     int a = 2, b = -4, c = 6;
>     printf("%d\n", max3(a, b, c));
> }
>
> Look at the way the logic flows.  Either a, b, or c has the maximum
> value (perhaps more than one of them if there are duplicates).
> We first check whether a has the maximum value.  If it doesn't,
> we check whether b has the maximum value.  If it doesn't, c does.
>
> Note that I've added a newline "\n" to the printf call.  Output should
> (almost) always end with a newline character.
>
> As others have pointed out, you can also build max3 on top of a simpler
> max2 function:
>
> #include <stdio.h>
>
> int max2(int a, int b) {
>     if (a >= b) {
>         return a;
>     }
>     else {
>         return b;
>     }
>     // could also be:
>     // return a >= b ? a : b;
> }
>
> int max3(int a, int b, int c) {
>     return max2(max2(a, b), c);
> }
>
> int main(void) {
>     int a = 2, b = -4, c = 6;
>     printf("%d\n", max3(a, b, c));
> }

Here's another approach:

#include <stdio.h>

int max3(int a, int b, int c) {
    int result = a;
    if (b > result) result = b;
    if (c > result) result = c;
    return result;
}

void try(int a, int b, int c) {
    // Assumes arguments are 1, 2, and 3 in any order
    int result = max3(a, b, c);
    printf("max3(%d, %d, %d) = %d ", a, b, c, result);
    if (result == 3) {
        printf("OK\n");
    }
    else {
        printf("FAIL\n");
    }
}

int main(void) {
    try(1, 2, 3);
    try(1, 3, 2);
    try(2, 1, 3);
    try(2, 3, 1);
    try(3, 1, 2);
    try(3, 2, 1);
}

You'll notice that I didn't use curly braces in the if statements,
something I said I always do.  I sometimes make an exception for
statements that fit cleanly on one line, especially if there are
several of them in a row and I can format them legibly.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

max of 3 axel porin <porinaxel@gmail.com> - 2020-08-22 02:12 -0700
  Re: max of 3 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-22 02:38 -0700
    Re: max of 3 axel porin <porinaxel@gmail.com> - 2020-08-22 03:24 -0700
      Re: max of 3 David Brown <david.brown@hesbynett.no> - 2020-08-22 15:53 +0200
    Re: max of 3 Elijah Stone <elronnd@elronnd.net> - 2020-08-22 19:03 -0700
      Re: max of 3 James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-22 22:13 -0400
      Re: max of 3 David Brown <david.brown@hesbynett.no> - 2020-08-23 13:42 +0200
  Re: max of 3 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-08-22 10:54 +0100
    Re: max of 3 Bart <bc@freeuk.com> - 2020-08-22 11:24 +0100
      Re: max of 3 axel porin <porinaxel@gmail.com> - 2020-08-22 03:29 -0700
        Re: max of 3 axel porin <porinaxel@gmail.com> - 2020-08-22 03:38 -0700
          Re: max of 3 axel porin <porinaxel@gmail.com> - 2020-08-22 04:06 -0700
          Re: max of 3 James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-22 07:49 -0400
        Re: max of 3 David Brown <david.brown@hesbynett.no> - 2020-08-22 15:58 +0200
          Re: max of 3 Poprocks <please@replytogroup.com> - 2020-08-22 15:15 -0400
            Re: max of 3 James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-22 16:49 -0400
            Re: max of 3 Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-08-22 20:56 +0000
            Re: max of 3 David Brown <david.brown@hesbynett.no> - 2020-08-22 22:56 +0200
              Re: max of 3 James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-22 17:04 -0400
                Re: max of 3 David Brown <david.brown@hesbynett.no> - 2020-08-23 13:55 +0200
              Re: max of 3 Poprocks <please@replytogroup.com> - 2020-08-22 17:33 -0400
                Re: max of 3 Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-08-23 05:54 +0000
                Re: max of 3 axel porin <porinaxel@gmail.com> - 2020-08-23 01:59 -0700
                Re: max of 3 Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-08-23 10:22 +0000
                Re: max of 3 Öö Tiib <ootiib@hot.ee> - 2020-08-23 03:27 -0700
                Re: max of 3 David Brown <david.brown@hesbynett.no> - 2020-08-23 14:05 +0200
                Re: max of 3 Bart <bc@freeuk.com> - 2020-08-23 14:09 +0100
                Re: max of 3 David Brown <david.brown@hesbynett.no> - 2020-08-23 15:58 +0200
                Re: max of 3 Bart <bc@freeuk.com> - 2020-08-23 11:33 +0100
                Re: max of 3 Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-08-23 07:04 -0700
                Re: max of 3 John Forkosh <forkosh@panix.com> - 2020-08-24 10:26 +0000
            Re: max of 3 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-22 16:26 -0700
        Re: max of 3 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-22 16:06 -0700
          Re: max of 3 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-22 16:41 -0700
      Re: max of 3 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-08-22 19:42 +0100
  Re: max of 3 Daniel Hyde <Daniel.Hyde71@gmail.com> - 2020-08-22 12:27 +0200
    Re: max of 3 Siri Cruise <chine.bleu@yahoo.com> - 2020-08-22 04:20 -0700
      Re: max of 3 Daniel Hyde <Daniel.Hyde71@gmail.com> - 2020-08-22 17:14 +0200
        Re: max of 3 Siri Cruise <chine.bleu@yahoo.com> - 2020-08-22 13:55 -0700
          Re: max of 3 Daniel Hyde <Daniel.Hyde71@gmail.com> - 2020-08-23 11:18 +0200
    Re: max of 3 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-08-22 20:08 +0100
  Re: max of 3 Bart <bc@freeuk.com> - 2020-08-22 13:01 +0100
    Re: max of 3 gazelle@shell.xmission.com (Kenny McCormack) - 2020-08-22 16:23 +0000
      Re: max of 3 Bart <bc@freeuk.com> - 2020-08-22 20:45 +0100
  Re: max of 3 Barry Schwarz <schwarzb@delq.com> - 2020-08-22 08:26 -0700
  Re: max of 3 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-22 14:45 -0700
    Re: max of 3 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-22 14:47 -0700
      Re: max of 3 axel porin <porinaxel@gmail.com> - 2020-08-22 15:16 -0700
        Re: max of 3 luser droog <luser.droog@gmail.com> - 2020-08-22 15:28 -0700
        Re: max of 3 Real Troll <real.troll@trolls.com> - 2020-08-22 19:50 -0400
          Re: max of 3 Paul <nospam@needed.invalid> - 2020-08-22 19:44 -0400
            Re: max of 3 Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-24 14:29 -0700
          Re: max of 3 Barry Schwarz <schwarzb@delq.com> - 2020-08-22 18:32 -0700
            Re: max of 3 Real Troll <real.troll@trolls.com> - 2020-08-23 12:30 -0400
        Re: max of 3 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-22 16:37 -0700
    Re: max of 3 Ike Naar <ike@sdf.org> - 2020-08-22 22:10 +0000
      Re: max of 3 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-22 16:49 -0700
    Re: max of 3 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-22 16:50 -0700
  Re: max of 3 Real Troll<real.troll@trolls.com> - 2020-08-22 17:17 -0400
    Re: max of 3 Barry Schwarz <schwarzb@delq.com> - 2020-08-22 18:22 -0700
  Re: max of 3 luser droog <luser.droog@gmail.com> - 2020-08-22 15:42 -0700
    Re: max of 3 luser droog <luser.droog@gmail.com> - 2020-08-22 15:52 -0700
    Re: max of 3 Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-24 14:16 -0700
      Re: max of 3 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-24 14:37 -0700
        Re: max of 3 Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-08-24 15:25 -0700
          Re: max of 3 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-24 20:15 -0700
            Re: max of 3 luser droog <luser.droog@gmail.com> - 2020-08-24 20:32 -0700
              Re: max of 3 luser droog <luser.droog@gmail.com> - 2020-08-24 23:35 -0700
              Re: max of 3 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-27 14:03 -0700
              Re: max of 3 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-28 23:35 -0700
        Re: max of 3 Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-29 04:52 -0700
      Re: max of 3 luser droog <luser.droog@gmail.com> - 2020-08-24 20:27 -0700
        Re: max of 3 Vir Campestris <vir.campestris@invalid.invalid> - 2020-08-27 22:19 +0100
          Re: max of 3 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-27 14:36 -0700
            Re: max of 3 Kaz Kylheku <793-849-0957@kylheku.com> - 2020-08-27 22:27 +0000
              Re: max of 3 Vir Campestris <vir.campestris@invalid.invalid> - 2020-08-31 22:07 +0100
                Re: max of 3 Richard Damon <Richard@Damon-Family.org> - 2020-09-01 07:19 -0400
        Re: max of 3 Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-29 05:19 -0700
  Re: max of 3 DFS <nospam@dfs.com> - 2020-08-26 19:25 -0400

csiph-web